-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathprogress_test.go
150 lines (121 loc) · 3.45 KB
/
progress_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package progress
import (
"fmt"
"io/ioutil"
"os"
"testing"
. "gopkg.in/check.v1"
)
// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }
type ProgressTestSuite struct {
attachedToTerminalReturn bool
originalAttachedToTerminal func() bool
}
var _ = Suite(&ProgressTestSuite{})
func (ts *ProgressTestSuite) MockAttachedToTerminal() bool {
return ts.attachedToTerminalReturn
}
func (ts *ProgressTestSuite) TestSpin(c *C) {
f, err := ioutil.TempFile("", "progress-")
c.Assert(err, IsNil)
defer os.Remove(f.Name())
oldStdout := os.Stdout
os.Stdout = f
t := NewTextProgress()
for i := 0; i < 6; i++ {
t.Spin("msg")
}
os.Stdout = oldStdout
f.Sync()
f.Seek(0, 0)
progress, err := ioutil.ReadAll(f)
c.Assert(err, IsNil)
c.Assert(string(progress), Equals, "\r[|] msg\x1b[K\r[/] msg\x1b[K\r[-] msg\x1b[K\r[\\] msg\x1b[K\r[|] msg\x1b[K\r[/] msg\x1b[K")
}
func (ts *ProgressTestSuite) testAgreed(answer string, value bool, c *C) {
fout, err := ioutil.TempFile("", "progress-out-")
c.Assert(err, IsNil)
oldStdout := os.Stdout
os.Stdout = fout
defer func() {
os.Stdout = oldStdout
os.Remove(fout.Name())
fout.Close()
}()
fin, err := ioutil.TempFile("", "progress-in-")
c.Assert(err, IsNil)
oldStdin := os.Stdin
os.Stdin = fin
defer func() {
os.Stdin = oldStdin
os.Remove(fin.Name())
fin.Close()
}()
_, err = fmt.Fprintln(fin, answer)
c.Assert(err, IsNil)
_, err = fin.Seek(0, 0)
c.Assert(err, IsNil)
license := "Void where empty."
t := NewTextProgress()
c.Check(t.Agreed("blah blah", license), Equals, value)
_, err = fout.Seek(0, 0)
c.Assert(err, IsNil)
out, err := ioutil.ReadAll(fout)
c.Assert(err, IsNil)
c.Check(string(out), Equals, "blah blah\n"+license+"\nDo you agree? [y/n] ")
}
func (ts *ProgressTestSuite) TestAgreed(c *C) {
ts.testAgreed("Y", true, c)
ts.testAgreed("N", false, c)
}
func (ts *ProgressTestSuite) TestNotify(c *C) {
fout, err := ioutil.TempFile("", "notify-out-")
c.Assert(err, IsNil)
oldStdout := os.Stdout
os.Stdout = fout
defer func() {
os.Stdout = oldStdout
os.Remove(fout.Name())
fout.Close()
}()
t := NewTextProgress()
t.Notify("blah blah")
_, err = fout.Seek(0, 0)
c.Assert(err, IsNil)
out, err := ioutil.ReadAll(fout)
c.Assert(err, IsNil)
c.Check(string(out), Equals, fmt.Sprintf("\rblah blah%s\n", clearUntilEOL))
}
func (ts *ProgressTestSuite) TestMakeProgressBar(c *C) {
var pbar Meter
ts.originalAttachedToTerminal = attachedToTerminal
attachedToTerminal = ts.MockAttachedToTerminal
defer func() {
// reset
attachedToTerminal = ts.originalAttachedToTerminal
}()
ts.attachedToTerminalReturn = true
pbar = MakeProgressBar()
c.Assert(pbar, FitsTypeOf, NewTextProgress())
ts.attachedToTerminalReturn = false
pbar = MakeProgressBar()
c.Assert(pbar, FitsTypeOf, &NullProgress{})
}