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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
"gopkg.in/check.v1"
"netbsd.org/pkglint/textproc"
"netbsd.org/pkglint/trace"
)
var equals = check.Equals
var deepEquals = check.DeepEquals
const RcsId = "$" + "NetBSD$"
const MkRcsId = "# $" + "NetBSD$"
const PlistRcsId = "@comment $" + "NetBSD$"
type Suite struct {
Tester *Tester
}
// Init initializes the suite with the check.C instance for the actual
// test run. See https://github.com/go-check/check/issues/22
func (s *Suite) Init(c *check.C) *Tester {
t := s.Tester // Has been initialized by SetUpTest
if t.checkC != nil {
panic("Suite.Init must only be called once.")
}
t.checkC = c
return t
}
func (s *Suite) SetUpTest(c *check.C) {
t := &Tester{checkC: c}
s.Tester = t
G = GlobalVars{Testing: true}
textproc.Testing = true
G.logOut = NewSeparatorWriter(&t.stdout)
G.logErr = NewSeparatorWriter(&t.stderr)
trace.Out = &t.stdout
t.checkC = c
t.SetupCommandLine( /* no arguments */ )
t.checkC = nil
G.opts.LogVerbose = true // To detect duplicate work being done
}
func (s *Suite) TearDownTest(c *check.C) {
t := s.Tester
t.checkC = nil // No longer usable; see https://github.com/go-check/check/issues/22
G = GlobalVars{}
textproc.Testing = false
if out := t.Output(); out != "" {
fmt.Fprintf(os.Stderr, "Unchecked output in %q; check with: t.CheckOutputLines(%v)",
c.TestName(), strings.Split(out, "\n"))
}
t.tmpdir = ""
}
var _ = check.Suite(new(Suite))
func Test(t *testing.T) { check.TestingT(t) }
// Tester provides utility methods for testing pkglint.
// It is separated from the Suite since the latter contains
// all the test methods, which makes it difficult to find
// a method by auto-completion.
type Tester struct {
stdout bytes.Buffer
stderr bytes.Buffer
tmpdir string
checkC *check.C
}
func (t *Tester) c() *check.C {
if t.checkC == nil {
panic("Suite.Init must be called before accessing check.C.")
}
return t.checkC
}
// SetupCommandLine simulates a command line for the remainder of the test.
// See Pkglint.ParseCommandLine.
func (t *Tester) SetupCommandLine(args ...string) {
exitcode := new(Pkglint).ParseCommandLine(append([]string{"pkglint"}, args...))
if exitcode != nil && *exitcode != 0 {
t.CheckOutputEmpty()
t.c().Fatalf("Cannot parse command line: %#v", args)
}
G.opts.LogVerbose = true // See SetUpTest
}
func (t *Tester) SetupMasterSite(varname string, urls ...string) {
name2url := &G.globalData.MasterSiteVarToURL
url2name := &G.globalData.MasterSiteURLToVar
if *name2url == nil {
*name2url = make(map[string]string)
*url2name = make(map[string]string)
}
(*name2url)[varname] = urls[0]
for _, url := range urls {
(*url2name)[url] = varname
}
}
func (t *Tester) SetupTool(tool *Tool) {
reg := G.globalData.Tools
if len(reg.byName) == 0 && len(reg.byVarname) == 0 {
reg = NewToolRegistry()
G.globalData.Tools = reg
}
if tool.Name != "" {
reg.byName[tool.Name] = tool
}
if tool.Varname != "" {
reg.byVarname[tool.Varname] = tool
}
}
// SetupFileLines creates a temporary file and writes the given lines to it.
// The file is then read in, without considering line continuations.
func (t *Tester) SetupFileLines(relativeFilename string, lines ...string) []Line {
filename := t.CreateFileLines(relativeFilename, lines...)
return LoadExistingLines(filename, false)
}
// SetupFileLines creates a temporary file and writes the given lines to it.
// The file is then read in, handling line continuations for Makefiles.
func (t *Tester) SetupFileLinesContinuation(relativeFilename string, lines ...string) []Line {
filename := t.CreateFileLines(relativeFilename, lines...)
return LoadExistingLines(filename, true)
}
func (t *Tester) CreateFileLines(relativeFilename string, lines ...string) (filename string) {
content := ""
for _, line := range lines {
content += line + "\n"
}
filename = t.TempFilename(relativeFilename)
err := os.MkdirAll(path.Dir(filename), 0777)
t.c().Assert(err, check.IsNil)
err = ioutil.WriteFile(filename, []byte(content), 0666)
t.c().Check(err, check.IsNil)
return filename
}
func (t *Tester) LoadTmpFile(relFname string) (absFname string) {
bytes, err := ioutil.ReadFile(t.TmpDir() + "/" + relFname)
t.c().Assert(err, check.IsNil)
return string(bytes)
}
func (t *Tester) TmpDir() string {
if t.tmpdir == "" {
t.tmpdir = filepath.ToSlash(t.c().MkDir())
}
return t.tmpdir
}
// TempFilename returns the absolute path to the given file in the
// temporary directory. It doesn't check whether that file exists.
func (t *Tester) TempFilename(relativeFilename string) string {
return t.TmpDir() + "/" + relativeFilename
}
func (t *Tester) ExpectFatalError(action func()) {
if r := recover(); r != nil {
if _, ok := r.(pkglintFatal); ok {
action()
return
}
panic(r)
}
}
// Arguments are either (lineno, orignl) or (lineno, orignl, textnl).
func (t *Tester) NewRawLines(args ...interface{}) []*RawLine {
rawlines := make([]*RawLine, len(args)/2)
j := 0
for i := 0; i < len(args); i += 2 {
lineno := args[i].(int)
orignl := args[i+1].(string)
textnl := orignl
if i+2 < len(args) {
if s, ok := args[i+2].(string); ok {
textnl = s
i++
}
}
rawlines[j] = &RawLine{lineno, orignl, textnl}
j++
}
return rawlines[:j]
}
func (t *Tester) NewLine(filename string, lineno int, text string) Line {
textnl := text + "\n"
rawLine := RawLine{lineno, textnl, textnl}
return NewLine(filename, lineno, text, []*RawLine{&rawLine})
}
func (t *Tester) NewMkLine(fileName string, lineno int, text string) MkLine {
return NewMkLine(t.NewLine(fileName, lineno, text))
}
func (t *Tester) NewShellLine(fileName string, lineno int, text string) *ShellLine {
return NewShellLine(t.NewMkLine(fileName, lineno, text))
}
// NewLines generates a slice of simple lines,
// i.e. each logical line has exactly one physical line.
// To work with line continuations like in Makefiles,
// use CreateFileLines together with LoadExistingLines.
func (t *Tester) NewLines(fileName string, lines ...string) []Line {
return t.NewLinesAt(fileName, 1, lines...)
}
// NewLinesAt generates a slice of simple lines,
// i.e. each logical line has exactly one physical line.
// To work with line continuations like in Makefiles,
// use Suite.CreateFileLines together with Suite.LoadExistingLines.
func (t *Tester) NewLinesAt(fileName string, firstLine int, texts ...string) []Line {
result := make([]Line, len(texts))
for i, text := range texts {
textnl := text + "\n"
result[i] = NewLine(fileName, i+firstLine, text, t.NewRawLines(i+firstLine, textnl))
}
return result
}
func (t *Tester) NewMkLines(fileName string, lines ...string) *MkLines {
return NewMkLines(t.NewLines(fileName, lines...))
}
// Returns and consumes the output from both stdout and stderr.
// The temporary directory is replaced with a tilde (~).
func (t *Tester) Output() string {
stdout := t.stdout.String()
stderr := t.stderr.String()
t.stdout.Reset()
t.stderr.Reset()
output := stdout + stderr
if t.tmpdir != "" {
output = strings.Replace(output, t.tmpdir, "~", -1)
}
return output
}
func (t *Tester) CheckOutputEmpty() {
t.CheckOutputLines( /* none */ )
}
// CheckOutputLines checks that the output up to now equals the given lines.
// After the comparison, the output buffers are cleared so that later
// calls only check against the newly added output.
func (t *Tester) CheckOutputLines(expectedLines ...string) {
output := t.Output()
actualLines := strings.Split(output, "\n")
actualLines = actualLines[:len(actualLines)-1]
t.c().Check(emptyToNil(actualLines), deepEquals, emptyToNil(expectedLines))
}
// BeginDebugToStdout redirects all logging output to stdout instead of
// the buffer. This is useful when stepping through the code, especially
// in combination with SetupCommandLine("--debug").
func (t *Tester) BeginDebugToStdout() {
G.logOut = NewSeparatorWriter(os.Stdout)
trace.Out = os.Stdout
trace.Tracing = true
}
// EndDebugToStdout logs the output to the buffers again, ready to be
// checked with CheckOutputLines.
func (t *Tester) EndDebugToStdout() {
G.logOut = NewSeparatorWriter(&t.stdout)
trace.Out = &t.stdout
trace.Tracing = false
}
// CheckFileLines loads the lines from the temporary file and checks that
// they equal the given lines.
func (t *Tester) CheckFileLines(relativeFileName string, lines ...string) {
text := t.LoadTmpFile(relativeFileName)
actualLines := strings.Split(text, "\n")
actualLines = actualLines[:len(actualLines)-1]
t.c().Check(emptyToNil(actualLines), deepEquals, emptyToNil(lines))
}
// CheckFileLinesDetab loads the lines from the temporary file and checks
// that they equal the given lines. The loaded file may use tabs or spaces
// for indentation, while the lines in the code use spaces exclusively,
// in order to make the depth of the indentation clearly visible.
func (t *Tester) CheckFileLinesDetab(relativeFileName string, lines ...string) {
actualLines, err := readLines(t.TempFilename(relativeFileName), false)
if !t.c().Check(err, check.IsNil) {
return
}
var detabbed []string
for _, line := range actualLines {
rawText := strings.TrimRight(detab(line.raw[0].orignl), "\n")
detabbed = append(detabbed, rawText)
}
t.c().Check(detabbed, deepEquals, lines)
}
|