❯ go-clikit
v0.10.0 pkg.go.dev ↗

packages › command line › cobratree/cobratreetest

cobratreetest

import "github.com/GSI-HPC/go-clikit/cobratree/cobratreetest"

Run a command tree in a test, and check the command lines that a manual shows against the tree.

since v0.6.0 imports: no package of the kitrequires: github.com/spf13/cobra, github.com/spf13/pflag decisions 24
The place of cobratree/cobratreetest in go-clikit: the packages it imports, and the packages that import it.termtextprogressdisplaycliprogressprogresstestmcpprogresseventlogfanoutfanouttestclioutdoctorfsutilcobratreecobratreetestlocalwebsessionsselocalwebtestlaunchassetspagehtmx

cobratreetest.Run runs the tree with the words of a command line, and returns standard output and standard error apart. A program writes its result to the one and its diagnostics to the other, and a test of either must tell them apart.

_ = func(t *testing.T) {
	stdout, stderr, err := cobratreetest.Run(t, newTree(), "get", "cluster")
	if err != nil || stdout != "NAME  NODES\n" || stderr != "prog: no realm given, using default\n" {
		t.Errorf("prog get cluster: %q, %q, %v", stdout, stderr, err)
	}
}
ExampleRun in cobratree/cobratreetest/example_test.go. CI compiles it.

Keep the manual true#

cobratreetest.CheckCommandLines reads text as a shell reads a script, finds the command lines that run the program, and checks that each names a command and flags that the tree has. When you rename a command or drop a flag, the test of the manual that still shows it fails.

$ prog get cluster --realm dev
eval "$(prog env dev)"
prog power on n[1-4] \
    --count 2 2>/dev/null
_ = func(t *testing.T) {
	manual := "$ prog get cluster dev -o json\nprog get cluster | wc -l\n"
	if n := cobratreetest.CheckCommandLines(t, newTree(), manual); n < 2 {
		t.Errorf("checked %d command lines of the manual, want 2", n)
	}
}
ExampleCheckCommandLines in cobratree/cobratreetest/example_test.go. CI compiles it.