github.com/dooferlad/cmd@v0.0.0-20150716022859-3edef806220b/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) TestContextGetenv(c *gc.C) {
    28  	ctx := cmdtesting.Context(c)
    29  	ctx.Env = make(map[string]string)
    30  	before := ctx.Getenv("foo")
    31  	ctx.Env["foo"] = "bar"
    32  	after := ctx.Getenv("foo")
    33  
    34  	c.Check(before, gc.Equals, "")
    35  	c.Check(after, gc.Equals, "bar")
    36  }
    37  
    38  func (s *CmdSuite) TestContextSetenv(c *gc.C) {
    39  	ctx := cmdtesting.Context(c)
    40  	before := ctx.Env["foo"]
    41  	ctx.Setenv("foo", "bar")
    42  	after := ctx.Env["foo"]
    43  
    44  	c.Check(before, gc.Equals, "")
    45  	c.Check(after, gc.Equals, "bar")
    46  }
    47  
    48  func (s *CmdSuite) TestInfo(c *gc.C) {
    49  	minimal := &TestCommand{Name: "verb", Minimal: true}
    50  	help := minimal.Info().Help(cmdtesting.NewFlagSet())
    51  	c.Assert(string(help), gc.Equals, minimalHelp)
    52  
    53  	full := &TestCommand{Name: "verb"}
    54  	f := cmdtesting.NewFlagSet()
    55  	var ignored string
    56  	f.StringVar(&ignored, "option", "", "option-doc")
    57  	help = full.Info().Help(f)
    58  	c.Assert(string(help), gc.Equals, fullHelp)
    59  
    60  	optionInfo := full.Info()
    61  	optionInfo.Doc = ""
    62  	help = optionInfo.Help(f)
    63  	c.Assert(string(help), gc.Equals, optionHelp)
    64  }
    65  
    66  var initErrorTests = []struct {
    67  	c    *TestCommand
    68  	help string
    69  }{
    70  	{&TestCommand{Name: "verb"}, fullHelp},
    71  	{&TestCommand{Name: "verb", Minimal: true}, minimalHelp},
    72  }
    73  
    74  func (s *CmdSuite) TestMainInitError(c *gc.C) {
    75  	for _, t := range initErrorTests {
    76  		ctx := cmdtesting.Context(c)
    77  		result := cmd.Main(t.c, ctx, []string{"--unknown"})
    78  		c.Assert(result, gc.Equals, 2)
    79  		c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
    80  		expected := "error: flag provided but not defined: --unknown\n"
    81  		c.Assert(bufferString(ctx.Stderr), gc.Equals, expected)
    82  	}
    83  }
    84  
    85  func (s *CmdSuite) TestMainRunError(c *gc.C) {
    86  	ctx := cmdtesting.Context(c)
    87  	result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "error"})
    88  	c.Assert(result, gc.Equals, 1)
    89  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
    90  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "error: BAM!\n")
    91  }
    92  
    93  func (s *CmdSuite) TestMainRunSilentError(c *gc.C) {
    94  	ctx := cmdtesting.Context(c)
    95  	result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "silent-error"})
    96  	c.Assert(result, gc.Equals, 1)
    97  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
    98  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    99  }
   100  
   101  func (s *CmdSuite) TestMainSuccess(c *gc.C) {
   102  	ctx := cmdtesting.Context(c)
   103  	result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "success!"})
   104  	c.Assert(result, gc.Equals, 0)
   105  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "success!\n")
   106  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   107  }
   108  
   109  func (s *CmdSuite) TestStdin(c *gc.C) {
   110  	const phrase = "Do you, Juju?"
   111  	ctx := cmdtesting.Context(c)
   112  	ctx.Stdin = bytes.NewBuffer([]byte(phrase))
   113  	result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "echo"})
   114  	c.Assert(result, gc.Equals, 0)
   115  	c.Assert(bufferString(ctx.Stdout), gc.Equals, phrase)
   116  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   117  }
   118  
   119  func (s *CmdSuite) TestMainHelp(c *gc.C) {
   120  	for _, arg := range []string{"-h", "--help"} {
   121  		ctx := cmdtesting.Context(c)
   122  		result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{arg})
   123  		c.Assert(result, gc.Equals, 0)
   124  		c.Assert(bufferString(ctx.Stdout), gc.Equals, fullHelp)
   125  		c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   126  	}
   127  }
   128  
   129  func (s *CmdSuite) TestDefaultContextReturnsErrorInDeletedDirectory(c *gc.C) {
   130  	ctx := cmdtesting.Context(c)
   131  	wd, err := os.Getwd()
   132  	c.Assert(err, gc.IsNil)
   133  	missing := ctx.Dir + "/missing"
   134  	err = os.Mkdir(missing, 0700)
   135  	c.Assert(err, gc.IsNil)
   136  	err = os.Chdir(missing)
   137  	c.Assert(err, gc.IsNil)
   138  	defer os.Chdir(wd)
   139  	err = os.Remove(missing)
   140  	c.Assert(err, gc.IsNil)
   141  	ctx, err = cmd.DefaultContext()
   142  	c.Assert(err, gc.ErrorMatches, `getwd: no such file or directory`)
   143  	c.Assert(ctx, gc.IsNil)
   144  }
   145  
   146  func (s *CmdSuite) TestCheckEmpty(c *gc.C) {
   147  	c.Assert(cmd.CheckEmpty(nil), gc.IsNil)
   148  	c.Assert(cmd.CheckEmpty([]string{"boo!"}), gc.ErrorMatches, `unrecognized args: \["boo!"\]`)
   149  }
   150  
   151  func (s *CmdSuite) TestZeroOrOneArgs(c *gc.C) {
   152  
   153  	expectValue := func(args []string, expected string) {
   154  		arg, err := cmd.ZeroOrOneArgs(args)
   155  		c.Assert(arg, gc.Equals, expected)
   156  		c.Assert(err, gc.IsNil)
   157  	}
   158  
   159  	expectValue(nil, "")
   160  	expectValue([]string{}, "")
   161  	expectValue([]string{"foo"}, "foo")
   162  
   163  	arg, err := cmd.ZeroOrOneArgs([]string{"foo", "bar"})
   164  	c.Assert(arg, gc.Equals, "")
   165  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bar"\]`)
   166  }