~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/cmd/util_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package cmd_test
 
2
 
 
3
import (
 
4
        "bytes"
 
5
        "errors"
 
6
        "fmt"
 
7
        "io"
 
8
        "launchpad.net/gnuflag"
 
9
        "launchpad.net/juju-core/cmd"
 
10
)
 
11
 
 
12
func bufferString(stream io.Writer) string {
 
13
        return stream.(*bytes.Buffer).String()
 
14
}
 
15
 
 
16
// TestCommand is used by several different tests.
 
17
type TestCommand struct {
 
18
        cmd.CommandBase
 
19
        Name    string
 
20
        Option  string
 
21
        Minimal bool
 
22
        Aliases []string
 
23
}
 
24
 
 
25
func (c *TestCommand) Info() *cmd.Info {
 
26
        if c.Minimal {
 
27
                return &cmd.Info{Name: c.Name}
 
28
        }
 
29
        return &cmd.Info{
 
30
                Name:    c.Name,
 
31
                Args:    "<something>",
 
32
                Purpose: c.Name + " the juju",
 
33
                Doc:     c.Name + "-doc",
 
34
                Aliases: c.Aliases,
 
35
        }
 
36
}
 
37
 
 
38
func (c *TestCommand) SetFlags(f *gnuflag.FlagSet) {
 
39
        if !c.Minimal {
 
40
                f.StringVar(&c.Option, "option", "", "option-doc")
 
41
        }
 
42
}
 
43
 
 
44
func (c *TestCommand) Init(args []string) error {
 
45
        return cmd.CheckEmpty(args)
 
46
}
 
47
 
 
48
func (c *TestCommand) Run(ctx *cmd.Context) error {
 
49
        switch c.Option {
 
50
        case "error":
 
51
                return errors.New("BAM!")
 
52
        case "silent-error":
 
53
                return cmd.ErrSilent
 
54
        case "echo":
 
55
                _, err := io.Copy(ctx.Stdout, ctx.Stdin)
 
56
                return err
 
57
        default:
 
58
                fmt.Fprintln(ctx.Stdout, c.Option)
 
59
        }
 
60
        return nil
 
61
}
 
62
 
 
63
// minimalHelp and fullHelp are the expected help strings for a TestCommand
 
64
// with Name "verb", with and without Minimal set.
 
65
var minimalHelp = "usage: verb\n"
 
66
var optionHelp = `usage: verb [options] <something>
 
67
purpose: verb the juju
 
68
 
 
69
options:
 
70
--option (= "")
 
71
    option-doc
 
72
`
 
73
var fullHelp = `usage: verb [options] <something>
 
74
purpose: verb the juju
 
75
 
 
76
options:
 
77
--option (= "")
 
78
    option-doc
 
79
 
 
80
verb-doc
 
81
`