mock tty using interfaces to test more easily
This commit is contained in:
parent
7f5c794e5f
commit
22df491c67
|
@ -0,0 +1,5 @@
|
|||
package packer
|
||||
|
||||
type TTY interface {
|
||||
ReadString() (string, error)
|
||||
}
|
|
@ -82,7 +82,7 @@ type BasicUi struct {
|
|||
ErrorWriter io.Writer
|
||||
l sync.Mutex
|
||||
interrupted bool
|
||||
tty *tty.TTY
|
||||
tty TTY
|
||||
StackableProgressBar
|
||||
}
|
||||
|
||||
|
@ -235,7 +235,7 @@ func (rw *BasicUi) Ask(query string) (string, error) {
|
|||
log.Printf("ui: scan err: %s", err)
|
||||
return
|
||||
}
|
||||
result <- line
|
||||
result <- strings.TrimSpace(line)
|
||||
}()
|
||||
|
||||
select {
|
||||
|
|
|
@ -34,9 +34,18 @@ func testUi() *BasicUi {
|
|||
Reader: new(bytes.Buffer),
|
||||
Writer: new(bytes.Buffer),
|
||||
ErrorWriter: new(bytes.Buffer),
|
||||
tty: new(testTTY),
|
||||
}
|
||||
}
|
||||
|
||||
type testTTY struct {
|
||||
say string
|
||||
}
|
||||
|
||||
func (tttyy *testTTY) ReadString() (string, error) {
|
||||
return tttyy.say, nil
|
||||
}
|
||||
|
||||
func TestColoredUi(t *testing.T) {
|
||||
bufferUi := testUi()
|
||||
ui := &ColoredUi{UiColorYellow, UiColorRed, bufferUi}
|
||||
|
@ -217,7 +226,7 @@ func TestBasicUi_Ask(t *testing.T) {
|
|||
for _, testCase := range testCases {
|
||||
// Because of the internal bufio we can't easily reset the input, so create a new one each time
|
||||
bufferUi := testUi()
|
||||
writeReader(bufferUi, testCase.Input)
|
||||
bufferUi.tty = &testTTY{testCase.Input}
|
||||
|
||||
actual, err = bufferUi.Ask(testCase.Prompt)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue