Skip to content

Commit

Permalink
Merge pull request #59 from serokell/dk318/#49-escaping-characters-in…
Browse files Browse the repository at this point in the history
…-parse-errors

[#49] Escaping characters in parse errors
  • Loading branch information
DK318 authored Apr 12, 2022
2 parents 5948695 + 66ad004 commit 33390ff
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 15 deletions.
26 changes: 13 additions & 13 deletions lib/CLI/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

Expand All @@ -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
]

Expand All @@ -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
]

Expand All @@ -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
]

Expand All @@ -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
]

Expand All @@ -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:"
Expand All @@ -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
Expand All @@ -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
]

Expand All @@ -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:"
Expand Down Expand Up @@ -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:"
Expand Down
119 changes: 119 additions & 0 deletions tests/golden/common/common.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# SPDX-FileCopyrightText: 2020 Serokell <https://serokell.io>
#
# 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 - <<EOF
Invalid path: "bad\npath".
Path segments can only contain the following characters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'
EOF
}

@test "bad entry path" {
run coffer create "$(echo -e "bad/entry\npath")"

assert_failure
assert_output --partial - <<EOF
Invalid entry path: "bad/entry\npath".
Path segments can only contain the following characters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_'
EOF
}

@test "bad entry tag" {
run coffer tag /path "$(echo -e "bad\ntag")"

assert_failure
assert_output --partial - <<EOF
Invalid tag: "bad\ntag".
Tags can only contain the following characters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;'
EOF
}

@test "bad backend name" {
run coffer create "$(echo -e "bad\nbackend#/path")"

assert_failure
assert_output --partial - <<EOF
Invalid backend name: "bad\nbackend".
Backend name can only contain the following characters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;'
EOF
}

@test "bad field key" {
run coffer view /path "$(echo -e "bad\nfieldkey")"

assert_failure
assert_output --partial - <<EOF
Invalid field name: "bad\nfieldkey".
Tags can only contain the following characters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_;'
EOF
}

@test "bad qualified entry path" {
run coffer create "$(echo -e "back#/path#\n\nsmth")"

assert_failure
assert_output --partial - <<EOF
Invalid qualified entry path format: "back#/path#\n\nsmth".
Expected format is: [<backend-name>#]<entry-path>.
<backend-name> 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 - <<EOF
Invalid qualified path format: "back#/path#\n\nsmth".
Expected format is: [<backend-name>#]<path>.
<backend-name> 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 - <<EOF
option --field: Invalid field format: "bad\n\n=test".
Expected format: 'fieldname=fieldcontents'.
Parser error:
1:4:
|
1 | bad
| ^
unexpected newline
expecting '=', fieldname, or white space
EOF
}

@test "bad filter" {
run coffer find --filter "$(echo -e "bad\nfilter~str")"

assert_failure
assert_output --partial - <<EOF
option --filter: Invalid filter format: "bad\nfilter~str".
EOF
}

@test "bad filter field" {
run coffer find --filter-field "$(echo -e "name:bad\n\n~str")"

assert_failure
assert_output --partial - <<EOF
option --filter-field: Invalid filter-field format: "name:bad\n\n~str".
EOF
}
4 changes: 2 additions & 2 deletions tests/golden/create-command/create-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ load '../helpers'

assert_failure
assert_output --partial - <<EOF
Invalid entry path: '/'.
Invalid entry path: "/".
Entry paths must not be empty.
EOF
}
Expand Down Expand Up @@ -68,7 +68,7 @@ EOF

assert_failure
assert_output --partial - <<EOF
option --field: Invalid field format: '[email protected]'.
option --field: Invalid field format: "[email protected]".
Expected format: 'fieldname=fieldcontents'.
Parser error:
Expand Down

0 comments on commit 33390ff

Please sign in to comment.