summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/var_test.go
blob: a11fa4aa145385bb8baa16dfdb5a04345e06e773 (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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package pkglint

import "gopkg.in/check.v1"

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, "value")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME=\toverwritten"), false)

	t.Check(v.ConstantValue(), equals, "overwritten")
}

// Variables that reference other variable are considered constants.
// Even if these referenced variables change their value in-between,
// this does not affect the constant-ness of this variable, since the
// references are resolved lazily.
func (s *Suite) Test_Var_ConstantValue__assign_reference(c *check.C) {
	t := s.Init(c)

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, "value")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME=\t${OTHER}"), false)

	t.Check(v.Constant(), equals, true)
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, "value")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME:=\t${OTHER}"), false)

	// To analyze this case correctly, pkglint would have to know
	// the current value of ${OTHER} in line 124. For that it would
	// need the complete scope including all other variables.
	//
	// As of March 2019 this is not implemented, therefore pkglint
	// doesn't treat the variable as constant, to prevent wrong warnings.
	t.Check(v.Constant(), equals, false)
}

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

	v := NewVar("VARNAME")

	t.Check(v.ConditionalVars(), check.IsNil)

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME=\tconditional"), true, "OPSYS")

	t.Check(v.Constant(), equals, false)
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME?=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, "value")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME?=\tignored"), false)

	t.Check(v.ConstantValue(), equals, "value")
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("buildlink3.mk", 123, "VARNAME:=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, "value")

	v.Write(t.NewMkLine("builtin.mk", 124, "VARNAME?=\tignored"), false)

	t.Check(v.ConstantValue(), equals, "value")
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME+=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, " value")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME+=\tappended"), false)

	t.Check(v.ConstantValue(), equals, " value appended")
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME:=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, "value")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME:=\toverwritten"), false)

	t.Check(v.ConstantValue(), equals, "overwritten")
}

