diff --git a/command/test/integration/__snapshots__/test.ts.snap b/command/test/integration/__snapshots__/test.ts.snap index d5ca2819..3b22993e 100644 --- a/command/test/integration/__snapshots__/test.ts.snap +++ b/command/test/integration/__snapshots__/test.ts.snap @@ -1,52 +1,52 @@ export const snapshot = {}; snapshot[`command integration > should complete boolean arg 1`] = ` -"stdout: -true -false +stdout: +"true +false" stderr: -" +"" `; snapshot[`command integration > should complete boolean arg from foo command 1`] = ` -"stdout: -true -false +stdout: +"true +false" stderr: -" +"" `; snapshot[`command integration > should complete boolean arg from foo bar command 1`] = ` -"stdout: -true -false +stdout: +"true +false" stderr: -" +"" `; snapshot[`command integration > should complete available commands for help command 1`] = ` -"stdout: -foo +stdout: +"foo bar validation-error help -completions +completions" stderr: -" +"" `; snapshot[`command integration > should complete enum 1`] = ` -"stdout: -blue +stdout: +"blue yellow -red +red" stderr: -" +"" `; snapshot[`command integration > should generate bash completions 1`] = ` -\`stdout: -#!/usr/bin/env bash +stdout: +\`#!/usr/bin/env bash # bash completion support for completions-test v1.0.0 _completions_test() { @@ -86,8 +86,7 @@ _completions_test() { # shellcheck disable=SC2120 _completions_test_file_dir() { listFiles=1 - local IFS=\$'\\\\t\\ -' xspec #glob + local IFS=\$'\\\\t\\\\n' xspec #glob _completions_test_expand || return 0 if [ "\${1:-}" = -d ]; then @@ -339,10 +338,8 @@ _completions_test() { fi local values - values="\$( printf "\\ -%s" "\${opts[@]}" )" - local IFS=\$'\\ -' + values="\$( printf "\\\\n%s" "\${opts[@]}" )" + local IFS=\$'\\\\n' # shellcheck disable=SC2207 local result=(\$(compgen -W "\${values[@]}" -- "\${cur}")) if [[ \${#result[@]} -eq 0 ]]; then @@ -350,22 +347,21 @@ _completions_test() { COMPREPLY=(\$(compgen -f "\${cur}")) else # shellcheck disable=SC2207 - COMPREPLY=(\$(printf '%q\\ -' "\${result[@]}")) + COMPREPLY=(\$(printf '%q\\\\n' "\${result[@]}")) fi return 0 } complete -F _completions_test -o bashdefault -o default completions-test - -stderr: \` +stderr: +"" `; snapshot[`command integration > should override name in bash completions 1`] = ` -\`stdout: -#!/usr/bin/env bash +stdout: +\`#!/usr/bin/env bash # bash completion support for foo-command v1.0.0 _foo_command() { @@ -405,8 +401,7 @@ _foo_command() { # shellcheck disable=SC2120 _foo_command_file_dir() { listFiles=1 - local IFS=\$'\\\\t\\ -' xspec #glob + local IFS=\$'\\\\t\\\\n' xspec #glob _foo_command_expand || return 0 if [ "\${1:-}" = -d ]; then @@ -658,10 +653,8 @@ _foo_command() { fi local values - values="\$( printf "\\ -%s" "\${opts[@]}" )" - local IFS=\$'\\ -' + values="\$( printf "\\\\n%s" "\${opts[@]}" )" + local IFS=\$'\\\\n' # shellcheck disable=SC2207 local result=(\$(compgen -W "\${values[@]}" -- "\${cur}")) if [[ \${#result[@]} -eq 0 ]]; then @@ -669,22 +662,21 @@ _foo_command() { COMPREPLY=(\$(compgen -f "\${cur}")) else # shellcheck disable=SC2207 - COMPREPLY=(\$(printf '%q\\ -' "\${result[@]}")) + COMPREPLY=(\$(printf '%q\\\\n' "\${result[@]}")) fi return 0 } complete -F _foo_command -o bashdefault -o default foo-command - -stderr: \` +stderr: +"" `; snapshot[`command integration > should generate fish completions 1`] = ` -\`stdout: -#!/usr/bin/env fish +stdout: +\`#!/usr/bin/env fish # fish completion support for completions-test v1.0.0 function __fish_completions_test_using_command @@ -769,14 +761,14 @@ complete -c completions-test -n '__fish_completions_test_using_command __complet complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a help -d 'Show this help or the help of a sub-command.' complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -k -f -a '(completions-test completions complete command help)' complete -c completions-test -n '__fish_completions_test_using_command __completions_test_help' -s h -l help -x -k -f -d 'Show this help.' - -stderr: \` +stderr: +"" `; snapshot[`command integration > should override name in fish completions 1`] = ` -\`stdout: -#!/usr/bin/env fish +stdout: +\`#!/usr/bin/env fish # fish completion support for foo-command v1.0.0 function __fish_foo_command_using_command @@ -861,14 +853,14 @@ complete -c foo-command -n '__fish_foo_command_using_command __foo_command_compl complete -c foo-command -n '__fish_foo_command_using_command __foo_command' -k -f -a help -d 'Show this help or the help of a sub-command.' complete -c foo-command -n '__fish_foo_command_using_command __foo_command_help' -k -f -a '(foo-command completions complete command help)' complete -c foo-command -n '__fish_foo_command_using_command __foo_command_help' -s h -l help -x -k -f -d 'Show this help.' - -stderr: \` +stderr: +"" `; snapshot[`command integration > should generate zsh completions 1`] = ` -\`stdout: -#!/usr/bin/env zsh +stdout: +\`#!/usr/bin/env zsh # zsh completion support for completions-test v1.0.0 autoload -U is-at-least @@ -886,8 +878,7 @@ function __completions_test_complete { if _requested "\$name"; then # shellcheck disable=SC2034 lines="\$(completions-test completions complete "\${action}" "\${@}")" - values=("\${(ps:\\ -:)lines}") + values=("\${(ps:\\\\n:)lines}") if (( \${#values[@]} )); then while _next_label "\$name" expl "\$action"; do compadd -S '' "\${expl[@]}" "\${values[@]}" @@ -1326,14 +1317,14 @@ function _completions_test_completions_zsh_help() { # _completions_test "\${@}" compdef _completions_test completions-test - -stderr: \` +stderr: +"" `; snapshot[`command integration > should override name in zsh completions 1`] = ` -\`stdout: -#!/usr/bin/env zsh +stdout: +\`#!/usr/bin/env zsh # zsh completion support for foo-command v1.0.0 autoload -U is-at-least @@ -1351,8 +1342,7 @@ function __foo_command_complete { if _requested "\$name"; then # shellcheck disable=SC2034 lines="\$(foo-command completions complete "\${action}" "\${@}")" - values=("\${(ps:\\ -:)lines}") + values=("\${(ps:\\\\n:)lines}") if (( \${#values[@]} )); then while _next_label "\$name" expl "\$action"; do compadd -S '' "\${expl[@]}" "\${values[@]}" @@ -1791,14 +1781,14 @@ function _foo_command_completions_zsh_help() { # _foo_command "\${@}" compdef _foo_command foo-command - -stderr: \` +stderr: +"" `; snapshot[`command integration > should output command help with help command 1`] = ` -\`stdout: - +stdout: +\` \\x1b[1mUsage:\\x1b[22m \\x1b[95mcompletions-test \\x1b[33m<\\x1b[95m\\x1b[95mcolor\\x1b[95m\\x1b[33m>\\x1b[95m \\x1b[33m[\\x1b[95m\\x1b[95mpath...\\x1b[95m\\x1b[33m]\\x1b[95m\\x1b[39m \\x1b[1mVersion:\\x1b[22m \\x1b[33m1.0.0\\x1b[39m @@ -1830,14 +1820,14 @@ snapshot[`command integration > should output command help with help command 1`] \\x1b[94mhelp\\x1b[39m \\x1b[33m[\\x1b[39m\\x1b[95mcommand\\x1b[39m\\x1b[33m]\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show this help or the help of a sub-command. \\x1b[94mcompletions\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Generate shell completions. - -stderr: \` +stderr: +"" `; snapshot[`command integration > should output sub-command help with help command 1`] = ` -\`stdout: - +stdout: +\` \\x1b[1mUsage:\\x1b[22m \\x1b[95mcompletions-test foo\\x1b[39m \\x1b[1mVersion:\\x1b[22m \\x1b[33m1.0.0\\x1b[39m @@ -1857,14 +1847,14 @@ snapshot[`command integration > should output sub-command help with help command \\x1b[94mhelp\\x1b[39m \\x1b[33m[\\x1b[39m\\x1b[95mcommand\\x1b[39m\\x1b[33m]\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show this help or the help of a sub-command. \\x1b[94mbar\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Bar command. - -stderr: \` +stderr: +"" `; snapshot[`command integration > should output short help with -h flag 1`] = ` -\`stdout: - +stdout: +\` \\x1b[1mUsage:\\x1b[22m \\x1b[95mcompletions-test \\x1b[33m<\\x1b[95m\\x1b[95mcolor\\x1b[95m\\x1b[33m>\\x1b[95m \\x1b[33m[\\x1b[95m\\x1b[95mpath...\\x1b[95m\\x1b[33m]\\x1b[95m\\x1b[39m \\x1b[1mVersion:\\x1b[22m \\x1b[33m1.0.0\\x1b[39m @@ -1896,14 +1886,14 @@ snapshot[`command integration > should output short help with -h flag 1`] = ` \\x1b[94mhelp\\x1b[39m \\x1b[33m[\\x1b[39m\\x1b[95mcommand\\x1b[39m\\x1b[33m]\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show this help or the help of a sub-command. \\x1b[94mcompletions\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Generate shell completions. - -stderr: \` +stderr: +"" `; snapshot[`command integration > should output long help with --help flag 1`] = ` -\`stdout: - +stdout: +\` \\x1b[1mUsage:\\x1b[22m \\x1b[95mcompletions-test \\x1b[33m<\\x1b[95m\\x1b[95mcolor\\x1b[95m\\x1b[33m>\\x1b[95m \\x1b[33m[\\x1b[95m\\x1b[95mpath...\\x1b[95m\\x1b[33m]\\x1b[95m\\x1b[39m \\x1b[1mVersion:\\x1b[22m \\x1b[33m1.0.0\\x1b[39m @@ -1940,33 +1930,33 @@ snapshot[`command integration > should output long help with --help flag 1`] = ` \\x1b[94mhelp\\x1b[39m \\x1b[33m[\\x1b[39m\\x1b[95mcommand\\x1b[39m\\x1b[33m]\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show this help or the help of a sub-command. \\x1b[94mcompletions\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Generate shell completions. - -stderr: \` +stderr: +"" `; snapshot[`command integration > should output short version with -V flag 1`] = ` -"stdout: -1.0.0 - -stderr: +stdout: +"1.0.0 " +stderr: +"" `; snapshot[`command integration > should output long version with --version flag 1`] = ` -"stdout: -\\x1b[1mcompletions-test\\x1b[22m \\x1b[94m1.0.0\\x1b[39m +stdout: +"\\x1b[1mcompletions-test\\x1b[22m \\x1b[94m1.0.0\\x1b[39m \\x1b[1mmeta1\\x1b[22m \\x1b[94mvalue1\\x1b[39m \\x1b[1mmeta2\\x1b[22m \\x1b[94mvalue2\\x1b[39m \\x1b[1mmeta3\\x1b[22m \\x1b[94mvalue3\\x1b[39m - -stderr: " +stderr: +"" `; snapshot[`command integration > should print the help of sub-command on validation error 1`] = ` -'stdout: - +stdout: +" \\x1b[1mUsage:\\x1b[22m \\x1b[95mcompletions-test bar\\x1b[39m \\x1b[1mVersion:\\x1b[22m \\x1b[33m1.0.0\\x1b[39m @@ -1980,16 +1970,16 @@ snapshot[`command integration > should print the help of sub-command on validati \\x1b[94mhelp\\x1b[39m \\x1b[33m[\\x1b[39m\\x1b[95mcommand\\x1b[39m\\x1b[33m]\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show this help or the help of a sub-command. - +" stderr: -\\x1b[31m \\x1b[1merror\\x1b[22m: Missing required option "--file". +'\\x1b[31m \\x1b[1merror\\x1b[22m: Missing required option "--file". \\x1b[39m ' `; snapshot[`command integration > should print error message for unknown option with suggestion 1`] = ` -\`stdout: - +stdout: +\` \\x1b[1mUsage:\\x1b[22m \\x1b[95mcompletions-test \\x1b[33m<\\x1b[95m\\x1b[95mcolor\\x1b[95m\\x1b[33m>\\x1b[95m \\x1b[33m[\\x1b[95m\\x1b[95mpath...\\x1b[95m\\x1b[33m]\\x1b[95m\\x1b[39m \\x1b[1mVersion:\\x1b[22m \\x1b[33m1.0.0\\x1b[39m @@ -2021,16 +2011,16 @@ snapshot[`command integration > should print error message for unknown option wi \\x1b[94mhelp\\x1b[39m \\x1b[33m[\\x1b[39m\\x1b[95mcommand\\x1b[39m\\x1b[33m]\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show this help or the help of a sub-command. \\x1b[94mcompletions\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Generate shell completions. - +\` stderr: -\\x1b[31m \\x1b[1merror\\x1b[22m: Unknown option "--colorr". Did you mean option "--color"? +'\\x1b[31m \\x1b[1merror\\x1b[22m: Unknown option "--colorr". Did you mean option "--color"? \\x1b[39m -\` +' `; snapshot[`command integration > should print help and error message when validation error 1`] = ` -"stdout: - +stdout: +" \\x1b[1mUsage:\\x1b[22m \\x1b[95mcompletions-test validation-error\\x1b[39m \\x1b[1mVersion:\\x1b[22m \\x1b[33m1.0.0\\x1b[39m @@ -2043,9 +2033,9 @@ snapshot[`command integration > should print help and error message when validat \\x1b[94mhelp\\x1b[39m \\x1b[33m[\\x1b[39m\\x1b[95mcommand\\x1b[39m\\x1b[33m]\\x1b[39m \\x1b[31m\\x1b[1m-\\x1b[22m\\x1b[39m Show this help or the help of a sub-command. - +" stderr: -\\x1b[31m \\x1b[1merror\\x1b[22m: Validation error message. +"\\x1b[31m \\x1b[1merror\\x1b[22m: Validation error message. \\x1b[39m " `; diff --git a/prompt/test/integration/__snapshots__/checkbox_test.ts.snap b/prompt/test/integration/__snapshots__/checkbox_test.ts.snap index 00a95f1e..5327cbc2 100644 --- a/prompt/test/integration/__snapshots__/checkbox_test.ts.snap +++ b/prompt/test/integration/__snapshots__/checkbox_test.ts.snap @@ -1,8 +1,8 @@ export const snapshot = {}; snapshot[`checkbox prompt > should check an option 1`] = ` -"stdout: -? Select an option +stdout: +"? Select an option ❯ ✘ Foo ✘ Bar ✘ Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J @@ -19,7 +19,7 @@ snapshot[`checkbox prompt > should check an option 1`] = ` ✘ Bar ❯ ✔ Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J ? Select an option › Baz -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/checkbox_test.ts.windows.snap b/prompt/test/integration/__snapshots__/checkbox_test.ts.windows.snap index 7f33a25d..df0b310a 100644 --- a/prompt/test/integration/__snapshots__/checkbox_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/checkbox_test.ts.windows.snap @@ -1,8 +1,8 @@ export const snapshot = {}; snapshot[`checkbox prompt > should check an option 1`] = ` -"stdout: -? Select an option +stdout: +"? Select an option ❯ × Foo × Bar × Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J @@ -19,7 +19,7 @@ snapshot[`checkbox prompt > should check an option 1`] = ` × Bar ❯ √ Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J ? Select an option » Baz -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/confirm_test.ts.snap b/prompt/test/integration/__snapshots__/confirm_test.ts.snap index c35edcf5..9fbe8708 100644 --- a/prompt/test/integration/__snapshots__/confirm_test.ts.snap +++ b/prompt/test/integration/__snapshots__/confirm_test.ts.snap @@ -1,35 +1,35 @@ export const snapshot = {}; snapshot[`confirm prompt > should confirm 1`] = ` -"stdout: -? Please confirm (y/N) › +stdout: +"? Please confirm (y/N) › ❯ some hint\\x1b[1A\\x1b[26G\\x1b[G\\x1b[0J ? Please confirm (y/N) › yes ❯ some hint\\x1b[1A\\x1b[27G\\x1b[G\\x1b[0J ? Please confirm (y/N) › Yes -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`confirm prompt > should not confirm 1`] = ` -"stdout: -? Please confirm (y/N) › +stdout: +"? Please confirm (y/N) › ❯ some hint\\x1b[1A\\x1b[26G\\x1b[G\\x1b[0J ? Please confirm (y/N) › no ❯ some hint\\x1b[1A\\x1b[27G\\x1b[G\\x1b[0J ? Please confirm (y/N) › No -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`confirm prompt > should not confirm by default 1`] = ` -"stdout: -? Please confirm (y/N) › +stdout: +"? Please confirm (y/N) › ❯ some hint\\x1b[1A\\x1b[26G\\x1b[G\\x1b[0J ? Please confirm (y/N) › No -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/confirm_test.ts.windows.snap b/prompt/test/integration/__snapshots__/confirm_test.ts.windows.snap index 00978108..d14b538b 100644 --- a/prompt/test/integration/__snapshots__/confirm_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/confirm_test.ts.windows.snap @@ -1,35 +1,35 @@ export const snapshot = {}; snapshot[`confirm prompt > should confirm 1`] = ` -"stdout: -? Please confirm (y/N) » +stdout: +"? Please confirm (y/N) » ❯ some hint\\x1b[1A\\x1b[26G\\x1b[G\\x1b[0J ? Please confirm (y/N) » yes ❯ some hint\\x1b[1A\\x1b[27G\\x1b[G\\x1b[0J ? Please confirm (y/N) » Yes -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`confirm prompt > should not confirm 1`] = ` -"stdout: -? Please confirm (y/N) » +stdout: +"? Please confirm (y/N) » ❯ some hint\\x1b[1A\\x1b[26G\\x1b[G\\x1b[0J ? Please confirm (y/N) » no ❯ some hint\\x1b[1A\\x1b[27G\\x1b[G\\x1b[0J ? Please confirm (y/N) » No -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`confirm prompt > should not confirm by default 1`] = ` -"stdout: -? Please confirm (y/N) » +stdout: +"? Please confirm (y/N) » ❯ some hint\\x1b[1A\\x1b[26G\\x1b[G\\x1b[0J ? Please confirm (y/N) » No -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/input_test.ts.snap b/prompt/test/integration/__snapshots__/input_test.ts.snap index 1ac569e6..d4a2a492 100644 --- a/prompt/test/integration/__snapshots__/input_test.ts.snap +++ b/prompt/test/integration/__snapshots__/input_test.ts.snap @@ -1,65 +1,65 @@ export const snapshot = {}; snapshot[`input prompt > should enter some text 1`] = ` -"stdout: -? Whats your name? (foo) › \\x1b[28G\\x1b[G\\x1b[0J +stdout: +"? Whats your name? (foo) › \\x1b[28G\\x1b[G\\x1b[0J ? Whats your name? (foo) › foo bar\\x1b[35G\\x1b[G\\x1b[0J ? Whats your name? (foo) › foo bar -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`input prompt with suggestions > should enable suggestions and list 1`] = ` -"stdout: -? Whats your name? (foo) › +stdout: +"? Whats your name? (foo) › foo bar baz\\x1b[3A\\x1b[28G\\x1b[G\\x1b[0J ? Whats your name? (foo) › foo ❯ foo\\x1b[1A\\x1b[31G\\x1b[G\\x1b[0J ? Whats your name? (foo) › foo -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`input prompt with prefix > should change prefix 1`] = ` -"stdout: -PREFIX Whats your name? (foo) › \\x1b[33G\\x1b[G\\x1b[0J +stdout: +"PREFIX Whats your name? (foo) › \\x1b[33G\\x1b[G\\x1b[0J PREFIX Whats your name? (foo) › bar\\x1b[36G\\x1b[G\\x1b[0J PREFIX Whats your name? (foo) › bar -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`input prompt with writer set to stderr > should enter some text 1`] = ` -"stdout: - +stdout: +"" stderr: -? Whats your name? (foo) › \\x1b[28G\\x1b[G\\x1b[0J +"? Whats your name? (foo) › \\x1b[28G\\x1b[G\\x1b[0J ? Whats your name? (foo) › foo bar\\x1b[35G\\x1b[G\\x1b[0J ? Whats your name? (foo) › foo bar \\x1b[?25h\\x1b[?25h" `; snapshot[`input prompt without prefix > should disable prefix 1`] = ` -"stdout: -Whats your name? (foo) › \\x1b[26G\\x1b[G\\x1b[0J +stdout: +"Whats your name? (foo) › \\x1b[26G\\x1b[G\\x1b[0J Whats your name? (foo) › bar\\x1b[29G\\x1b[G\\x1b[0J Whats your name? (foo) › bar -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`input prompt with no location flag > should work without --location flag 1`] = ` -"stdout: -? Works without --location? (hope so) › \\x1b[41G\\x1b[G\\x1b[0J +stdout: +"? Works without --location? (hope so) › \\x1b[41G\\x1b[G\\x1b[0J ? Works without --location? (hope so) › yes\\x1b[44G\\x1b[G\\x1b[0J ? Works without --location? (hope so) › yes -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/input_test.ts.windows.snap b/prompt/test/integration/__snapshots__/input_test.ts.windows.snap index b979ac03..cf2fdd8d 100644 --- a/prompt/test/integration/__snapshots__/input_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/input_test.ts.windows.snap @@ -1,65 +1,65 @@ export const snapshot = {}; snapshot[`input prompt > should enter some text 1`] = ` -"stdout: -? Whats your name? (foo) » \\x1b[28G\\x1b[G\\x1b[0J +stdout: +"? Whats your name? (foo) » \\x1b[28G\\x1b[G\\x1b[0J ? Whats your name? (foo) » foo bar\\x1b[35G\\x1b[G\\x1b[0J ? Whats your name? (foo) » foo bar -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`input prompt with suggestions > should enable suggestions and list 1`] = ` -"stdout: -? Whats your name? (foo) » +stdout: +"? Whats your name? (foo) » foo bar baz\\x1b[3A\\x1b[28G\\x1b[G\\x1b[0J ? Whats your name? (foo) » foo ❯ foo\\x1b[1A\\x1b[31G\\x1b[G\\x1b[0J ? Whats your name? (foo) » foo -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`input prompt with prefix > should change prefix 1`] = ` -"stdout: -PREFIX Whats your name? (foo) » \\x1b[33G\\x1b[G\\x1b[0J +stdout: +"PREFIX Whats your name? (foo) » \\x1b[33G\\x1b[G\\x1b[0J PREFIX Whats your name? (foo) » bar\\x1b[36G\\x1b[G\\x1b[0J PREFIX Whats your name? (foo) » bar -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`input prompt with writer set to stderr > should enter some text 1`] = ` -"stdout: - +stdout: +"" stderr: -? Whats your name? (foo) » \\x1b[28G\\x1b[G\\x1b[0J +"? Whats your name? (foo) » \\x1b[28G\\x1b[G\\x1b[0J ? Whats your name? (foo) » foo bar\\x1b[35G\\x1b[G\\x1b[0J ? Whats your name? (foo) » foo bar \\x1b[?25h\\x1b[?25h" `; snapshot[`input prompt without prefix > should disable prefix 1`] = ` -"stdout: -Whats your name? (foo) » \\x1b[26G\\x1b[G\\x1b[0J +stdout: +"Whats your name? (foo) » \\x1b[26G\\x1b[G\\x1b[0J Whats your name? (foo) » bar\\x1b[29G\\x1b[G\\x1b[0J Whats your name? (foo) » bar -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`input prompt with no location flag > should work without --location flag 1`] = ` -"stdout: -? Works without --location? (hope so) » \\x1b[41G\\x1b[G\\x1b[0J +stdout: +"? Works without --location? (hope so) » \\x1b[41G\\x1b[G\\x1b[0J ? Works without --location? (hope so) » yes\\x1b[44G\\x1b[G\\x1b[0J ? Works without --location? (hope so) » yes -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/list_test.ts.snap b/prompt/test/integration/__snapshots__/list_test.ts.snap index e14e5d33..f7037ef8 100644 --- a/prompt/test/integration/__snapshots__/list_test.ts.snap +++ b/prompt/test/integration/__snapshots__/list_test.ts.snap @@ -1,13 +1,13 @@ export const snapshot = {}; snapshot[`list prompt > should enter some keywords 1`] = ` -"stdout: -? Enter some keywords (foo, far) › +stdout: +"? Enter some keywords (foo, far) › ❯ some hint...\\x1b[1A\\x1b[36G\\x1b[G\\x1b[0J ? Enter some keywords (foo, far) › foo, bar ❯ some hint...\\x1b[1A\\x1b[44G\\x1b[G\\x1b[0J ? Enter some keywords (foo, far) › foo, bar -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/list_test.ts.windows.snap b/prompt/test/integration/__snapshots__/list_test.ts.windows.snap index 67545dc0..d33084d3 100644 --- a/prompt/test/integration/__snapshots__/list_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/list_test.ts.windows.snap @@ -1,13 +1,13 @@ export const snapshot = {}; snapshot[`list prompt > should enter some keywords 1`] = ` -"stdout: -? Enter some keywords (foo, far) » +stdout: +"? Enter some keywords (foo, far) » ❯ some hint...\\x1b[1A\\x1b[36G\\x1b[G\\x1b[0J ? Enter some keywords (foo, far) » foo, bar ❯ some hint...\\x1b[1A\\x1b[44G\\x1b[G\\x1b[0J ? Enter some keywords (foo, far) » foo, bar -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/number_test.ts.snap b/prompt/test/integration/__snapshots__/number_test.ts.snap index 23443a1c..135a6495 100644 --- a/prompt/test/integration/__snapshots__/number_test.ts.snap +++ b/prompt/test/integration/__snapshots__/number_test.ts.snap @@ -1,8 +1,8 @@ export const snapshot = {}; snapshot[`number prompt > should enter a number 1`] = ` -"stdout: -? How old are you? (7) › \\x1b[26G\\x1b[G\\x1b[0J +stdout: +"? How old are you? (7) › \\x1b[26G\\x1b[G\\x1b[0J ? How old are you? (7) › 19\\x1b[28G\\x1b[G\\x1b[0J ? How old are you? (7) › 20\\x1b[27G\\x1b[G\\x1b[0J ? How old are you? (7) › 21\\x1b[27G\\x1b[G\\x1b[0J @@ -14,21 +14,21 @@ snapshot[`number prompt > should enter a number 1`] = ` ? How old are you? (7) › 19\\x1b[27G\\x1b[G\\x1b[0J ? How old are you? (7) › 20\\x1b[27G\\x1b[G\\x1b[0J ? How old are you? (7) › 20 -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`number prompt with float > should enter a floating number 1`] = ` -"stdout: -? How old are you? (7) › \\x1b[26G\\x1b[G\\x1b[0J +stdout: +"? How old are you? (7) › \\x1b[26G\\x1b[G\\x1b[0J ? How old are you? (7) › 19.\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) › 19.1\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) › 19.2\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) › 19.3\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) › 19.4\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) › 19.4 -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/number_test.ts.windows.snap b/prompt/test/integration/__snapshots__/number_test.ts.windows.snap index 3c2ab433..2c863446 100644 --- a/prompt/test/integration/__snapshots__/number_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/number_test.ts.windows.snap @@ -1,8 +1,8 @@ export const snapshot = {}; snapshot[`number prompt > should enter a number 1`] = ` -"stdout: -? How old are you? (7) » \\x1b[26G\\x1b[G\\x1b[0J +stdout: +"? How old are you? (7) » \\x1b[26G\\x1b[G\\x1b[0J ? How old are you? (7) » 19\\x1b[28G\\x1b[G\\x1b[0J ? How old are you? (7) » 20\\x1b[27G\\x1b[G\\x1b[0J ? How old are you? (7) » 21\\x1b[27G\\x1b[G\\x1b[0J @@ -14,21 +14,21 @@ snapshot[`number prompt > should enter a number 1`] = ` ? How old are you? (7) » 19\\x1b[27G\\x1b[G\\x1b[0J ? How old are you? (7) » 20\\x1b[27G\\x1b[G\\x1b[0J ? How old are you? (7) » 20 -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; snapshot[`number prompt with float > should enter a floating number 1`] = ` -"stdout: -? How old are you? (7) » \\x1b[26G\\x1b[G\\x1b[0J +stdout: +"? How old are you? (7) » \\x1b[26G\\x1b[G\\x1b[0J ? How old are you? (7) » 19.\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) » 19.1\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) » 19.2\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) » 19.3\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) » 19.4\\x1b[29G\\x1b[G\\x1b[0J ? How old are you? (7) » 19.4 -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/prompt_test.ts.snap b/prompt/test/integration/__snapshots__/prompt_test.ts.snap index 4421d827..0bbe7684 100644 --- a/prompt/test/integration/__snapshots__/prompt_test.ts.snap +++ b/prompt/test/integration/__snapshots__/prompt_test.ts.snap @@ -1,12 +1,18 @@ export const snapshot = {}; snapshot[`prompt method > should check an option 1`] = ` -'stdout: -[checkbox:before] input is: foo -{ input: "foo", number: 43, confirm: true, toggle: true, checkbox: [ "bar", "baz" ] } - +stdout: +'[checkbox:before] input is: foo +{ + input: "foo", + number: 43, + confirm: true, + toggle: true, + checkbox: [ "bar", "baz" ] +} +' stderr: -? Enter some text (default value) › \\x1b[37G\\x1b[G\\x1b[0J +"? Enter some text (default value) › \\x1b[37G\\x1b[G\\x1b[0J ? Enter some text (default value) › f\\x1b[38G\\x1b[G\\x1b[0J ? Enter some text (default value) › fo\\x1b[39G\\x1b[G\\x1b[0J ? Enter some text (default value) › foo\\x1b[40G\\x1b[G\\x1b[0J @@ -48,5 +54,5 @@ stderr: ✔ Bar ❯ ✔ Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J ? Select an option › Bar, Baz -\\x1b[?25h\\x1b[?25h\\x1b[?25h' +\\x1b[?25h\\x1b[?25h\\x1b[?25h" `; diff --git a/prompt/test/integration/__snapshots__/prompt_test.ts.windows.snap b/prompt/test/integration/__snapshots__/prompt_test.ts.windows.snap index 9d1088ec..ff8c5f05 100644 --- a/prompt/test/integration/__snapshots__/prompt_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/prompt_test.ts.windows.snap @@ -1,12 +1,18 @@ export const snapshot = {}; snapshot[`prompt method > should check an option 1`] = ` -'stdout: -[checkbox:before] input is: foo -{ input: "foo", number: 43, confirm: true, toggle: true, checkbox: [ "bar", "baz" ] } - +stdout: +'[checkbox:before] input is: foo +{ + input: "foo", + number: 43, + confirm: true, + toggle: true, + checkbox: [ "bar", "baz" ] +} +' stderr: -? Enter some text (default value) » \\x1b[37G\\x1b[G\\x1b[0J +"? Enter some text (default value) » \\x1b[37G\\x1b[G\\x1b[0J ? Enter some text (default value) » f\\x1b[38G\\x1b[G\\x1b[0J ? Enter some text (default value) » fo\\x1b[39G\\x1b[G\\x1b[0J ? Enter some text (default value) » foo\\x1b[40G\\x1b[G\\x1b[0J @@ -48,5 +54,5 @@ stderr: √ Bar ❯ √ Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J ? Select an option » Bar, Baz -\\x1b[?25h\\x1b[?25h\\x1b[?25h' +\\x1b[?25h\\x1b[?25h\\x1b[?25h" `; diff --git a/prompt/test/integration/__snapshots__/secret_test.ts.snap b/prompt/test/integration/__snapshots__/secret_test.ts.snap index c1786ac1..a04879fe 100644 --- a/prompt/test/integration/__snapshots__/secret_test.ts.snap +++ b/prompt/test/integration/__snapshots__/secret_test.ts.snap @@ -1,11 +1,11 @@ export const snapshot = {}; snapshot[`secret prompt > should enter a secret 1`] = ` -"stdout: -? enter your secret › \\x1b[23G\\x1b[G\\x1b[0J +stdout: +"? enter your secret › \\x1b[23G\\x1b[G\\x1b[0J ? enter your secret › ***\\x1b[26G\\x1b[G\\x1b[0J ? enter your secret › *** -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/secret_test.ts.windows.snap b/prompt/test/integration/__snapshots__/secret_test.ts.windows.snap index 8fd1cc96..38dc5089 100644 --- a/prompt/test/integration/__snapshots__/secret_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/secret_test.ts.windows.snap @@ -1,11 +1,11 @@ export const snapshot = {}; snapshot[`secret prompt > should enter a secret 1`] = ` -"stdout: -? enter your secret » \\x1b[23G\\x1b[G\\x1b[0J +stdout: +"? enter your secret » \\x1b[23G\\x1b[G\\x1b[0J ? enter your secret » ***\\x1b[26G\\x1b[G\\x1b[0J ? enter your secret » *** -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/select_test.ts.snap b/prompt/test/integration/__snapshots__/select_test.ts.snap index 531787d7..cf0bb7a2 100644 --- a/prompt/test/integration/__snapshots__/select_test.ts.snap +++ b/prompt/test/integration/__snapshots__/select_test.ts.snap @@ -1,8 +1,8 @@ export const snapshot = {}; snapshot[`select prompt > should select an option 1`] = ` -"stdout: -? Select an option +stdout: +"? Select an option ❯ Foo Bar Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J @@ -15,7 +15,7 @@ snapshot[`select prompt > should select an option 1`] = ` Bar ❯ Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J ? Select an option › Baz -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/select_test.ts.windows.snap b/prompt/test/integration/__snapshots__/select_test.ts.windows.snap index 03f5fdd7..1d027b3f 100644 --- a/prompt/test/integration/__snapshots__/select_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/select_test.ts.windows.snap @@ -1,8 +1,8 @@ export const snapshot = {}; snapshot[`select prompt > should select an option 1`] = ` -"stdout: -? Select an option +stdout: +"? Select an option ❯ Foo Bar Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J @@ -15,7 +15,7 @@ snapshot[`select prompt > should select an option 1`] = ` Bar ❯ Baz\\x1b[3A\\x1b[19G\\x1b[?25l\\x1b[G\\x1b[0J ? Select an option » Baz -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/toggle_test.ts.snap b/prompt/test/integration/__snapshots__/toggle_test.ts.snap index 9d22d7e1..44e4f34b 100644 --- a/prompt/test/integration/__snapshots__/toggle_test.ts.snap +++ b/prompt/test/integration/__snapshots__/toggle_test.ts.snap @@ -1,8 +1,8 @@ export const snapshot = {}; snapshot[`toggle prompt > should toggle the prompt 1`] = ` -"stdout: -? Please confirm (No) › No / Yes +stdout: +"? Please confirm (No) › No / Yes ❯ some hint\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J ? Please confirm (No) › No / Yes ❯ some hint\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J @@ -11,7 +11,7 @@ snapshot[`toggle prompt > should toggle the prompt 1`] = ` ? Please confirm (No) › No / Yes ❯ some hint\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J ? Please confirm (No) › Yes -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/prompt/test/integration/__snapshots__/toggle_test.ts.windows.snap b/prompt/test/integration/__snapshots__/toggle_test.ts.windows.snap index 318c742d..55734715 100644 --- a/prompt/test/integration/__snapshots__/toggle_test.ts.windows.snap +++ b/prompt/test/integration/__snapshots__/toggle_test.ts.windows.snap @@ -1,8 +1,8 @@ export const snapshot = {}; snapshot[`toggle prompt > should toggle the prompt 1`] = ` -"stdout: -? Please confirm (No) » No / Yes +stdout: +"? Please confirm (No) » No / Yes ❯ some hint\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J ? Please confirm (No) » No / Yes ❯ some hint\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J @@ -11,7 +11,7 @@ snapshot[`toggle prompt > should toggle the prompt 1`] = ` ? Please confirm (No) » No / Yes ❯ some hint\\x1b[1A\\x1b[0G\\x1b[?25l\\x1b[G\\x1b[0J ? Please confirm (No) » Yes -\\x1b[?25h\\x1b[?25h +\\x1b[?25h\\x1b[?25h" stderr: -" +"" `; diff --git a/testing/__snapshots__/snapshot_test.ts.snap b/testing/__snapshots__/snapshot_test.ts.snap index 4d590937..d2910fda 100644 --- a/testing/__snapshots__/snapshot_test.ts.snap +++ b/testing/__snapshots__/snapshot_test.ts.snap @@ -4,48 +4,48 @@ snapshot[`should run snapshot tests 1`] = ` "export const snapshot = {}; snapshot[\`should create a simple snapshot 1\`] = \` -\\"stdout: -foo - +stdout: +\\"foo +\\" stderr: -bar +\\"bar \\" \`; snapshot[\`should set deno args > step 1 1\`] = \` -'stdout: -[ \\"foo\\", \\"bar\\", \\"beep\\" ] - -stderr: +stdout: +'[ \\"foo\\", \\"bar\\", \\"beep\\" ] ' +stderr: +\\"\\" \`; snapshot[\`should set deno args > step 2 1\`] = \` -'stdout: -[ \\"foo\\", \\"bar\\", \\"boop\\" ] - -stderr: +stdout: +'[ \\"foo\\", \\"bar\\", \\"boop\\" ] ' +stderr: +\\"\\" \`; snapshot[\`should write to stdin > step 1 1\`] = \` -\\"stdout: -# stdin start +stdout: +\\"# stdin start foo # stdin end - -stderr: \\" +stderr: +\\"\\" \`; snapshot[\`should write to stdin > step 2 1\`] = \` -\\"stdout: -# stdin start +stdout: +\\"# stdin start bar # stdin end - -stderr: \\" +stderr: +\\"\\" \`; " `; @@ -54,11 +54,11 @@ snapshot[`should run snapshot tests 2`] = ` 'export const snapshot = {}; snapshot[\`should change the snapshot path 1\`] = \` -"stdout: -foo - +stdout: +"foo +" stderr: -bar +"bar " \`; ' @@ -68,11 +68,11 @@ snapshot[`should run snapshot tests 3`] = ` 'export const snapshot = {}; snapshot[\`should change the snapshot dir 1\`] = \` -"stdout: -foo - +stdout: +"foo +" stderr: -bar +"bar " \`; ' diff --git a/testing/_quote_string.ts b/testing/_quote_string.ts new file mode 100644 index 00000000..e28390d9 --- /dev/null +++ b/testing/_quote_string.ts @@ -0,0 +1,35 @@ +const ESCAPE_MAP: Partial> = Object.freeze({ + "\b": "\\b", + "\f": "\\f", + "\n": "\\n", + "\r": "\\r", + "\t": "\\t", + "\v": "\\v", +}); +const EXCLUDE_LIST = ["\n", "\r"]; +const ESCAPE_PATTERN = new RegExp("[\x00-\x1f\x7f-\x9f]", "g"); +const QUOTES = ['"', "'", "`"]; + +export function quoteString(str: string) { + const quote = QUOTES.find( + (c) => !str.includes(c), + ) ?? QUOTES[0]; + const escapePattern = new RegExp(`(?=[${quote}\\\\])`, "g"); + str = str.replace(escapePattern, "\\"); + str = replaceEscapeSequences(str); + return `${quote}${str}${quote}`; +} + +function replaceEscapeSequences(str: string) { + return str + .replace( + ESCAPE_PATTERN, + (sequence) => + EXCLUDE_LIST.includes(sequence) + ? sequence + : ESCAPE_MAP[sequence] || "\\x" + sequence + .charCodeAt(0) + .toString(16) + .padStart(2, "0"), + ); +} diff --git a/testing/snapshot.ts b/testing/snapshot.ts index 0630e785..b9e21534 100644 --- a/testing/snapshot.ts +++ b/testing/snapshot.ts @@ -1,4 +1,5 @@ import { eraseDown } from "../ansi/ansi_escapes.ts"; +import { quoteString } from "./_quote_string.ts"; import { AssertionError, assertSnapshot, basename, red } from "./deps.ts"; export interface SnapshotTestStep { @@ -66,6 +67,8 @@ export interface SnapshotTestOptions extends SnapshotTestStep { /** If at least one test has `only` set to `true`, only run tests that have * `only` set to `true` and fail the test suite. */ only?: boolean; + /** Function to use when serializing the snapshot. */ + serializer?: (actual: string) => string; } const encoder = new TextEncoder(); @@ -115,10 +118,10 @@ function registerTest(options: SnapshotTestOptions) { async fn(ctx) { const steps = Object.entries(options.steps ?? {}); if (steps.length) { - for (const [name, inputs] of steps) { + for (const [name, step] of steps) { await ctx.step({ name, - fn: (ctx) => fn(ctx, inputs), + fn: (ctx) => fn(ctx, step, name), }); } } else { @@ -127,8 +130,18 @@ function registerTest(options: SnapshotTestOptions) { }, }); - async function fn(ctx: Deno.TestContext, step?: SnapshotTestStep) { - const output: string = await runPrompt(options, step); + async function fn( + ctx: Deno.TestContext, + step?: SnapshotTestStep, + name?: string, + ) { + const { stdout, stderr } = await runPrompt(options, step); + + const serializer = options.serializer ?? quoteString; + const output = `stdout:\n${serializer(stdout)}\nstderr:\n${ + serializer(stderr) + }`; + const suffix = options.osSuffix?.includes(Deno.build.os) ? `.${Deno.build.os}` : ""; @@ -137,6 +150,7 @@ function registerTest(options: SnapshotTestOptions) { dir: options.dir, path: options.path ?? (options.dir ? undefined : `__snapshots__/${fileName}${suffix}.snap`), + serializer: (value) => value, }); } } @@ -144,7 +158,7 @@ function registerTest(options: SnapshotTestOptions) { async function runPrompt( options: SnapshotTestOptions, step?: SnapshotTestStep, -): Promise { +): Promise<{ stdout: string; stderr: string }> { let output: Deno.CommandOutput | undefined; let stdout: string | undefined; let stderr: string | undefined; @@ -185,8 +199,8 @@ async function runPrompt( } output = await child.output(); - stdout = new TextDecoder().decode(output.stdout); - stderr = new TextDecoder().decode(output.stderr); + stdout = addLineBreaks(new TextDecoder().decode(output.stdout)); + stderr = addLineBreaks(new TextDecoder().decode(output.stderr)); writer.releaseLock(); await child.stdin.close(); @@ -207,9 +221,15 @@ async function runPrompt( ); } - // Add a line break after each test input. - return "stdout:\n" + stdout.replaceAll(eraseDown(), eraseDown() + "\n") + - "\nstderr:\n" + stderr.replaceAll(eraseDown(), eraseDown() + "\n"); + return { stdout, stderr }; +} + +/** Add a line break after each test input. */ +function addLineBreaks(str: string) { + return str.replaceAll( + eraseDown(), + eraseDown() + "\n", + ); } async function runTest(options: SnapshotTestOptions) { diff --git a/testing/snapshot_test.ts b/testing/snapshot_test.ts index fdb89f70..715520e8 100644 --- a/testing/snapshot_test.ts +++ b/testing/snapshot_test.ts @@ -1,4 +1,5 @@ import { assert, assertSnapshot } from "../dev_deps.ts"; +import { quoteString } from "./_quote_string.ts"; import { dirname, fromFileUrl } from "./deps.ts"; Deno.test({ @@ -33,9 +34,9 @@ Deno.test({ const snapshot2Content = await Deno.readTextFile(snapshot2Path); const snapshot3Content = await Deno.readTextFile(snapshot3Path); - await assertSnapshot(ctx, snapshotContent); - await assertSnapshot(ctx, snapshot2Content); - await assertSnapshot(ctx, snapshot3Content); + await assertSnapshot(ctx, snapshotContent, { serializer: quoteString }); + await assertSnapshot(ctx, snapshot2Content, { serializer: quoteString }); + await assertSnapshot(ctx, snapshot3Content, { serializer: quoteString }); await Deno.remove(snapshotPath); await Deno.remove(snapshot2Path);