launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/output_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package cmd_test
     5  
     6  import (
     7  	"launchpad.net/gnuflag"
     8  	gc "launchpad.net/gocheck"
     9  
    10  	"launchpad.net/juju-core/cmd"
    11  	"launchpad.net/juju-core/testing"
    12  )
    13  
    14  // OutputCommand is a command that uses the output.go formatters.
    15  type OutputCommand struct {
    16  	cmd.CommandBase
    17  	out   cmd.Output
    18  	value interface{}
    19  }
    20  
    21  func (c *OutputCommand) Info() *cmd.Info {
    22  	return &cmd.Info{
    23  		Name:    "output",
    24  		Args:    "<something>",
    25  		Purpose: "I like to output",
    26  		Doc:     "output",
    27  	}
    28  }
    29  
    30  func (c *OutputCommand) SetFlags(f *gnuflag.FlagSet) {
    31  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    32  }
    33  
    34  func (c *OutputCommand) Init(args []string) error {
    35  	return cmd.CheckEmpty(args)
    36  }
    37  
    38  func (c *OutputCommand) Run(ctx *cmd.Context) error {
    39  	return c.out.Write(ctx, c.value)
    40  }
    41  
    42  // use a struct to control field ordering.
    43  var defaultValue = struct {
    44  	Juju   int
    45  	Puppet bool
    46  }{1, false}
    47  
    48  var outputTests = map[string][]struct {
    49  	value  interface{}
    50  	output string
    51  }{
    52  	"": {
    53  		{nil, ""},
    54  		{"", ""},
    55  		{1, "1\n"},
    56  		{-1, "-1\n"},
    57  		{1.1, "1.1\n"},
    58  		{true, "True\n"},
    59  		{false, "False\n"},
    60  		{"hello", "hello\n"},
    61  		{"\n\n\n", "\n\n\n\n"},
    62  		{"foo: bar", "foo: bar\n"},
    63  		{[]string{"blam", "dink"}, "blam\ndink\n"},
    64  		{map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"},
    65  	},
    66  	"smart": {
    67  		{nil, ""},
    68  		{"", ""},
    69  		{1, "1\n"},
    70  		{-1, "-1\n"},
    71  		{1.1, "1.1\n"},
    72  		{true, "True\n"},
    73  		{false, "False\n"},
    74  		{"hello", "hello\n"},
    75  		{"\n\n\n", "\n\n\n\n"},
    76  		{"foo: bar", "foo: bar\n"},
    77  		{[]string{"blam", "dink"}, "blam\ndink\n"},
    78  		{map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"},
    79  	},
    80  	"json": {
    81  		{nil, "null\n"},
    82  		{"", `""` + "\n"},
    83  		{1, "1\n"},
    84  		{-1, "-1\n"},
    85  		{1.1, "1.1\n"},
    86  		{true, "true\n"},
    87  		{false, "false\n"},
    88  		{"hello", `"hello"` + "\n"},
    89  		{"\n\n\n", `"\n\n\n"` + "\n"},
    90  		{"foo: bar", `"foo: bar"` + "\n"},
    91  		{[]string{}, `[]` + "\n"},
    92  		{[]string{"blam", "dink"}, `["blam","dink"]` + "\n"},
    93  		{defaultValue, `{"Juju":1,"Puppet":false}` + "\n"},
    94  	},
    95  	"yaml": {
    96  		{nil, ""},
    97  		{"", `""` + "\n"},
    98  		{1, "1\n"},
    99  		{-1, "-1\n"},
   100  		{1.1, "1.1\n"},
   101  		{true, "true\n"},
   102  		{false, "false\n"},
   103  		{"hello", "hello\n"},
   104  		{"\n\n\n", "'\n\n\n\n'\n"},
   105  		{"foo: bar", "'foo: bar'\n"},
   106  		{[]string{"blam", "dink"}, "- blam\n- dink\n"},
   107  		{defaultValue, "juju: 1\npuppet: false\n"},
   108  	},
   109  }
   110  
   111  func (s *CmdSuite) TestOutputFormat(c *gc.C) {
   112  	for format, tests := range outputTests {
   113  		c.Logf("format %s", format)
   114  		var args []string
   115  		if format != "" {
   116  			args = []string{"--format", format}
   117  		}
   118  		for i, t := range tests {
   119  			c.Logf("  test %d", i)
   120  			ctx := testing.Context(c)
   121  			result := cmd.Main(&OutputCommand{value: t.value}, ctx, args)
   122  			c.Check(result, gc.Equals, 0)
   123  			c.Check(bufferString(ctx.Stdout), gc.Equals, t.output)
   124  			c.Check(bufferString(ctx.Stderr), gc.Equals, "")
   125  		}
   126  	}
   127  }
   128  
   129  func (s *CmdSuite) TestUnknownOutputFormat(c *gc.C) {
   130  	ctx := testing.Context(c)
   131  	result := cmd.Main(&OutputCommand{}, ctx, []string{"--format", "cuneiform"})
   132  	c.Check(result, gc.Equals, 2)
   133  	c.Check(bufferString(ctx.Stdout), gc.Equals, "")
   134  	c.Check(bufferString(ctx.Stderr), gc.Matches, ".*: unknown format \"cuneiform\"\n")
   135  }
   136  
   137  // Py juju allowed both --format json and --format=json. This test verifies that juju is
   138  // being built against a version of the gnuflag library (rev 14 or above) that supports
   139  // this argument format.
   140  // LP #1059921
   141  func (s *CmdSuite) TestFormatAlternativeSyntax(c *gc.C) {
   142  	ctx := testing.Context(c)
   143  	result := cmd.Main(&OutputCommand{}, ctx, []string{"--format=json"})
   144  	c.Assert(result, gc.Equals, 0)
   145  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "null\n")
   146  }