Skip to content

Commit 389fd56

Browse files
committed
fix: More specific error on bare key with '!' and similar characters
This also fixes some other stuff
1 parent fc6c2ae commit 389fd56

File tree

6 files changed

+28
-19
lines changed

6 files changed

+28
-19
lines changed

pkg/lib/cmd/bash-toml.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
# shellcheck shell=bash
22

3-
if [ -n "${DEBUG+x}" ]; then
4-
trap 'bash_toml.trap_error' 'ERR'
5-
bash_toml.trap_error() {
6-
bash_toml.debug
7-
}
8-
fi
9-
103
for f in "$BASH_TOML_LIB_DIR"/util/?*.sh; do
114
# shellcheck disable=SC1090
125
source "$f"

pkg/lib/commands/parse.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ bash_toml.do_parse() {
66

77
declare char=
88
declare mode='MODE_DEFAULT'
9-
declare -i PARSER_LINE_NUMBER=0
9+
declare -i PARSER_LINE_NUMBER=1
1010
declare -i PARSER_COLUMN_NUMBER=0
1111

1212
while IFS= read -rn 1 char; do
@@ -78,7 +78,7 @@ bash_toml.do_parse() {
7878
bash_toml.parse_fail 'KEY_INVALID'
7979
return 1
8080
else
81-
bash_toml.parse_fail 'UNEXPECTED_BRANCH'
81+
bash_toml.parse_fail 'UNEXPECTED_CHARACTER' "Char '$char' is not valid in toml bare keys"
8282
return 1
8383
fi
8484
;;

pkg/lib/util/util.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ declare -gA BASH_TOML_ERRORS=(
1010
[VALUE_STRING_INVALID]='The value was not valid'
1111
)
1212

13-
declare -a token_history=()
13+
declare -a BASH_TOKEN_HISTORY=()
1414

1515
# @description Appends to token history for improved error insight
1616
bash_toml.token_history_add() {
1717
local str=
18-
printf -v str '%s\n' "$mode ($char) at $PARSER_LINE_NUMBER:$PARSER_COLUMN_NUMBER"
18+
printf -v str '%s' "$mode ($char) at $PARSER_LINE_NUMBER:$PARSER_COLUMN_NUMBER"
1919

20-
token_history+=("$str")
20+
BASH_TOKEN_HISTORY+=("$str")
2121

2222
if [ -n "${DEBUG_BASH_TOML+x}" ]; then
2323
if [ -n "${BATS_RUN_TMPDIR+x}" ]; then
@@ -32,6 +32,10 @@ bash_toml.parse_fail() {
3232
local error_key="$1"
3333
local error_context="$2"
3434

35+
if [ -z "$error_context" ]; then
36+
error_context="<empty>"
37+
fi
38+
3539
local error_message="${BASH_TOML_ERRORS["$error_key"]}"
3640

3741
local error_output=
@@ -41,8 +45,8 @@ bash_toml.parse_fail() {
4145
-> context: %s
4246
-> history:' "$error_key" "$error_message" "$error_context"
4347

44-
for history_item in "${token_history[@]}"; do
45-
printf -v error_output '%s\n - %s\n' "$error_output" "$history_item"
48+
for history_item in "${BASH_TOKEN_HISTORY[@]}"; do
49+
printf -v error_output '%s\n - %s' "$error_output" "$history_item"
4650
done
4751

4852
if [ "$TOML_MANUAL_ERROR" = yes ]; then

tests/equals.bats

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bats
2+
3+
load './util/init.sh'
4+
5+
@test "fails on invalid key 4" {
6+
run bash_toml.do_parse <<-"EOF"
7+
fox=
8+
EOF
9+
10+
assert_failure
11+
assert_output -p 'VALUE_STRING_INVALID'
12+
}

tests/fail.bats renamed to tests/key-bare.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
load './util/init.sh'
44

5-
@test "fails on incomplete key" {
5+
@test "fails on invalid bare key" {
66
run bash_toml.do_parse <<-"EOF"
77
fox
88
EOF
@@ -11,7 +11,7 @@ load './util/init.sh'
1111
assert_output -p 'KEY_INVALID'
1212
}
1313

14-
@test "fails on incomplete key 2" {
14+
@test "fails on invalid bare key 2" {
1515
run bash_toml.do_parse <<-"EOF"
1616
f ox
1717
EOF
@@ -20,11 +20,11 @@ load './util/init.sh'
2020
assert_output -p 'KEY_INVALID'
2121
}
2222

23-
@test "fails on incomplete key 3" {
23+
@test "fails on invalid bare key 3" {
2424
run bash_toml.do_parse <<-"EOF"
25-
fox=
25+
f!ox
2626
EOF
2727

2828
assert_failure
29-
assert_output -p 'VALUE_STRING_INVALID'
29+
assert_output -p 'UNEXPECTED_CHARACTER'
3030
}

tests/key-quoted.bats

Whitespace-only changes.

0 commit comments

Comments
 (0)