diff --git a/shellkin b/shellkin index 6503b36..21c9cc1 100755 --- a/shellkin +++ b/shellkin @@ -1,8 +1,8 @@ #!/usr/bin/env bash -# This script was generated by bashly 1.4.0 (https://bashly.dev) +# This script was generated by bashly 2.0.0.rc1 (https://bashly.dev) # Modifying it manually is not recommended -# :wrapper.bash3_bouncer +# :wrapper.bash_version_bouncer if ((BASH_VERSINFO[0] < 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] < 2))); then printf "bash version 4.2 or higher is required\n" >&2 exit 1 @@ -342,7 +342,7 @@ inspect_args() { local k if ((${#args[@]})); then - readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort) + readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | LC_ALL=C sort) echo args: for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}" @@ -1806,6 +1806,8 @@ step_run() { local j local step_type local body + local capture_index + local capture_value local -a tokens=() local -a capture_indexes=() @@ -1830,7 +1832,16 @@ step_run() { fi for j in "${!tokens[@]}"; do - printf -v "${tokens[$j]}" '%s' "${BASH_REMATCH[${capture_indexes[$j]}]}" + capture_index=${capture_indexes[$j]} + if [[ $capture_index == q* ]]; then + capture_index=${capture_index#q} + capture_value=${BASH_REMATCH[$capture_index]} + capture_value=${capture_value:1:-1} + else + capture_value=${BASH_REMATCH[$capture_index]} + fi + + printf -v "${tokens[$j]}" '%s' "$capture_value" export "${tokens[$j]}" done @@ -2220,7 +2231,6 @@ pattern_regex() { local remainder=$pattern local literal local quote - local quote_group while [[ $remainder =~ ^([^{}]*)\{([A-Za-z_][A-Za-z0-9_]*)\}(.*)$ ]]; do literal=${BASH_REMATCH[1]} @@ -2236,12 +2246,9 @@ pattern_regex() { if [[ -n $quote ]]; then literal=${literal%?} remainder=${remainder#?} - ((capture_index += 1)) - quote_group=$capture_index compiled+=$(_pattern_escape_literal "$literal") - compiled+='(['\''"])' - compiled+="(.+)\\$quote_group" + compiled+="('.+'|\".+\")" ((capture_index += 1)) continue fi @@ -2290,8 +2297,8 @@ pattern_capture_indexes() { if [[ -n $quote ]]; then remainder=${remainder#?} - ((capture_index += 2)) - indexes+=("$capture_index") + ((capture_index += 1)) + indexes+=("q$capture_index") continue fi @@ -2835,9 +2842,13 @@ bashly_run() { esac } -if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then - # :command.start +# :command.start +start() { command_line_args=("$@") initialize bashly_run "${command_line_args[@]}" +} + +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + start "$@" fi diff --git a/src/lib/step/core.sh b/src/lib/step/core.sh index 5652430..6f57509 100644 --- a/src/lib/step/core.sh +++ b/src/lib/step/core.sh @@ -25,6 +25,8 @@ step_run() { local j local step_type local body + local capture_index + local capture_value local -a tokens=() local -a capture_indexes=() @@ -49,7 +51,16 @@ step_run() { fi for j in "${!tokens[@]}"; do - printf -v "${tokens[$j]}" '%s' "${BASH_REMATCH[${capture_indexes[$j]}]}" + capture_index=${capture_indexes[$j]} + if [[ $capture_index == q* ]]; then + capture_index=${capture_index#q} + capture_value=${BASH_REMATCH[$capture_index]} + capture_value=${capture_value:1:-1} + else + capture_value=${BASH_REMATCH[$capture_index]} + fi + + printf -v "${tokens[$j]}" '%s' "$capture_value" export "${tokens[$j]}" done diff --git a/src/lib/stepdef/pattern.sh b/src/lib/stepdef/pattern.sh index e60c86e..bfa1d69 100644 --- a/src/lib/stepdef/pattern.sh +++ b/src/lib/stepdef/pattern.sh @@ -6,7 +6,6 @@ pattern_regex() { local remainder=$pattern local literal local quote - local quote_group while [[ $remainder =~ ^([^{}]*)\{([A-Za-z_][A-Za-z0-9_]*)\}(.*)$ ]]; do literal=${BASH_REMATCH[1]} @@ -22,12 +21,9 @@ pattern_regex() { if [[ -n $quote ]]; then literal=${literal%?} remainder=${remainder#?} - ((capture_index += 1)) - quote_group=$capture_index compiled+=$(_pattern_escape_literal "$literal") - compiled+='(['\''"])' - compiled+="(.+)\\$quote_group" + compiled+="('.+'|\".+\")" ((capture_index += 1)) continue fi @@ -78,8 +74,8 @@ pattern_capture_indexes() { if [[ -n $quote ]]; then remainder=${remainder#?} - ((capture_index += 2)) - indexes+=("$capture_index") + ((capture_index += 1)) + indexes+=("q$capture_index") continue fi diff --git a/test/lib/step/core.bats b/test/lib/step/core.bats index a0ecf6e..b8d378c 100644 --- a/test/lib/step/core.bats +++ b/test/lib/step/core.bats @@ -55,6 +55,15 @@ teardown() { [ "$STEP_RESULT" = "Something's wrong" ] } +@test "step_run rejects mismatched quote delimiters" { + stepdef_parse "@Then the text should include '{text}'" + stepdef_register 'STEP_RESULT="$text"' + + ! step_run Then "the text should include 'mismatched\"" + + [ -z "$STEP_RESULT" ] +} + @test "step_run returns non-zero when no step definition matches" { stepdef_parse "@When I run '{command}'" stepdef_register 'run "$command"' diff --git a/test/lib/stepdef/files.bats b/test/lib/stepdef/files.bats index 03cbdec..98c1f3b 100644 --- a/test/lib/stepdef/files.bats +++ b/test/lib/stepdef/files.bats @@ -42,9 +42,9 @@ EOF [ "${#STEPDEF_TYPES[@]}" -eq 2 ] [ "${STEPDEF_TYPES[0]}" = "When" ] [ "${STEPDEF_TYPES[1]}" = "Then" ] - [ "${STEPDEF_REGEXES[0]}" = "I run (['\"])(.+)\\1" ] - [ "${STEPDEF_CAPTURE_INDEXES_LIST[0]}" = "2" ] - [ "${STEPDEF_CAPTURE_INDEXES_LIST[1]}" = "2" ] + [ "${STEPDEF_REGEXES[0]}" = "I run ('.+'|\".+\")" ] + [ "${STEPDEF_CAPTURE_INDEXES_LIST[0]}" = "q1" ] + [ "${STEPDEF_CAPTURE_INDEXES_LIST[1]}" = "q1" ] } @test "stepdefs_file_parse preserves multi-line step bodies" { diff --git a/test/lib/stepdef/parse.bats b/test/lib/stepdef/parse.bats index f402a79..207280c 100644 --- a/test/lib/stepdef/parse.bats +++ b/test/lib/stepdef/parse.bats @@ -40,8 +40,8 @@ teardown() { @test "stepdef_parse derives the regex from the template" { stepdef_parse "@Then the file '{path}' should exist" - [ "$STEPDEF_REGEX" = "the file (['\"])(.+)\\1 should exist" ] - [ "$STEPDEF_CAPTURE_INDEXES" = "2" ] + [ "$STEPDEF_REGEX" = "the file ('.+'|\".+\") should exist" ] + [ "$STEPDEF_CAPTURE_INDEXES" = "q1" ] } @test "stepdef_parse returns token names in declaration order" { @@ -136,9 +136,9 @@ teardown() { [ "${STEPDEF_TYPES[0]}" = "When" ] [ "${STEPDEF_PATTERNS[0]}" = "I run '{command}'" ] - [ "${STEPDEF_REGEXES[0]}" = "I run (['\"])(.+)\\1" ] + [ "${STEPDEF_REGEXES[0]}" = "I run ('.+'|\".+\")" ] [ "${STEPDEF_TOKENS_LIST[0]}" = "command" ] - [ "${STEPDEF_CAPTURE_INDEXES_LIST[0]}" = "2" ] + [ "${STEPDEF_CAPTURE_INDEXES_LIST[0]}" = "q1" ] [ "${STEPDEF_BODIES[0]}" = 'run "$command"' ] } @@ -154,6 +154,6 @@ teardown() { [ "${STEPDEF_TYPES[1]}" = "Then" ] [ "${STEPDEF_TOKENS_LIST[0]}" = "directory" ] [ "${STEPDEF_TOKENS_LIST[1]}" = "path" ] - [ "${STEPDEF_CAPTURE_INDEXES_LIST[0]}" = "2" ] - [ "${STEPDEF_CAPTURE_INDEXES_LIST[1]}" = "2" ] + [ "${STEPDEF_CAPTURE_INDEXES_LIST[0]}" = "q1" ] + [ "${STEPDEF_CAPTURE_INDEXES_LIST[1]}" = "q1" ] } diff --git a/test/lib/stepdef/pattern.bats b/test/lib/stepdef/pattern.bats index cef4cf8..1ac028c 100644 --- a/test/lib/stepdef/pattern.bats +++ b/test/lib/stepdef/pattern.bats @@ -15,14 +15,14 @@ teardown() { run pattern_regex "I run '{command}'" [ "$status" -eq 0 ] - [ "$output" = "I run (['\"])(.+)\\1" ] + [ "$output" = "I run ('.+'|\".+\")" ] } @test "pattern_regex treats single and double quoted token delimiters interchangeably" { run pattern_regex 'I run "{command}"' [ "$status" -eq 0 ] - [ "$output" = "I run (['\"])(.+)\\1" ] + [ "$output" = "I run ('.+'|\".+\")" ] } @test "pattern_regex turns multiple tokens into capture groups" { @@ -78,5 +78,5 @@ teardown() { run pattern_capture_indexes "I copy {source} to '{destination}'" [ "$status" -eq 0 ] - [ "$output" = "1 3" ] + [ "$output" = "1 q2" ] }