From a5c651f33ade3353a9beb84d151ea45de0735f1e Mon Sep 17 00:00:00 2001 From: Leonid Vasilev Date: Mon, 4 Apr 2022 17:29:27 +0300 Subject: [PATCH 1/2] [#49] Escaping characters in parse errors Problem: we are not escaping failing input in parse errors. Some characters can be invisible (e.g. zero width space) so user can't figure out what is going on when he sees parse error. Solution: escaping failing input in parse errors. --- lib/CLI/Parser.hs | 26 +++++++++---------- .../golden/create-command/create-command.bats | 4 +-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/CLI/Parser.hs b/lib/CLI/Parser.hs index e1a6ad38..06cd50d6 100644 --- a/lib/CLI/Parser.hs +++ b/lib/CLI/Parser.hs @@ -317,14 +317,14 @@ tagOptions = readPath' :: Text -> Either String Path readPath' input = mkPath input & first \err -> unlines - [ "Invalid path: '" <> T.unpack input <> "'." + [ "Invalid path: " <> show input <> "." , T.unpack err ] readEntryPath' :: Text -> Either String EntryPath readEntryPath' input = mkEntryPath input & first \err -> unlines - [ "Invalid entry path: '" <> T.unpack input <> "'." + [ "Invalid entry path: " <> show input <> "." , T.unpack err ] @@ -335,14 +335,14 @@ readEntryTag :: ReadM EntryTag readEntryTag = do eitherReader \input -> newEntryTag (T.pack input) & first \err -> unlines - [ "Invalid tag: '" <> input <> "'." + [ "Invalid tag: " <> show input <> "." , T.unpack err ] readBackendName' :: Text -> Either String BackendName readBackendName' input = newBackendName input & first \err -> unlines - [ "Invalid backend name: '" <> T.unpack input <> "'." + [ "Invalid backend name: " <> show input <> "." , T.unpack err ] @@ -361,7 +361,7 @@ readFieldKey' input = do case newFieldKey input of Right tag -> pure tag Left err -> Left $ unlines - [ "Invalid field name: '" <> T.unpack input <> "'." + [ "Invalid field name: " <> show input <> "." , T.unpack err ] @@ -378,7 +378,7 @@ readQualifiedEntryPath = do pure $ QualifiedPath Nothing entryPath _ -> Left $ unlines - [ "Invalid qualified entry path format: '" <> input <> "'." + [ "Invalid qualified entry path format: " <> show input <> "." , show expectedQualifiedEntryPathFormat ] @@ -395,7 +395,7 @@ readQualifiedPath = do pure $ QualifiedPath Nothing path _ -> Left $ unlines - [ "Invalid qualified entry path format: '" <> input <> "'." + [ "Invalid qualified path format: " <> show input <> "." , show expectedQualifiedPathFormat ] @@ -406,7 +406,7 @@ readFieldInfo :: ReadM FieldInfo readFieldInfo = do eitherReader \input -> P.parse (parseFieldInfo <* P.eof) "" (T.pack input) & first \err -> unlines - [ "Invalid field format: '" <> input <> "'." + [ "Invalid field format: " <> show input <> "." , "Expected format: 'fieldname=fieldcontents'." , "" , "Parser error:" @@ -423,7 +423,7 @@ readSort = do "name" -> pure (SortByEntryName, direction') "date" -> pure (SortByEntryDate, direction') _ -> Left $ unlines - [ "Invalid sort: '" <> T.unpack means <> "'." + [ "Invalid sort: " <> show means <> "." , "Choose one of: 'name', 'date'." , "" , show expectedSortFormat @@ -435,13 +435,13 @@ readSort = do "value" -> pure (SortByFieldValue fieldName', direction') "date" -> pure (SortByFieldDate fieldName', direction') _ -> Left $ unlines - [ "Invalid sort: '" <> T.unpack means <> "'." + [ "Invalid sort: " <> show means <> "." , "Choose one of: 'value', 'date'." , "" , show expectedSortFormat ] _ -> Left $ unlines - [ "Invalid sort format: '" <> input <> "'." + [ "Invalid sort format: " <> show input <> "." , show expectedSortFormat ] @@ -467,7 +467,7 @@ readFilter :: ReadM Filter readFilter = do eitherReader \input -> P.parse (parseFilter <* P.eof) "" (T.pack input) & first \err -> unlines - [ "Invalid filter format: '" <> input <> "'." + [ "Invalid filter format: " <> show input <> "." , show expectedFilterFormat , "" , "Parser error:" @@ -500,7 +500,7 @@ readFilterField :: ReadM (FieldKey, FilterField) readFilterField = do eitherReader \input -> P.parse (parseFilterField <* P.eof) "" (T.pack input) & first \err -> unlines - [ "Invalid filter-field format: '" <> input <> "'." + [ "Invalid filter-field format: " <> show input <> "." , show expectedFilterFieldFormat , "" , "Parser error:" diff --git a/tests/golden/create-command/create-command.bats b/tests/golden/create-command/create-command.bats index c6735b23..66346acd 100644 --- a/tests/golden/create-command/create-command.bats +++ b/tests/golden/create-command/create-command.bats @@ -13,7 +13,7 @@ load '../helpers' assert_failure assert_output --partial - < Date: Mon, 4 Apr 2022 17:32:54 +0300 Subject: [PATCH 2/2] [#49] Failing input `bats` tests Problem: we don't have `bats` tests for failing input. Solution: add these tests. --- tests/golden/common/common.bats | 119 ++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 tests/golden/common/common.bats diff --git a/tests/golden/common/common.bats b/tests/golden/common/common.bats new file mode 100644 index 00000000..ec73b9e4 --- /dev/null +++ b/tests/golden/common/common.bats @@ -0,0 +1,119 @@ +# SPDX-FileCopyrightText: 2020 Serokell +# +# SPDX-License-Identifier: MPL-2.0 + +#!/usr/bin/env bats + +load '../helpers/bats-support/load' +load '../helpers/bats-assert/load' +load '../helpers' + +@test "bad path" { + run coffer view "$(echo -e "bad\npath")" + + assert_failure + assert_output --partial - <#]. + can be a string of the following characters: [a-zA-Z0-9] and symbols '-', '_', ';'. +Examples: 'vault_kv-backend#secrets/google', 'my/passwords/entry'. +EOF +} + +@test "bad qualified path" { + run coffer view "$(echo -e "back#/path#\n\nsmth")" + + assert_failure + assert_output --partial - <#]. + can be a string of the following characters: [a-zA-Z0-9] and symbols '-', '_', ';'. +Examples: 'vault_kv-backend#secrets/google', 'my/passwords/mypage/'. +EOF +} + +@test "bad field info" { + run coffer create /path --field "$(echo -e "bad\n\n=test")" + + assert_failure + assert_output --partial - <