12876: More compatibility with "arv get".
[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         "fmt"
10         "io"
11         "strings"
12         "testing"
13
14         check "gopkg.in/check.v1"
15 )
16
17 // Gocheck boilerplate
18 func Test(t *testing.T) {
19         check.TestingT(t)
20 }
21
22 var _ = check.Suite(&CmdSuite{})
23
24 type CmdSuite struct{}
25
26 var testCmd = Multi(map[string]RunFunc{
27         "echo": func(prog string, args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int {
28                 fmt.Fprintln(stdout, strings.Join(args, " "))
29                 return 0
30         },
31 })
32
33 func (s *CmdSuite) TestHello(c *check.C) {
34         stdout := bytes.NewBuffer(nil)
35         stderr := bytes.NewBuffer(nil)
36         exited := testCmd("prog", []string{"echo", "hello", "world"}, bytes.NewReader(nil), stdout, stderr)
37         c.Check(exited, check.Equals, 0)
38         c.Check(stdout.String(), check.Equals, "hello world\n")
39         c.Check(stderr.String(), check.Equals, "")
40 }
41
42 func (s *CmdSuite) TestWithLateSubcommand(c *check.C) {
43         stdout := bytes.NewBuffer(nil)
44         stderr := bytes.NewBuffer(nil)
45         run := WithLateSubcommand(testCmd, []string{"format", "f"}, []string{"n"})
46         exited := run("prog", []string{"--format=yaml", "-n", "-format", "beep", "echo", "hi"}, bytes.NewReader(nil), stdout, stderr)
47         c.Check(exited, check.Equals, 0)
48         c.Check(stdout.String(), check.Equals, "--format=yaml -n -format beep hi\n")
49         c.Check(stderr.String(), check.Equals, "")
50 }