1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.
package cmd_test
import (
"github.com/juju/gnuflag"
"github.com/juju/loggo"
"github.com/juju/testing"
gc "gopkg.in/check.v1"
"github.com/juju/cmd/v3"
"github.com/juju/cmd/v3/cmdtesting"
)
// OutputCommand is a command that uses the output.go formatters.
type OutputCommand struct {
cmd.CommandBase
out cmd.Output
value interface{}
}
func (c *OutputCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "output",
Args: "<something>",
Purpose: "I like to output",
Doc: "output",
}
}
func (c *OutputCommand) SetFlags(f *gnuflag.FlagSet) {
formatters := make(map[string]cmd.Formatter, len(cmd.DefaultFormatters))
for k, v := range cmd.DefaultFormatters {
formatters[k] = v.Formatter
}
c.out.AddFlags(f, "smart", formatters)
}
func (c *OutputCommand) Init(args []string) error {
return cmd.CheckEmpty(args)
}
func (c *OutputCommand) Run(ctx *cmd.Context) error {
if value, ok := c.value.(overrideFormatter); ok {
return c.out.WriteFormatter(ctx, value.formatter, value.value)
}
return c.out.Write(ctx, c.value)
}
type overrideFormatter struct {
formatter cmd.Formatter
value interface{}
}
// use a struct to control field ordering.
var defaultValue = struct {
Juju int
Puppet bool
}{1, false}
var outputTests = map[string][]struct {
value interface{}
output string
}{
"": {
{nil, ""},
{"", ""},
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{10000000, "10000000\n"},
{true, "True\n"},
{false, "False\n"},
{"hello", "hello\n"},
{"\n\n\n", "\n\n\n\n"},
{"foo: bar", "foo: bar\n"},
{[]string{}, ""},
{[]string{"blam", "dink"}, "blam\ndink\n"},
{map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"},
{overrideFormatter{cmd.FormatSmart, "abc\ndef"}, "abc\ndef\n"},
},
"smart": {
{nil, ""},
{"", ""},
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{10000000, "10000000\n"},
{true, "True\n"},
{false, "False\n"},
{"hello", "hello\n"},
{"\n\n\n", "\n\n\n\n"},
{"foo: bar", "foo: bar\n"},
{[]string{}, ""},
{[]string{"blam", "dink"}, "blam\ndink\n"},
{map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"},
{overrideFormatter{cmd.FormatSmart, "abc\ndef"}, "abc\ndef\n"},
},
"json": {
{nil, "null\n"},
{"", `""` + "\n"},
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{10000000, "10000000\n"},
{true, "true\n"},
{false, "false\n"},
{"hello", `"hello"` + "\n"},
{"\n\n\n", `"\n\n\n"` + "\n"},
{"foo: bar", `"foo: bar"` + "\n"},
{[]string{}, `[]` + "\n"},
{[]string{"blam", "dink"}, `["blam","dink"]` + "\n"},
{defaultValue, `{"Juju":1,"Puppet":false}` + "\n"},
{overrideFormatter{cmd.FormatSmart, "abc\ndef"}, "abc\ndef\n"},
{overrideFormatter{cmd.FormatJson, struct{}{}}, "{}\n"},
},
"yaml": {
{nil, ""},
{"", `""` + "\n"},
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{10000000, "10000000\n"},
{true, "true\n"},
{false, "false\n"},
{"hello", "hello\n"},
{"\n\n\n", "|2+\n"},
{"foo: bar", "'foo: bar'\n"},
{[]string{}, "[]\n"},
{[]string{"blam", "dink"}, "- blam\n- dink\n"},
{defaultValue, "juju: 1\npuppet: false\n"},
{overrideFormatter{cmd.FormatSmart, "abc\ndef"}, "abc\ndef\n"},
{overrideFormatter{cmd.FormatYaml, struct{}{}}, "{}\n"},
},
}
type OutputSuite struct {
testing.LoggingCleanupSuite
ctx *cmd.Context
}
var _ = gc.Suite(&OutputSuite{})
func (s *OutputSuite) SetUpTest(c *gc.C) {
s.LoggingCleanupSuite.SetUpTest(c)
s.ctx = cmdtesting.Context(c)
loggo.ReplaceDefaultWriter(cmd.NewWarningWriter(s.ctx.Stderr))
}
func (s *OutputSuite) TestOutputFormat(c *gc.C) {
s.testOutputFormat(c, "")
}
func (s *OutputSuite) TestOutputFormatSmart(c *gc.C) {
s.testOutputFormat(c, "smart")
}
func (s *OutputSuite) TestOutputFormatJson(c *gc.C) {
s.testOutputFormat(c, "json")
}
func (s *OutputSuite) TestOutputFormatYaml(c *gc.C) {
s.testOutputFormat(c, "yaml")
}
func (s *OutputSuite) testOutputFormat(c *gc.C, format string) {
tests := outputTests[format]
var args []string
if format != "" {
args = []string{"--format", format}
}
for i, t := range tests {
c.Logf(" test %d", i)
s.SetUpTest(c)
result := cmd.Main(&OutputCommand{value: t.value}, s.ctx, args)
c.Check(result, gc.Equals, 0)
c.Check(bufferString(s.ctx.Stdout), gc.Equals, t.output)
c.Check(bufferString(s.ctx.Stderr), gc.Equals, "")
s.TearDownTest(c)
}
}
func (s *OutputSuite) TestUnknownOutputFormat(c *gc.C) {
result := cmd.Main(&OutputCommand{}, s.ctx, []string{"--format", "cuneiform"})
c.Check(result, gc.Equals, 2)
c.Check(bufferString(s.ctx.Stdout), gc.Equals, "")
c.Check(bufferString(s.ctx.Stderr), gc.Matches, ".*: unknown format \"cuneiform\"\n")
}
// Py juju allowed both --format json and --format=json. This test verifies that juju is
// being built against a version of the gnuflag library (rev 14 or above) that supports
// this argument format.
// LP #1059921
func (s *OutputSuite) TestFormatAlternativeSyntax(c *gc.C) {
result := cmd.Main(&OutputCommand{}, s.ctx, []string{"--format=json"})
c.Assert(result, gc.Equals, 0)
c.Assert(bufferString(s.ctx.Stdout), gc.Equals, "null\n")
}
func (s *OutputSuite) TestFormatters(c *gc.C) {
typeFormatters := cmd.DefaultFormatters
formatters := typeFormatters.Formatters()
c.Assert(len(typeFormatters), gc.Equals, len(formatters))
for k := range typeFormatters {
_, ok := formatters[k]
c.Assert(ok, gc.Equals, true)
}
}
|