forked from juju/cmd
/
output_test.go
146 lines (133 loc) · 3.58 KB
/
output_test.go
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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cmd_test
import (
"launchpad.net/gnuflag"
gc "launchpad.net/gocheck"
"github.com/juju/juju/cmd"
"github.com/juju/juju/testing"
)
// 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) {
c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
}
func (c *OutputCommand) Init(args []string) error {
return cmd.CheckEmpty(args)
}
func (c *OutputCommand) Run(ctx *cmd.Context) error {
return c.out.Write(ctx, c.value)
}
// 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"},
{true, "True\n"},
{false, "False\n"},
{"hello", "hello\n"},
{"\n\n\n", "\n\n\n\n"},
{"foo: bar", "foo: bar\n"},
{[]string{"blam", "dink"}, "blam\ndink\n"},
{map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"},
},
"smart": {
{nil, ""},
{"", ""},
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{true, "True\n"},
{false, "False\n"},
{"hello", "hello\n"},
{"\n\n\n", "\n\n\n\n"},
{"foo: bar", "foo: bar\n"},
{[]string{"blam", "dink"}, "blam\ndink\n"},
{map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"},
},
"json": {
{nil, "null\n"},
{"", `""` + "\n"},
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\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"},
},
"yaml": {
{nil, ""},
{"", `""` + "\n"},
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{true, "true\n"},
{false, "false\n"},
{"hello", "hello\n"},
{"\n\n\n", "'\n\n\n\n'\n"},
{"foo: bar", "'foo: bar'\n"},
{[]string{"blam", "dink"}, "- blam\n- dink\n"},
{defaultValue, "juju: 1\npuppet: false\n"},
},
}
func (s *CmdSuite) TestOutputFormat(c *gc.C) {
for format, tests := range outputTests {
c.Logf("format %s", format)
var args []string
if format != "" {
args = []string{"--format", format}
}
for i, t := range tests {
c.Logf(" test %d", i)
ctx := testing.Context(c)
result := cmd.Main(&OutputCommand{value: t.value}, ctx, args)
c.Check(result, gc.Equals, 0)
c.Check(bufferString(ctx.Stdout), gc.Equals, t.output)
c.Check(bufferString(ctx.Stderr), gc.Equals, "")
}
}
}
func (s *CmdSuite) TestUnknownOutputFormat(c *gc.C) {
ctx := testing.Context(c)
result := cmd.Main(&OutputCommand{}, ctx, []string{"--format", "cuneiform"})
c.Check(result, gc.Equals, 2)
c.Check(bufferString(ctx.Stdout), gc.Equals, "")
c.Check(bufferString(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 *CmdSuite) TestFormatAlternativeSyntax(c *gc.C) {
ctx := testing.Context(c)
result := cmd.Main(&OutputCommand{}, ctx, []string{"--format=json"})
c.Assert(result, gc.Equals, 0)
c.Assert(bufferString(ctx.Stdout), gc.Equals, "null\n")
}