summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/files_test.go
blob: 6dcdc7d4d80e72b90b5d7cf53a585617d81e9fa5 (plain)
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
package pkglint

import (
	"gopkg.in/check.v1"
)

func (s *Suite) Test_Load(c *check.C) {
	t := s.Init(c)

	nonexistent := t.File("nonexistent")
	empty := t.CreateFileLines("empty")
	oneLiner := t.CreateFileLines("one-liner",
		"hello, world")

	t.Check(Load(nonexistent, 0), check.IsNil)
	t.Check(Load(empty, 0).Lines, check.HasLen, 0)
	t.CheckEquals(Load(oneLiner, 0).Lines[0].Text, "hello, world")

	t.CheckOutputEmpty()

	t.Check(Load(nonexistent, LogErrors), check.IsNil)
	t.Check(Load(empty, LogErrors).Lines, check.HasLen, 0)
	t.CheckEquals(Load(oneLiner, LogErrors).Lines[0].Text, "hello, world")

	t.CheckOutputLines(
		"ERROR: ~/nonexistent: Cannot be read.")

	t.Check(Load(nonexistent, NotEmpty), check.IsNil)
	t.Check(Load(empty, NotEmpty), check.IsNil)
	t.CheckEquals(Load(oneLiner, NotEmpty).Lines[0].Text, "hello, world")

	t.CheckOutputEmpty()

	t.Check(Load(nonexistent, NotEmpty|LogErrors), check.IsNil)
	t.Check(Load(empty, NotEmpty|LogErrors), check.IsNil)
	t.CheckEquals(Load(oneLiner, NotEmpty|LogErrors).Lines[0].Text, "hello, world")

	t.CheckOutputLines(
		"ERROR: ~/nonexistent: Cannot be read.",
		"ERROR: ~/empty: Must not be empty.")

	t.ExpectFatal(
		func() { Load(t.File("does-not-exist"), MustSucceed) },
		"FATAL: ~/does-not-exist: Cannot be read.")

	t.ExpectFatal(
		func() { Load(t.File("empty"), MustSucceed|NotEmpty) },
		"FATAL: ~/empty: Must not be empty.")
}

// Up to 2019-12-04, pkglint suppressed fatal errors when it was started
// with the --autofix option. This was another case where the clear
// separation between diagnostics and technical errors had been confused.
func (s *Suite) Test_Load__not_found_in_autofix_mode(c *check.C) {
	t := s.Init(c)

	t.SetUpCommandLine("--autofix")
	t.Chdir(".")

	t.ExpectFatal(
		func() { Load("nonexistent", MustSucceed) },
		"FATAL: nonexistent: Cannot be read.")
}

func (s *Suite) Test_convertToLogicalLines__no_continuation(c *check.C) {
	t := s.Init(c)

	rawText := "" +
		"first line\n" +
		"second line\n"

	lines := convertToLogicalLines("filename", rawText, false)

	t.CheckEquals(lines.Len(), 2)
	t.CheckEquals(lines.Lines[0].String(), "filename:1: first line")
	t.CheckEquals(lines.Lines[1].String(), "filename:2: second line")
}

func (s *Suite) Test_convertToLogicalLines__continuation(c *check.C) {
	t := s.Init(c)

	rawText := "" +
		"first line, \\\n" +
		"still first line\n" +
		"second line\n"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Len(), 2)
	t.CheckEquals(lines.Lines[0].String(), "filename:1--2: first line, still first line")
	t.CheckEquals(lines.Lines[1].String(), "filename:3: second line")
}

// This test demonstrates that pkglint deviates from bmake.
// Bmake keeps all the trailing whitespace from the line and replaces the
// backslash plus any indentation with a single space. This results in:
//  "\tprintf '%s\\n' 'none none  space  space  tab\t tab'"
// This is another of the edge cases probably no-one relies on.
func (s *Suite) Test_convertToLogicalLines__continuation_spacing(c *check.C) {
	t := s.Init(c)

	rawText := "" +
		"\tprintf '%s\\n' 'none\\\n" +
		"none\\\n" +
		"space \\\n" +
		" space \\\n" +
		"tab\t\\\n" +
		"\ttab'\n"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Lines[0].Text,
		"\tprintf '%s\\n' 'none none space space tab tab'")
}

func (s *Suite) Test_convertToLogicalLines__continuation_in_last_line(c *check.C) {
	t := s.Init(c)

	rawText := "" +
		"last line\\\n"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Len(), 1)
	t.CheckEquals(lines.Lines[0].String(), "filename:1: last line\\")
	t.CheckOutputEmpty()
}

