diff options
Diffstat (limited to 'vendor/github.com/mitchellh/cli/ui_mock.go')
-rw-r--r-- | vendor/github.com/mitchellh/cli/ui_mock.go | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/vendor/github.com/mitchellh/cli/ui_mock.go b/vendor/github.com/mitchellh/cli/ui_mock.go new file mode 100644 index 0000000..0bfe0a1 --- /dev/null +++ b/vendor/github.com/mitchellh/cli/ui_mock.go | |||
@@ -0,0 +1,111 @@ | |||
1 | package cli | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "fmt" | ||
6 | "io" | ||
7 | "sync" | ||
8 | ) | ||
9 | |||
10 | // NewMockUi returns a fully initialized MockUi instance | ||
11 | // which is safe for concurrent use. | ||
12 | func NewMockUi() *MockUi { | ||
13 | m := new(MockUi) | ||
14 | m.once.Do(m.init) | ||
15 | return m | ||
16 | } | ||
17 | |||
18 | // MockUi is a mock UI that is used for tests and is exported publicly | ||
19 | // for use in external tests if needed as well. Do not instantite this | ||
20 | // directly since the buffers will be initialized on the first write. If | ||
21 | // there is no write then you will get a nil panic. Please use the | ||
22 | // NewMockUi() constructor function instead. You can fix your code with | ||
23 | // | ||
24 | // sed -i -e 's/new(cli.MockUi)/cli.NewMockUi()/g' *_test.go | ||
25 | type MockUi struct { | ||
26 | InputReader io.Reader | ||
27 | ErrorWriter *syncBuffer | ||
28 | OutputWriter *syncBuffer | ||
29 | |||
30 | once sync.Once | ||
31 | } | ||
32 | |||
33 | func (u *MockUi) Ask(query string) (string, error) { | ||
34 | u.once.Do(u.init) | ||
35 | |||
36 | var result string | ||
37 | fmt.Fprint(u.OutputWriter, query) | ||
38 | if _, err := fmt.Fscanln(u.InputReader, &result); err != nil { | ||
39 | return "", err | ||
40 | } | ||
41 | |||
42 | return result, nil | ||
43 | } | ||
44 | |||
45 | func (u *MockUi) AskSecret(query string) (string, error) { | ||
46 | return u.Ask(query) | ||
47 | } | ||
48 | |||
49 | func (u *MockUi) Error(message string) { | ||
50 | u.once.Do(u.init) | ||
51 | |||
52 | fmt.Fprint(u.ErrorWriter, message) | ||
53 | fmt.Fprint(u.ErrorWriter, "\n") | ||
54 | } | ||
55 | |||
56 | func (u *MockUi) Info(message string) { | ||
57 | u.Output(message) | ||
58 | } | ||
59 | |||
60 | func (u *MockUi) Output(message string) { | ||
61 | u.once.Do(u.init) | ||
62 | |||
63 | fmt.Fprint(u.OutputWriter, message) | ||
64 | fmt.Fprint(u.OutputWriter, "\n") | ||
65 | } | ||
66 | |||
67 | func (u *MockUi) Warn(message string) { | ||
68 | u.once.Do(u.init) | ||
69 | |||
70 | fmt.Fprint(u.ErrorWriter, message) | ||
71 | fmt.Fprint(u.ErrorWriter, "\n") | ||
72 | } | ||
73 | |||
74 | func (u *MockUi) init() { | ||
75 | u.ErrorWriter = new(syncBuffer) | ||
76 | u.OutputWriter = new(syncBuffer) | ||
77 | } | ||
78 | |||
79 | type syncBuffer struct { | ||
80 | sync.RWMutex | ||
81 | b bytes.Buffer | ||
82 | } | ||
83 | |||
84 | func (b *syncBuffer) Write(data []byte) (int, error) { | ||
85 | b.Lock() | ||
86 | defer b.Unlock() | ||
87 | return b.b.Write(data) | ||
88 | } | ||
89 | |||
90 | func (b *syncBuffer) Read(data []byte) (int, error) { | ||
91 | b.RLock() | ||
92 | defer b.RUnlock() | ||
93 | return b.b.Read(data) | ||
94 | } | ||
95 | |||
96 | func (b *syncBuffer) Reset() { | ||
97 | b.Lock() | ||
98 | b.b.Reset() | ||
99 | b.Unlock() | ||
100 | } | ||
101 | |||
102 | func (b *syncBuffer) String() string { | ||
103 | return string(b.Bytes()) | ||
104 | } | ||
105 | |||
106 | func (b *syncBuffer) Bytes() []byte { | ||
107 | b.RLock() | ||
108 | data := b.b.Bytes() | ||
109 | b.RUnlock() | ||
110 | return data | ||
111 | } | ||