Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: make program outputs using the cli module testable in cli/testdata #21456

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions vlib/cli/cli_test.v
Original file line number Diff line number Diff line change
@@ -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: '<name>'
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
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cli { Command, CommandFlag }
import os

fn main() {
mut cmd := Command{
Expand All @@ -10,5 +9,5 @@ fn main() {
man: false
}
}
cmd.parse(os.args)
cmd.parse([''])
}
10 changes: 10 additions & 0 deletions vlib/cli/testdata/long_description.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Usage: cli greet [flags] <name>

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.
21 changes: 21 additions & 0 deletions vlib/cli/testdata/long_description.vv
Original file line number Diff line number Diff line change
@@ -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: '<name>'
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'])