From b53012a09b0e1672c803a20fab103ed0a8802d11 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Wed, 8 May 2024 09:11:23 +0200 Subject: [PATCH] cli: make program outputs using the cli module testable in `cli/testdata` (#21456) --- vlib/cli/cli_test.v | 53 ++++++++++++------- .../testdata/default_command_flag.out} | 0 .../testdata/default_command_flag.vv} | 3 +- .../testdata/default_help.out} | 0 .../testdata/default_help.vv} | 0 vlib/cli/testdata/long_description.out | 10 ++++ vlib/cli/testdata/long_description.vv | 21 ++++++++ .../testdata/no_execute.out} | 0 .../testdata/no_execute.vv} | 0 9 files changed, 65 insertions(+), 22 deletions(-) rename vlib/{v/slow_tests/inout/cli_root_default_control.out => cli/testdata/default_command_flag.out} (100%) rename vlib/{v/slow_tests/inout/cli_root_default_control.vv => cli/testdata/default_command_flag.vv} (87%) rename vlib/{v/slow_tests/inout/cli_root_default_help.out => cli/testdata/default_help.out} (100%) rename vlib/{v/slow_tests/inout/cli_root_default_help.vv => cli/testdata/default_help.vv} (100%) create mode 100644 vlib/cli/testdata/long_description.out create mode 100644 vlib/cli/testdata/long_description.vv rename vlib/{v/slow_tests/inout/cli_command_no_execute.out => cli/testdata/no_execute.out} (100%) rename vlib/{v/slow_tests/inout/cli_command_no_execute.vv => cli/testdata/no_execute.vv} (100%) diff --git a/vlib/cli/cli_test.v b/vlib/cli/cli_test.v index 10f8c9aa8..8e1ff275f 100644 --- a/vlib/cli/cli_test.v +++ b/vlib/cli/cli_test.v @@ -1,25 +1,38 @@ module main -import cli { Command, Flag } +import v.util.diff +import term +import os -fn test_long_description() { - mut cmd := Command{ - name: 'cli' - description: 'An example of the cli library.' - version: '1.0.0' - } - mut greet_cmd := Command{ - name: 'greet' - description: 'Prints greeting in different languages.' - usage: '' - required_args: 1 +const vexe = @VEXE +const vroot = os.dir(vexe) + +fn test_cli_programs() { + testdata := os.join_path(vroot, 'vlib', 'cli', 'testdata') + mut has_err := false + for test in os.walk_ext(testdata, '.vv') { + print(test + ' ') + out_path := test.all_before_last('.vv') + '.out' + if !os.exists(out_path) { + println(term.red('FAIL')) + eprintln('failed to find output file for `${test}`') + has_err = true + continue + } + mut expected_out := os.read_file(out_path)! + mut test_out := os.execute('${vexe} run ${test}').output + $if windows { + expected_out = expected_out.replace('\r\n', '\n') + test_out = test_out.replace('\r\n', '\n') + } + diff_ := diff.compare_text(expected_out, test_out)! + if diff_ != '' { + println(term.red('FAIL')) + eprintln(diff_) + has_err = true + } else { + println(term.green('OK')) + } } - greet_cmd.add_flag(Flag{ - flag: .string_array - name: 'fun' - description: '\'{"uri":"mqtt://broker.emqx.io:1883","topic":"test_emq/1","filters":[{"producer_id":0,"trace_group":2,"string_id":3001},{"string_id":3002}]}\'' - }) - cmd.add_command(greet_cmd) - cmd.setup() - cmd.parse(['cli', 'greet', '-help']) + assert !has_err } diff --git a/vlib/v/slow_tests/inout/cli_root_default_control.out b/vlib/cli/testdata/default_command_flag.out similarity index 100% rename from vlib/v/slow_tests/inout/cli_root_default_control.out rename to vlib/cli/testdata/default_command_flag.out diff --git a/vlib/v/slow_tests/inout/cli_root_default_control.vv b/vlib/cli/testdata/default_command_flag.vv similarity index 87% rename from vlib/v/slow_tests/inout/cli_root_default_control.vv rename to vlib/cli/testdata/default_command_flag.vv index dda6398e6..644897264 100644 --- a/vlib/v/slow_tests/inout/cli_root_default_control.vv +++ b/vlib/cli/testdata/default_command_flag.vv @@ -1,5 +1,4 @@ import cli { Command, CommandFlag } -import os fn main() { mut cmd := Command{ @@ -10,5 +9,5 @@ fn main() { man: false } } - cmd.parse(os.args) + cmd.parse(['']) } diff --git a/vlib/v/slow_tests/inout/cli_root_default_help.out b/vlib/cli/testdata/default_help.out similarity index 100% rename from vlib/v/slow_tests/inout/cli_root_default_help.out rename to vlib/cli/testdata/default_help.out diff --git a/vlib/v/slow_tests/inout/cli_root_default_help.vv b/vlib/cli/testdata/default_help.vv similarity index 100% rename from vlib/v/slow_tests/inout/cli_root_default_help.vv rename to vlib/cli/testdata/default_help.vv diff --git a/vlib/cli/testdata/long_description.out b/vlib/cli/testdata/long_description.out new file mode 100644 index 000000000..27a6f4ce5 --- /dev/null +++ b/vlib/cli/testdata/long_description.out @@ -0,0 +1,10 @@ +Usage: cli greet [flags] + +Prints greeting in different languages. + +Flags: + -fun '{"uri":"mqtt://broker.emqx.io:1883","topic":"test_emq/1 +'{"uri":"mqtt://broker.emqx.io:1883","topic":"test_emq/1","filters":[{"producer_id":0,"trace_group":2,"string_id + ":3001},{"string_id":3002}]}' + -help Prints help information. + -man Prints the auto-generated manpage. diff --git a/vlib/cli/testdata/long_description.vv b/vlib/cli/testdata/long_description.vv new file mode 100644 index 000000000..b1d778e84 --- /dev/null +++ b/vlib/cli/testdata/long_description.vv @@ -0,0 +1,21 @@ +import cli { Command, Flag } + +mut cmd := Command{ + name: 'cli' + description: 'An example of the cli library.' + version: '1.0.0' +} +mut greet_cmd := Command{ + name: 'greet' + description: 'Prints greeting in different languages.' + usage: '' + required_args: 1 +} +greet_cmd.add_flag(Flag{ + flag: .string_array + name: 'fun' + description: '\'{"uri":"mqtt://broker.emqx.io:1883","topic":"test_emq/1","filters":[{"producer_id":0,"trace_group":2,"string_id":3001},{"string_id":3002}]}\'' +}) +cmd.add_command(greet_cmd) +cmd.setup() +cmd.parse(['cli', 'greet', '-help']) diff --git a/vlib/v/slow_tests/inout/cli_command_no_execute.out b/vlib/cli/testdata/no_execute.out similarity index 100% rename from vlib/v/slow_tests/inout/cli_command_no_execute.out rename to vlib/cli/testdata/no_execute.out diff --git a/vlib/v/slow_tests/inout/cli_command_no_execute.vv b/vlib/cli/testdata/no_execute.vv similarity index 100% rename from vlib/v/slow_tests/inout/cli_command_no_execute.vv rename to vlib/cli/testdata/no_execute.vv -- 2.39.5