github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/e2e/cli-plugins/help_test.go (about) 1 package cliplugins 2 3 import ( 4 "regexp" 5 "testing" 6 7 "gotest.tools/v3/assert" 8 is "gotest.tools/v3/assert/cmp" 9 "gotest.tools/v3/icmd" 10 ) 11 12 // TestGlobalHelp ensures correct behaviour when running `docker help` 13 func TestGlobalHelp(t *testing.T) { 14 run, _, cleanup := prepare(t) 15 defer cleanup() 16 17 res := icmd.RunCmd(run("help")) 18 res.Assert(t, icmd.Expected{ 19 ExitCode: 0, 20 }) 21 assert.Assert(t, is.Equal(res.Stderr(), "")) 22 output := res.Stdout() 23 24 // Instead of baking in the full current output of `docker 25 // help`, which can be expected to change regularly, bake in 26 // some checkpoints. Key things we are looking for: 27 // 28 // - The top-level description 29 // - Each of the main headings 30 // - Some builtin commands under the main headings 31 // - The `helloworld` plugin in the appropriate place 32 // - The `badmeta` plugin under the "Invalid Plugins" heading. 33 // 34 // Regexps are needed because the width depends on `unix.TIOCGWINSZ` or similar. 35 for _, s := range []string{ 36 `Management Commands:`, 37 `\s+container\s+Manage containers`, 38 `\s+helloworld\*\s+A basic Hello World plugin for tests`, 39 `\s+image\s+Manage images`, 40 `Commands:`, 41 `\s+create\s+Create a new container`, 42 `Invalid Plugins:`, 43 `\s+badmeta\s+invalid metadata: invalid character 'i' looking for beginning of object key string`, 44 } { 45 expected := regexp.MustCompile(`(?m)^` + s + `$`) 46 matches := expected.FindAllString(output, -1) 47 assert.Equal(t, len(matches), 1, "Did not find expected number of matches for %q in `docker help` output", expected) 48 } 49 50 // Running with `--help` should produce the same. 51 t.Run("help_flag", func(t *testing.T) { 52 res2 := icmd.RunCmd(run("--help")) 53 res2.Assert(t, icmd.Expected{ 54 ExitCode: 0, 55 }) 56 assert.Assert(t, is.Equal(res2.Stdout(), output)) 57 assert.Assert(t, is.Equal(res2.Stderr(), "")) 58 }) 59 60 // Running just `docker` (without `help` nor `--help`) should produce the same thing, except on Stderr. 61 t.Run("bare", func(t *testing.T) { 62 res2 := icmd.RunCmd(run()) 63 res2.Assert(t, icmd.Expected{ 64 ExitCode: 0, 65 }) 66 assert.Assert(t, is.Equal(res2.Stdout(), "")) 67 assert.Assert(t, is.Equal(res2.Stderr(), output)) 68 }) 69 70 t.Run("badopt", func(t *testing.T) { 71 // Running `docker --badopt` should also produce the 72 // same thing, give or take the leading error message 73 // and a trailing carriage return (due to main() using 74 // Println in the error case). 75 res2 := icmd.RunCmd(run("--badopt")) 76 res2.Assert(t, icmd.Expected{ 77 ExitCode: 125, 78 }) 79 assert.Assert(t, is.Equal(res2.Stdout(), "")) 80 assert.Assert(t, is.Contains(res2.Stderr(), "unknown flag: --badopt")) 81 assert.Assert(t, is.Contains(res2.Stderr(), "See 'docker --help")) 82 }) 83 }