15954: Fix test-mode volume config.
[arvados.git] / lib / cmd / cmd_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package cmd
6
7 import (
8         "bytes"
9         "flag"
10         "fmt"
11         "io"
12         "strings"
13         "testing"
14
15         "git.arvados.org/arvados.git/lib/cmdtest"
16         check "gopkg.in/check.v1"
17 )
18
19 // Gocheck boilerplate
20 func Test(t *testing.T) {
21         check.TestingT(t)
22 }
23
24 var _ = check.Suite(&CmdSuite{})
25
26 type CmdSuite struct{}
27
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, " "))
31                 return 0
32         }),
33 })
34
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, "")
43 }
44
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, "")
53 }
54
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`)
63 }
64
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"})
72 }