Home > Backend Development > Golang > golang testscript .txtar syntax for text contained in stderr or stdout

golang testscript .txtar syntax for text contained in stderr or stdout

王林
Release: 2024-02-12 16:15:05
forward
500 people have browsed it

golang testscript .txtar 语法,用于 stderr 或 stdout 中包含的文本

Question content

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
Copy after login

...The actual error output is:

No help topic for 'totalfoobar'
Copy after login

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*
Copy after login

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?

Solution

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
Copy after login

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)
}
Copy after login

By the way, the mode it compiles in is as follows:

re, err := regexp.Compile(`(?m)` + pattern)
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template