// In Makefiles, comment lines can also have continuations.
// See devel/bmake/files/unit-tests/comment.mk
func (s *Suite) Test_convertToLogicalLines__comments(c *check.C) {
	t := s.Init(c)

	mklines := t.SetUpFileMkLines("comment.mk",
		"# This is a comment",
		"",
		"#\\",
		"\tMultiline comment",
		"# Another escaped comment \\",
		"that \\",
		"goes \\",
		"on and on",
		"# This is NOT an escaped comment due to the double backslashes \\\\",
		"VAR=\tThis is not a comment",
		"",
		"#\\",
		"\tThis is a comment",
		"#\\\\",
		"\tThis is no comment",
		"#\\\\\\",
		"\tThis is a comment",
		"#\\\\\\\\",
		"\tThis is no comment",
		"#\\\\\\\\\\",
		"\tThis is a comment",
		"#\\\\\\\\\\\\",
		"\tThis is no comment")

	var texts []string
	for _, line := range mklines.lines.Lines {
		texts = append(texts, line.Text)
	}

	t.CheckDeepEquals(texts, []string{
		"# This is a comment",
		"",
		"# Multiline comment",
		"# Another escaped comment that goes on and on",
		"# This is NOT an escaped comment due to the double backslashes \\\\",
		"VAR=\tThis is not a comment",
		"",
		"# This is a comment",
		"#\\\\",
		"\tThis is no comment",
		"#\\\\ This is a comment",
		"#\\\\\\\\",
		"\tThis is no comment",
		"#\\\\\\\\ This is a comment",
		"#\\\\\\\\\\\\",
		"\tThis is no comment"})

	t.CheckOutputEmpty()
}

func (s *Suite) Test_convertToLogicalLines__missing_newline_at_eof(c *check.C) {
	t := s.Init(c)

	rawText := "" +
		"The package description\n" +
		"takes 2 lines"

	lines := convertToLogicalLines("DESCR", rawText, true)

	t.CheckEquals(lines.Len(), 2)
	t.CheckEquals(lines.Lines[1].String(), "DESCR:2: takes 2 lines")
	t.CheckOutputLines(
		"ERROR: DESCR:2: File must end with a newline.")
}

func (s *Suite) Test_convertToLogicalLines__missing_newline_at_eof_in_continuation_line(c *check.C) {
	t := s.Init(c)

	rawText := "" +
		"last line\\"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Len(), 1)
	t.CheckEquals(lines.Lines[0].String(), "filename:1: last line\\")
	t.CheckOutputLines(
		"ERROR: filename:1: File must end with a newline.")
}

func (s *Suite) Test_convertToLogicalLines__missing_newline_at_eof_with_source(c *check.C) {
	t := s.Init(c)

	t.SetUpCommandLine("-Wall", "--source")
	rawText := "" +
		"last line\\"

	lines := convertToLogicalLines("filename", rawText, true)

	t.CheckEquals(lines.Len(), 1)
	t.CheckEquals(lines.Lines[0].String(), "filename:1: last line\\")
	t.CheckOutputLines(
		">\tlast line\\",
		"ERROR: filename:1: File must end with a newline.")
}

func (s *Suite) Test_nextLogicalLine__commented_multi(c *check.C) {
	t := s.Init(c)

	mklines := t.NewMkLines("filename.mk",
		"#COMMENTED= \\",
		"#\tcontinuation 1 \\",
		"#\tcontinuation 2")
	mkline := mklines.mklines[0]

	// The leading comments are stripped from the continuation lines as well.
	t.CheckEquals(mkline.Value(), "continuation 1 \tcontinuation 2")
	t.CheckEquals(mkline.HasComment(), false)
}

func (s *Suite) Test_matchContinuationLine(c *check.C) {
	t := s.Init(c)

	leadingWhitespace, text, trailingWhitespace, continuation := matchContinuationLine("")

	t.CheckEquals(leadingWhitespace, "")
	t.CheckEquals(text, "")
	t.CheckEquals(trailingWhitespace, "")
	t.CheckEquals(continuation, "")

	leadingWhitespace, text, trailingWhitespace, continuation = matchContinuationLine("\tword   \\")

	t.CheckEquals(leadingWhitespace, "\t")
	t.CheckEquals(text, "word")
	t.CheckEquals(trailingWhitespace, "   ")
	t.CheckEquals(continuation, "\\")
}