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)
}
}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)
}
}cobratree/cobratreetest/example_test.go. CI compiles it.