1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
15 "git.arvados.org/arvados.git/lib/cmdtest"
16 check "gopkg.in/check.v1"
19 // Gocheck boilerplate
20 func Test(t *testing.T) {
24 var _ = check.Suite(&CmdSuite{})
26 type CmdSuite struct{}
28 var testCmd = Multi(map[string]Handler{
29 "echo": HandlerFunc(func(prog string, args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int {
30 fmt.Fprintln(stdout, strings.Join(args, " "))
35 func (s *CmdSuite) TestHello(c *check.C) {
36 defer cmdtest.LeakCheck(c)()
37 stdout := bytes.NewBuffer(nil)
38 stderr := bytes.NewBuffer(nil)
39 exited := testCmd.RunCommand("prog", []string{"echo", "hello", "world"}, bytes.NewReader(nil), stdout, stderr)
40 c.Check(exited, check.Equals, 0)
41 c.Check(stdout.String(), check.Equals, "hello world\n")
42 c.Check(stderr.String(), check.Equals, "")
45 func (s *CmdSuite) TestHelloViaProg(c *check.C) {
46 defer cmdtest.LeakCheck(c)()
47 stdout := bytes.NewBuffer(nil)
48 stderr := bytes.NewBuffer(nil)
49 exited := testCmd.RunCommand("/usr/local/bin/echo", []string{"hello", "world"}, bytes.NewReader(nil), stdout, stderr)
50 c.Check(exited, check.Equals, 0)
51 c.Check(stdout.String(), check.Equals, "hello world\n")
52 c.Check(stderr.String(), check.Equals, "")
55 func (s *CmdSuite) TestUsage(c *check.C) {
56 defer cmdtest.LeakCheck(c)()
57 stdout := bytes.NewBuffer(nil)
58 stderr := bytes.NewBuffer(nil)
59 exited := testCmd.RunCommand("prog", []string{"nosuchcommand", "hi"}, bytes.NewReader(nil), stdout, stderr)
60 c.Check(exited, check.Equals, 2)
61 c.Check(stdout.String(), check.Equals, "")
62 c.Check(stderr.String(), check.Matches, `(?ms)^prog: unrecognized command "nosuchcommand"\n.*echo.*\n`)
65 func (s *CmdSuite) TestSubcommandToFront(c *check.C) {
66 defer cmdtest.LeakCheck(c)()
67 flags := flag.NewFlagSet("", flag.ContinueOnError)
68 flags.String("format", "json", "")
69 flags.Bool("n", false, "")
70 args := SubcommandToFront([]string{"--format=yaml", "-n", "-format", "beep", "echo", "hi"}, flags)
71 c.Check(args, check.DeepEquals, []string{"echo", "--format=yaml", "-n", "-format", "beep", "hi"})