Home >Backend Development >Golang >golang testscript .txtar syntax for text contained in stderr or stdout
I am learning how to use the https://github.com/rogpeppe/go-internal testscript module to test the cli program.
When I use this line in the .txtar
file:
stderr /No help topic/gm
...The actual error output is:
No help topic for 'totalfoobar'
I get the error usage: stderr [-count=N] 'pattern'
.
I've looked at the documentation here and to be honest, it's hard to understand. I don't see any explanation of what -count=N
means.
I tried all variations of the following:
stderr -count=1 No help topic for 'totalfoobar' stderr No help topic for 'totalfoobar' stderr *No help topic*
No matter what the string is, the assertion still fails with the same error.
Q: How can I get a substring of the total stderr/stdout output for more flexible testing?
Use single quotes to quote the pattern:
// parse parses a single line as a list of space-separated arguments // subject to environment variable expansion (but not resplitting). // Single quotes around text disable splitting and expansion. // To embed a single quote, double it: 'Don”t communicate by sharing memory.' func (ts *TestScript) parse(line string) []string
The results are passed as args
arguments to scriptMatch at the end. The implementation of scriptMatch indicates that it requires only 1
arguments in addition to the optional -count flag. If No help topic
is not quoted, it will be parsed into 3 parameters, which is not as expected:
extraUsage := "" want := 1 // [code truncated] if len(args) != want { ts.Fatalf("usage: %s [-count=N] 'pattern'%s", name, extraUsage) }
By the way, the mode it compiles in is as follows:
re, err := regexp.Compile(`(?m)` + pattern)
Remember that you should write regular expressions using Go flavor syntax.
The above is the detailed content of golang testscript .txtar syntax for text contained in stderr or stdout. For more information, please follow other related articles on the PHP Chinese website!