-
Notifications
You must be signed in to change notification settings - Fork 32
/
util_test.go
100 lines (84 loc) · 1.76 KB
/
util_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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.
package cmd_test
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/juju/gnuflag"
"github.com/juju/cmd/v4"
)
func bufferString(stream io.Writer) string {
return stream.(*bytes.Buffer).String()
}
// TestCommand is used by several different tests.
type TestCommand struct {
cmd.CommandBase
Name string
Option string
Minimal bool
Aliases []string
FlagAKA string
CustomRun func(*cmd.Context) error
}
func (c *TestCommand) Info() *cmd.Info {
if c.Minimal {
return &cmd.Info{Name: c.Name}
}
i := &cmd.Info{
Name: c.Name,
Args: "<something>",
Purpose: c.Name + " the juju",
Doc: c.Name + "-doc",
Aliases: c.Aliases,
}
if c.FlagAKA != "" {
i.FlagKnownAs = c.FlagAKA
}
return i
}
func (c *TestCommand) SetFlags(f *gnuflag.FlagSet) {
if !c.Minimal {
f.StringVar(&c.Option, "option", "", "option-doc")
}
}
func (c *TestCommand) Init(args []string) error {
return cmd.CheckEmpty(args)
}
func (c *TestCommand) Run(ctx *cmd.Context) error {
if c.CustomRun != nil {
return c.CustomRun(ctx)
}
switch c.Option {
case "error":
return errors.New("BAM!")
case "silent-error":
return cmd.ErrSilent
case "echo":
_, err := io.Copy(ctx.Stdout, ctx.Stdin)
return err
default:
fmt.Fprintln(ctx.Stdout, c.Option)
}
return nil
}
// minimalHelp and fullHelp are the expected help strings for a TestCommand
// with Name "verb", with and without Minimal set.
var minimalHelp = "Usage: verb\n"
var optionHelp = `Usage: verb [options] <something>
Summary:
verb the juju
Options:
--option (= "")
option-doc
`
var fullHelp = `Usage: verb [%vs] <something>
Summary:
verb the juju
%vs:
--option (= "")
option-doc
Details:
verb-doc
`