github.com/mattyw/cmd@v0.0.0-20141203021739-f75cb03fd9ba/cmd_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the LGPLv3, see LICENSE file for details. 3 4 package cmd_test 5 6 import ( 7 "bytes" 8 "os" 9 "path/filepath" 10 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/cmd" 14 "github.com/juju/cmd/cmdtesting" 15 ) 16 17 type CmdSuite struct{} 18 19 var _ = gc.Suite(&CmdSuite{}) 20 21 func (s *CmdSuite) TestContext(c *gc.C) { 22 ctx := cmdtesting.Context(c) 23 c.Assert(ctx.AbsPath("/foo/bar"), gc.Equals, "/foo/bar") 24 c.Assert(ctx.AbsPath("foo/bar"), gc.Equals, filepath.Join(ctx.Dir, "foo/bar")) 25 } 26 27 func (s *CmdSuite) TestInfo(c *gc.C) { 28 minimal := &TestCommand{Name: "verb", Minimal: true} 29 help := minimal.Info().Help(cmdtesting.NewFlagSet()) 30 c.Assert(string(help), gc.Equals, minimalHelp) 31 32 full := &TestCommand{Name: "verb"} 33 f := cmdtesting.NewFlagSet() 34 var ignored string 35 f.StringVar(&ignored, "option", "", "option-doc") 36 help = full.Info().Help(f) 37 c.Assert(string(help), gc.Equals, fullHelp) 38 39 optionInfo := full.Info() 40 optionInfo.Doc = "" 41 help = optionInfo.Help(f) 42 c.Assert(string(help), gc.Equals, optionHelp) 43 } 44 45 var initErrorTests = []struct { 46 c *TestCommand 47 help string 48 }{ 49 {&TestCommand{Name: "verb"}, fullHelp}, 50 {&TestCommand{Name: "verb", Minimal: true}, minimalHelp}, 51 } 52 53 func (s *CmdSuite) TestMainInitError(c *gc.C) { 54 for _, t := range initErrorTests { 55 ctx := cmdtesting.Context(c) 56 result := cmd.Main(t.c, ctx, []string{"--unknown"}) 57 c.Assert(result, gc.Equals, 2) 58 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 59 expected := "error: flag provided but not defined: --unknown\n" 60 c.Assert(bufferString(ctx.Stderr), gc.Equals, expected) 61 } 62 } 63 64 func (s *CmdSuite) TestMainRunError(c *gc.C) { 65 ctx := cmdtesting.Context(c) 66 result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "error"}) 67 c.Assert(result, gc.Equals, 1) 68 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 69 c.Assert(bufferString(ctx.Stderr), gc.Equals, "error: BAM!\n") 70 } 71 72 func (s *CmdSuite) TestMainRunSilentError(c *gc.C) { 73 ctx := cmdtesting.Context(c) 74 result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "silent-error"}) 75 c.Assert(result, gc.Equals, 1) 76 c.Assert(bufferString(ctx.Stdout), gc.Equals, "") 77 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 78 } 79 80 func (s *CmdSuite) TestMainSuccess(c *gc.C) { 81 ctx := cmdtesting.Context(c) 82 result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "success!"}) 83 c.Assert(result, gc.Equals, 0) 84 c.Assert(bufferString(ctx.Stdout), gc.Equals, "success!\n") 85 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 86 } 87 88 func (s *CmdSuite) TestStdin(c *gc.C) { 89 const phrase = "Do you, Juju?" 90 ctx := cmdtesting.Context(c) 91 ctx.Stdin = bytes.NewBuffer([]byte(phrase)) 92 result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "echo"}) 93 c.Assert(result, gc.Equals, 0) 94 c.Assert(bufferString(ctx.Stdout), gc.Equals, phrase) 95 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 96 } 97 98 func (s *CmdSuite) TestMainHelp(c *gc.C) { 99 for _, arg := range []string{"-h", "--help"} { 100 ctx := cmdtesting.Context(c) 101 result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{arg}) 102 c.Assert(result, gc.Equals, 0) 103 c.Assert(bufferString(ctx.Stdout), gc.Equals, fullHelp) 104 c.Assert(bufferString(ctx.Stderr), gc.Equals, "") 105 } 106 } 107 108 func (s *CmdSuite) TestDefaultContextReturnsErrorInDeletedDirectory(c *gc.C) { 109 ctx := cmdtesting.Context(c) 110 wd, err := os.Getwd() 111 c.Assert(err, gc.IsNil) 112 missing := ctx.Dir + "/missing" 113 err = os.Mkdir(missing, 0700) 114 c.Assert(err, gc.IsNil) 115 err = os.Chdir(missing) 116 c.Assert(err, gc.IsNil) 117 defer os.Chdir(wd) 118 err = os.Remove(missing) 119 c.Assert(err, gc.IsNil) 120 ctx, err = cmd.DefaultContext() 121 c.Assert(err, gc.ErrorMatches, `getwd: no such file or directory`) 122 c.Assert(ctx, gc.IsNil) 123 } 124 125 func (s *CmdSuite) TestCheckEmpty(c *gc.C) { 126 c.Assert(cmd.CheckEmpty(nil), gc.IsNil) 127 c.Assert(cmd.CheckEmpty([]string{"boo!"}), gc.ErrorMatches, `unrecognized args: \["boo!"\]`) 128 } 129 130 func (s *CmdSuite) TestZeroOrOneArgs(c *gc.C) { 131 132 expectValue := func(args []string, expected string) { 133 arg, err := cmd.ZeroOrOneArgs(args) 134 c.Assert(arg, gc.Equals, expected) 135 c.Assert(err, gc.IsNil) 136 } 137 138 expectValue(nil, "") 139 expectValue([]string{}, "") 140 expectValue([]string{"foo"}, "foo") 141 142 arg, err := cmd.ZeroOrOneArgs([]string{"foo", "bar"}) 143 c.Assert(arg, gc.Equals, "") 144 c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bar"\]`) 145 }