// Variables that are based on running shell commands are never constant.
func (s *Suite) Test_Var_ConstantValue__shell(c *check.C) {
	t := s.Init(c)

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, "value")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME!=\techo hello"), false)

	t.Check(v.Constant(), equals, false)
}

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

	v := NewVar("VARNAME")

	// Since the value of VARNAME escapes here, the value is not
	// guaranteed to be the same in all evaluations of ${VARNAME}.
	// For example, OTHER may be used at load time in an .if
	// condition.
	v.Read(t.NewMkLine("readwrite.mk", 123, "OTHER=\t${VARNAME}"))

	t.Check(v.Constant(), equals, false)

	v.Write(t.NewMkLine("readwrite.mk", 124, "VARNAME=\tvalue"), false)

	t.Check(v.Constant(), equals, false)
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("readwrite.mk", 123, "VARNAME=\tvalue"), false)

	t.Check(v.ConstantValue(), equals, "value")

	// Since the value of VARNAME escapes here, the value is not
	// guaranteed to be the same in all evaluations of ${VARNAME}.
	// For example, OTHER may be used at load time in an .if
	// condition.
	v.Read(t.NewMkLine("readwrite.mk", 124, "OTHER=\t${VARNAME}"))

	t.Check(v.ConstantValue(), equals, "value")

	v.Write(t.NewMkLine("write.mk", 125, "VARNAME=\toverwritten"), false)

	t.Check(v.Constant(), equals, false)
}

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

	v := NewVar("VARNAME")

	t.Check(v.Conditional(), equals, false)
	t.Check(v.ConditionalVars(), check.IsNil)

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME=\tconditional"), true, "OPSYS")

	t.Check(v.Constant(), equals, false)
	t.Check(v.Conditional(), equals, true)
	t.Check(v.ConditionalVars(), deepEquals, []string{"OPSYS"})

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME=\tconditional"), true, "OPSYS")

	t.Check(v.Conditional(), equals, true)
	t.Check(v.ConditionalVars(), deepEquals, []string{"OPSYS"})
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME:=\toverwritten conditionally"), true, "OPSYS")

	// Since there is no previous value, the simplest choice is to just
	// take the first seen value, no matter if that value is conditional
	// or not.
	t.Check(v.Conditional(), equals, true)
	t.Check(v.Constant(), equals, false)
	t.Check(v.Value(), equals, "overwritten conditionally")
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine("write.mk", 123, "VARNAME=\tvalue"), false)

	t.Check(v.Value(), equals, "value")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME+=\tappended"), false)

	t.Check(v.Value(), equals, "value appended")

	v.Write(t.NewMkLine("write.mk", 124, "VARNAME:=\toverwritten conditionally"), true, "OPSYS")

	// When there is a previous value, it's probably best to keep
	// that value since this way the following code results in the
	// most generic value:
	//  VAR=    generic
	//  .if ${OPSYS} == NetBSD
	//  VAR=    specific
	//  .endif
	// The value stays the same, still it is marked as conditional and therefore
	// not constant anymore.
	t.Check(v.Conditional(), equals, true)
	t.Check(v.Constant(), equals, false)
	t.Check(v.Value(), equals, "value appended")
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine(t.File("write.mk"), 123, "VARNAME=\tvalue"), false)

	t.Check(v.Value(), equals, "value")

	v.Write(t.NewMkLine(t.File("mk/write.mk"), 123, "VARNAME=\tinfra"), false)

	t.Check(v.Value(), equals, "value")

	v.Write(t.NewMkLine(t.File("wip/mk/write.mk"), 123, "VARNAME=\twip infra"), false)

	t.Check(v.Value(), equals, "value")
}

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

	v := NewVar("VARNAME")

	v.Write(t.NewMkLine(t.File("write.mk"), 123, "VARNAME=\tvalue"), false)

	t.Check(v.ValueInfra(), equals, "value")

	v.Write(t.NewMkLine(t.File("mk/write.mk"), 123, "VARNAME=\tinfra"), false)

	t.Check(v.ValueInfra(), equals, "infra")

	v.Write(t.NewMkLine(t.File("wip/mk/write.mk"), 123, "VARNAME=\twip infra"), false)

	t.Check(v.ValueInfra(), equals, "wip infra")
}

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

	v := NewVar("VAR")

	t.Check(v.ReadLocations(), check.IsNil)

	mkline123 := t.NewMkLine("read.mk", 123, "OTHER=\t${VAR}")
	v.Read(mkline123)

	t.Check(v.ReadLocations(), deepEquals, []MkLine{mkline123})

	mkline124 := t.NewMkLine("read.mk", 124, "OTHER=\t${VAR} ${VAR}")
	v.Read(mkline124)
	v.Read(mkline124)

	// For now, count every read of the variable. I'm not yet sure
	// whether that's the best way or whether to make the lines unique.
	t.Check(v.ReadLocations(), deepEquals, []MkLine{mkline123, mkline124, mkline124})
}

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

	v := NewVar("VAR")

	t.Check(v.WriteLocations(), check.IsNil)

	mkline123 := t.NewMkLine("write.mk", 123, "VAR=\tvalue")
	v.Write(mkline123, false)

	t.Check(v.WriteLocations(), deepEquals, []MkLine{mkline123})

	// Multiple writes from the same line may happen because of a .for loop.
	mkline125 := t.NewMkLine("write.mk", 125, "VAR+=\t${var}")
	v.Write(mkline125, false)
	v.Write(mkline125, false)

	// For now, count every write of the variable. I'm not yet sure
	// whether that's the best way or whether to make the lines unique.
	t.Check(v.WriteLocations(), deepEquals, []MkLine{mkline123, mkline125, mkline125})
}

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

	v := NewVar("VAR")

	t.Check(v.Refs(), check.IsNil)

	// The referenced variables are taken from the mkline.
	// They don't need to be passed separately.
	v.Write(t.NewMkLine("write.mk", 123, "VAR=${OTHER} ${${OPSYS} == NetBSD :? ${THEN} : ${ELSE}}"), true, "COND")

	v.AddRef("FOR")

	t.Check(v.Refs(), deepEquals, []string{"OTHER", "OPSYS", "THEN", "ELSE", "COND", "FOR"})
}