Skip to content

Commit

Permalink
e2e: cleanup TestGlobalHelp() to be less brittle
Browse files Browse the repository at this point in the history
- remove check for "A self-sufficient runtime for containers"; really
  not important to check for.
- don't make the checks positional (just match that we find them, and
  that we don't find them multiple times)
- account for leading whitespace to change instead of hard-coding the
  number of spaces before output.
- change the badopt check; I think it should be sufficient to check
  that the bad option was printed and that "run --help" output is
  printed.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Mar 27, 2022
1 parent dd73973 commit bc2b48a
Showing 1 changed file with 17 additions and 43 deletions.
60 changes: 17 additions & 43 deletions e2e/cli-plugins/help_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cliplugins

import (
"bufio"
"regexp"
"strings"
"testing"

"gotest.tools/v3/assert"
Expand All @@ -21,7 +19,7 @@ func TestGlobalHelp(t *testing.T) {
ExitCode: 0,
})
assert.Assert(t, is.Equal(res.Stderr(), ""))
scanner := bufio.NewScanner(strings.NewReader(res.Stdout()))
output := res.Stdout()

// Instead of baking in the full current output of `docker
// help`, which can be expected to change regularly, bake in
Expand All @@ -34,52 +32,28 @@ func TestGlobalHelp(t *testing.T) {
// - The `badmeta` plugin under the "Invalid Plugins" heading.
//
// Regexps are needed because the width depends on `unix.TIOCGWINSZ` or similar.
helloworldre := regexp.MustCompile(`^ helloworld\*\s+A basic Hello World plugin for tests \(Docker Inc\., testing\)$`)
badmetare := regexp.MustCompile(`^ badmeta\s+invalid metadata: invalid character 'i' looking for beginning of object key string$`)
var helloworldcount, badmetacount int
for _, expected := range []*regexp.Regexp{
regexp.MustCompile(`^A self-sufficient runtime for containers$`),
regexp.MustCompile(`^Management Commands:$`),
regexp.MustCompile(`^ container\s+Manage containers$`),
helloworldre,
regexp.MustCompile(`^ image\s+Manage images$`),
regexp.MustCompile(`^Commands:$`),
regexp.MustCompile(`^ create\s+Create a new container$`),
regexp.MustCompile(`^Invalid Plugins:$`),
badmetare,
nil, // scan to end of input rather than stopping at badmetare
for _, s := range []string{
`Management Commands:`,
`\s+container\s+Manage containers`,
`\s+helloworld\*\s+A basic Hello World plugin for tests \(Docker Inc\., testing\)`,
`\s+image\s+Manage images`,
`Commands:`,
`\s+create\s+Create a new container`,
`Invalid Plugins:`,
`\s+badmeta\s+invalid metadata: invalid character 'i' looking for beginning of object key string`,
} {
var found bool
for scanner.Scan() {
text := scanner.Text()
if helloworldre.MatchString(text) {
helloworldcount++
}
if badmetare.MatchString(text) {
badmetacount++
}

if expected != nil && expected.MatchString(text) {
found = true
break
}
}
assert.Assert(t, expected == nil || found, "Did not find match for %q in `docker help` output", expected)
expected := regexp.MustCompile(`(?m)^` + s + `$`)
matches := expected.FindAllString(output, -1)
assert.Equal(t, len(matches), 1, "Did not find expected number of matches for %q in `docker help` output", expected)
}
// We successfully scanned all the input
assert.Assert(t, !scanner.Scan())
assert.NilError(t, scanner.Err())
// Plugins should only be listed once.
assert.Assert(t, is.Equal(helloworldcount, 1))
assert.Assert(t, is.Equal(badmetacount, 1))

// Running with `--help` should produce the same.
t.Run("help_flag", func(t *testing.T) {
res2 := icmd.RunCmd(run("--help"))
res2.Assert(t, icmd.Expected{
ExitCode: 0,
})
assert.Assert(t, is.Equal(res2.Stdout(), res.Stdout()))
assert.Assert(t, is.Equal(res2.Stdout(), output))
assert.Assert(t, is.Equal(res2.Stderr(), ""))
})

Expand All @@ -90,7 +64,7 @@ func TestGlobalHelp(t *testing.T) {
ExitCode: 0,
})
assert.Assert(t, is.Equal(res2.Stdout(), ""))
assert.Assert(t, is.Equal(res2.Stderr(), res.Stdout()))
assert.Assert(t, is.Equal(res2.Stderr(), output))
})

t.Run("badopt", func(t *testing.T) {
Expand All @@ -103,7 +77,7 @@ func TestGlobalHelp(t *testing.T) {
ExitCode: 125,
})
assert.Assert(t, is.Equal(res2.Stdout(), ""))
exp := "unknown flag: --badopt\nSee 'docker --help'.\n" + res.Stdout() + "\n"
assert.Assert(t, is.Equal(res2.Stderr(), exp))
assert.Assert(t, is.Contains(res2.Stderr(), "unknown flag: --badopt"))
assert.Assert(t, is.Contains(res2.Stderr(), "See 'docker --help"))
})
}

0 comments on commit bc2b48a

Please sign in to comment.