Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 1257993

Browse files
committed
commands/commandstest: Add
1 parent c3c0726 commit 1257993

2 files changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package commandstest
2+
3+
import (
4+
"github.com/codegangsta/cli"
5+
)
6+
7+
type FakeFlagger struct {
8+
Data map[string]interface{}
9+
}
10+
11+
type FakeCommandLine struct {
12+
LocalFlags, GlobalFlags *FakeFlagger
13+
HelpShown, VersionShown bool
14+
CliArgs []string
15+
}
16+
17+
func (ff FakeFlagger) String(key string) string {
18+
if value, ok := ff.Data[key]; ok {
19+
return value.(string)
20+
}
21+
return ""
22+
}
23+
24+
func (ff FakeFlagger) StringSlice(key string) []string {
25+
if value, ok := ff.Data[key]; ok {
26+
return value.([]string)
27+
}
28+
return []string{}
29+
}
30+
31+
func (ff FakeFlagger) Int(key string) int {
32+
if value, ok := ff.Data[key]; ok {
33+
return value.(int)
34+
}
35+
return 0
36+
}
37+
38+
func (ff FakeFlagger) Bool(key string) bool {
39+
if value, ok := ff.Data[key]; ok {
40+
return value.(bool)
41+
}
42+
return false
43+
}
44+
45+
func (fcli *FakeCommandLine) IsSet(key string) bool {
46+
_, ok := fcli.LocalFlags.Data[key]
47+
return ok
48+
}
49+
50+
func (fcli *FakeCommandLine) String(key string) string {
51+
return fcli.LocalFlags.String(key)
52+
}
53+
54+
func (fcli *FakeCommandLine) StringSlice(key string) []string {
55+
return fcli.LocalFlags.StringSlice(key)
56+
}
57+
58+
func (fcli *FakeCommandLine) Int(key string) int {
59+
return fcli.LocalFlags.Int(key)
60+
}
61+
62+
func (fcli *FakeCommandLine) Bool(key string) bool {
63+
if fcli.LocalFlags == nil {
64+
return false
65+
}
66+
return fcli.LocalFlags.Bool(key)
67+
}
68+
69+
func (fcli *FakeCommandLine) GlobalString(key string) string {
70+
return fcli.GlobalFlags.String(key)
71+
}
72+
73+
func (fcli *FakeCommandLine) Generic(name string) interface{} {
74+
return fcli.LocalFlags.Data[name]
75+
}
76+
77+
func (fcli *FakeCommandLine) FlagNames() []string {
78+
flagNames := []string{}
79+
for key := range fcli.LocalFlags.Data {
80+
flagNames = append(flagNames, key)
81+
}
82+
83+
return flagNames
84+
}
85+
86+
func (fcli *FakeCommandLine) ShowHelp() {
87+
fcli.HelpShown = true
88+
}
89+
90+
func (fcli *FakeCommandLine) Application() *cli.App {
91+
return cli.NewApp()
92+
}
93+
94+
func (fcli *FakeCommandLine) Args() cli.Args {
95+
return fcli.CliArgs
96+
}
97+
98+
func (fcli *FakeCommandLine) ShowVersion() {
99+
fcli.VersionShown = true
100+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package commandstest
2+
3+
import (
4+
"bytes"
5+
"io"
6+
7+
"os"
8+
)
9+
10+
var (
11+
stdout *os.File
12+
)
13+
14+
func init() {
15+
stdout = os.Stdout
16+
}
17+
18+
type StdoutGetter interface {
19+
Output() string
20+
Stop()
21+
}
22+
23+
type stdoutCapturer struct {
24+
stdout *os.File
25+
output chan string
26+
}
27+
28+
func NewStdoutGetter() StdoutGetter {
29+
r, w, _ := os.Pipe()
30+
os.Stdout = w
31+
32+
output := make(chan string)
33+
go func() {
34+
var testOutput bytes.Buffer
35+
io.Copy(&testOutput, r)
36+
output <- testOutput.String()
37+
}()
38+
39+
return &stdoutCapturer{
40+
stdout: w,
41+
output: output,
42+
}
43+
}
44+
45+
func (c *stdoutCapturer) Output() string {
46+
c.stdout.Close()
47+
text := <-c.output
48+
close(c.output)
49+
return text
50+
}
51+
52+
func (c *stdoutCapturer) Stop() {
53+
os.Stdout = stdout
54+
}

0 commit comments

Comments
 (0)