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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
|
package pkglint
import "gopkg.in/check.v1"
// In a single file, five variables get a default value and are later overridden
// with the same value using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_default(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT?=\tvalue",
"ASSIGN?=\tvalue",
"APPEND?=\tvalue",
"EVAL?=\tvalue",
"SHELL?=\tvalue",
"",
"DEFAULT?=\tvalue",
"ASSIGN=\tvalue",
"APPEND+=\tvalue",
"EVAL:=\tvalue",
"SHELL!=\tvalue")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"NOTE: file.mk:8: Definition of ASSIGN is redundant because of line 2.",
"WARN: file.mk:4: Variable EVAL is overwritten in line 10.")
// TODO: "5: is overwritten later"
}
// In a single file, five variables get assigned are value and are later overridden
// with the same value using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_assign(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT=\tvalue",
"ASSIGN=\tvalue",
"APPEND=\tvalue",
"EVAL=\tvalue",
"SHELL=\tvalue",
"",
"DEFAULT?=\tvalue",
"ASSIGN=\tvalue",
"APPEND+=\tvalue",
"EVAL:=\tvalue",
"SHELL!=\tvalue")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"NOTE: file.mk:8: Definition of ASSIGN is redundant because of line 2.",
"WARN: file.mk:4: Variable EVAL is overwritten in line 10.")
// TODO: "5: is overwritten later"
}
// In a single file, five variables get appended a value and are later overridden
// with the same value using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_append(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT+=\tvalue",
"ASSIGN+=\tvalue",
"APPEND+=\tvalue",
"EVAL+=\tvalue",
"SHELL+=\tvalue",
"",
"DEFAULT?=\tvalue",
"ASSIGN=\tvalue",
"APPEND+=\tvalue",
"EVAL:=\tvalue",
"SHELL!=\tvalue")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"WARN: file.mk:2: Variable ASSIGN is overwritten in line 8.",
"WARN: file.mk:4: Variable EVAL is overwritten in line 10.")
// TODO: "5: is overwritten later"
}
// In a single file, five variables get assigned a value using the := operator,
// which in this simple case is equivalent to the = operator. The variables are
// later overridden with the same value using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_eval(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT:=\tvalue",
"ASSIGN:=\tvalue",
"APPEND:=\tvalue",
"EVAL:=\tvalue",
"SHELL:=\tvalue",
"",
"DEFAULT?=\tvalue",
"ASSIGN=\tvalue",
"APPEND+=\tvalue",
"EVAL:=\tvalue",
"SHELL!=\tvalue")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"NOTE: file.mk:8: Definition of ASSIGN is redundant because of line 2.",
"WARN: file.mk:4: Variable EVAL is overwritten in line 10.")
// TODO: "5: is overwritten later"
}
// In a single file, five variables get assigned a value using the != operator,
// which runs a shell command. As of March 2019 pkglint doesn't try to evaluate
// the shell commands, therefore the variable values are unknown. The variables
// are later overridden using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_shell(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT!=\tvalue",
"ASSIGN!=\tvalue",
"APPEND!=\tvalue",
"EVAL!=\tvalue",
"SHELL!=\tvalue",
"",
"DEFAULT?=\tvalue",
"ASSIGN=\tvalue",
"APPEND+=\tvalue",
"EVAL:=\tvalue",
"SHELL!=\tvalue")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"WARN: file.mk:2: Variable ASSIGN is overwritten in line 8.",
"WARN: file.mk:4: Variable EVAL is overwritten in line 10.")
// TODO: "5: is overwritten later"
}
// In a single file, five variables get a default value and are later overridden
// with the same value using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_default_ref(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT?=\t${OTHER}",
"ASSIGN?=\t${OTHER}",
"APPEND?=\t${OTHER}",
"EVAL?=\t${OTHER}",
"SHELL?=\t${OTHER}",
"",
"DEFAULT?=\t${OTHER}",
"ASSIGN=\t${OTHER}",
"APPEND+=\t${OTHER}",
"EVAL:=\t${OTHER}",
"SHELL!=\t${OTHER}")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"NOTE: file.mk:8: Definition of ASSIGN is redundant because of line 2.")
// TODO: "4: is overwritten later",
// TODO: "5: is overwritten later"
}
// In a single file, five variables get assigned are value and are later overridden
// with the same value using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_assign_ref(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT=\t${OTHER}",
"ASSIGN=\t${OTHER}",
"APPEND=\t${OTHER}",
"EVAL=\t${OTHER}",
"SHELL=\t${OTHER}",
"",
"DEFAULT?=\t${OTHER}",
"ASSIGN=\t${OTHER}",
"APPEND+=\t${OTHER}",
"EVAL:=\t${OTHER}",
"SHELL!=\t${OTHER}")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"NOTE: file.mk:8: Definition of ASSIGN is redundant because of line 2.")
// TODO: "4: is overwritten later",
// TODO: "5: is overwritten later"
}
// In a single file, five variables get appended a value and are later overridden
// with the same value using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_append_ref(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT+=\t${OTHER}",
"ASSIGN+=\t${OTHER}",
"APPEND+=\t${OTHER}",
"EVAL+=\t${OTHER}",
"SHELL+=\t${OTHER}",
"",
"DEFAULT?=\t${OTHER}",
"ASSIGN=\t${OTHER}",
"APPEND+=\t${OTHER}",
"EVAL:=\t${OTHER}",
"SHELL!=\t${OTHER}")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"WARN: file.mk:2: Variable ASSIGN is overwritten in line 8.")
// TODO: "4: is overwritten later",
// TODO: "5: is overwritten later"
}
// In a single file, five variables get assigned a value using the := operator,
// which in this simple case is equivalent to the = operator. The variables are
// later overridden with the same value using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_eval_ref(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT:=\t${OTHER}",
"ASSIGN:=\t${OTHER}",
"APPEND:=\t${OTHER}",
"EVAL:=\t${OTHER}",
"SHELL:=\t${OTHER}",
"",
"DEFAULT?=\t${OTHER}",
"ASSIGN=\t${OTHER}",
"APPEND+=\t${OTHER}",
"EVAL:=\t${OTHER}",
"SHELL!=\t${OTHER}")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"NOTE: file.mk:8: Definition of ASSIGN is redundant because of line 2.")
// TODO: "4: is overwritten later",
// TODO: "5: is overwritten later"
}
// In a single file, five variables get assigned a value using the != operator,
// which runs a shell command. As of March 2019 pkglint doesn't try to evaluate
// the shell commands, therefore the variable values are unknown. The variables
// are later overridden using the five different assignments operators.
func (s *Suite) Test_RedundantScope__single_file_shell_ref(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("file.mk",
"DEFAULT!=\t${OTHER}",
"ASSIGN!=\t${OTHER}",
"APPEND!=\t${OTHER}",
"EVAL!=\t${OTHER}",
"SHELL!=\t${OTHER}",
"",
"DEFAULT?=\t${OTHER}",
"ASSIGN=\t${OTHER}",
"APPEND+=\t${OTHER}",
"EVAL:=\t${OTHER}",
"SHELL!=\t${OTHER}")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: file.mk:7: Default assignment of DEFAULT has no effect because of line 1.",
"WARN: file.mk:2: Variable ASSIGN is overwritten in line 8.")
// TODO: "4: is overwritten later",
// TODO: "5: is overwritten later"
}
func (s *Suite) Test_RedundantScope__after_including_same_value(c *check.C) {
t := s.Init(c)
// Only test the ?=, = and += operators since the others are ignored,
// as of March 2019.
include, get := t.SetUpHierarchy()
include("including.mk",
include("included.mk",
"VAR.def.def?= ${OTHER}",
"VAR.def.asg?= ${OTHER}",
"VAR.def.app?= ${OTHER}",
"VAR.asg.def= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app= ${OTHER}",
"VAR.app.def+= ${OTHER}",
"VAR.app.asg+= ${OTHER}",
"VAR.app.app+= ${OTHER}"),
"VAR.def.def?= ${OTHER}",
"VAR.def.asg= ${OTHER}",
"VAR.def.app+= ${OTHER}",
"VAR.asg.def?= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app+= ${OTHER}",
"VAR.app.def?= ${OTHER}",
"VAR.app.asg= ${OTHER}",
"VAR.app.app+= ${OTHER}")
mklines := get("including.mk")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: including.mk:2: Default assignment of VAR.def.def has no effect because of included.mk:1.",
"NOTE: including.mk:3: Definition of VAR.def.asg is redundant because of included.mk:2.",
// VAR.def.app defines a default value and then appends to it. This is a common pattern.
// Appending the same value feels redundant but probably doesn't happen in practice.
// If it does, there should be a note for it.
"NOTE: including.mk:5: Default assignment of VAR.asg.def has no effect because of included.mk:4.",
"NOTE: including.mk:6: Definition of VAR.asg.asg is redundant because of included.mk:5.",
// VAR.asg.app defines a variable and later appends to it. This is a common pattern.
// Appending the same value feels redundant but probably doesn't happen in practice.
// If it does, there should be a note for it.
"NOTE: including.mk:8: Default assignment of VAR.app.def has no effect because of included.mk:7.",
// VAR.app.asg first appends and then overwrites. This might be a mistake.
// TODO: Find out whether this case happens in actual pkgsrc and if it's accidental.
// VAR.app.app first appends and then appends one more. This is a common pattern.
)
}
func (s *Suite) Test_RedundantScope__after_including_different_value(c *check.C) {
t := s.Init(c)
// Only test the ?=, = and += operators since the others are ignored,
// as of March 2019.
include, get := t.SetUpHierarchy()
include("including.mk",
include("included.mk",
"VAR.def.def?= ${VALUE}",
"VAR.def.asg?= ${VALUE}",
"VAR.def.app?= ${VALUE}",
"VAR.asg.def= ${VALUE}",
"VAR.asg.asg= ${VALUE}",
"VAR.asg.app= ${VALUE}",
"VAR.app.def+= ${VALUE}",
"VAR.app.asg+= ${VALUE}",
"VAR.app.app+= ${VALUE}"),
"VAR.def.def?= ${OTHER}",
"VAR.def.asg= ${OTHER}",
"VAR.def.app+= ${OTHER}",
"VAR.asg.def?= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app+= ${OTHER}",
"VAR.app.def?= ${OTHER}",
"VAR.app.asg= ${OTHER}",
"VAR.app.app+= ${OTHER}")
mklines := get("including.mk")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: including.mk:2: Default assignment of VAR.def.def has no effect because of included.mk:1.",
"NOTE: including.mk:5: Default assignment of VAR.asg.def has no effect because of included.mk:4.",
"NOTE: including.mk:8: Default assignment of VAR.app.def has no effect because of included.mk:7.")
}
func (s *Suite) Test_RedundantScope__before_including_same_value(c *check.C) {
t := s.Init(c)
// Only test the ?=, = and += operators since the others are ignored,
// as of March 2019.
include, get := t.SetUpHierarchy()
include("including.mk",
"VAR.def.def?= ${OTHER}",
"VAR.def.asg?= ${OTHER}",
"VAR.def.app?= ${OTHER}",
"VAR.asg.def= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app= ${OTHER}",
"VAR.app.def+= ${OTHER}",
"VAR.app.asg+= ${OTHER}",
"VAR.app.app+= ${OTHER}",
include("included.mk",
"VAR.def.def?= ${OTHER}",
"VAR.def.asg= ${OTHER}",
"VAR.def.app+= ${OTHER}",
"VAR.asg.def?= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app+= ${OTHER}",
"VAR.app.def?= ${OTHER}",
"VAR.app.asg= ${OTHER}",
"VAR.app.app+= ${OTHER}"))
mklines := get("including.mk")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: including.mk:1: Default assignment of VAR.def.def has no effect because of included.mk:1.",
"NOTE: including.mk:2: Default assignment of VAR.def.asg has no effect because of included.mk:2.",
"NOTE: including.mk:4: Definition of VAR.asg.def is redundant because of included.mk:4.",
"NOTE: including.mk:5: Definition of VAR.asg.asg is redundant because of included.mk:5.",
"WARN: including.mk:8: Variable VAR.app.asg is overwritten in included.mk:8.")
}
func (s *Suite) Test_RedundantScope__before_including_different_value(c *check.C) {
t := s.Init(c)
// Only test the ?=, = and += operators since the others are ignored,
// as of March 2019.
include, get := t.SetUpHierarchy()
include("including.mk",
"VAR.def.def?= ${VALUE}",
"VAR.def.asg?= ${VALUE}",
"VAR.def.app?= ${VALUE}",
"VAR.asg.def= ${VALUE}",
"VAR.asg.asg= ${VALUE}",
"VAR.asg.app= ${VALUE}",
"VAR.app.def+= ${VALUE}",
"VAR.app.asg+= ${VALUE}",
"VAR.app.app+= ${VALUE}",
include("included.mk",
"VAR.def.def?= ${OTHER}",
"VAR.def.asg= ${OTHER}",
"VAR.def.app+= ${OTHER}",
"VAR.asg.def?= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app+= ${OTHER}",
"VAR.app.def?= ${OTHER}",
"VAR.app.asg= ${OTHER}",
"VAR.app.app+= ${OTHER}"))
mklines := get("including.mk")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"WARN: including.mk:2: Variable VAR.def.asg is overwritten in included.mk:2.",
"WARN: including.mk:5: Variable VAR.asg.asg is overwritten in included.mk:5.",
"WARN: including.mk:8: Variable VAR.app.asg is overwritten in included.mk:8.")
}
func (s *Suite) Test_RedundantScope__independent_same_value(c *check.C) {
t := s.Init(c)
// Only test the ?=, = and += operators since the others are ignored,
// as of March 2019.
include, get := t.SetUpHierarchy()
include("including.mk",
include("included1.mk",
"VAR.def.def?= ${OTHER}",
"VAR.def.asg?= ${OTHER}",
"VAR.def.app?= ${OTHER}",
"VAR.asg.def= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app= ${OTHER}",
"VAR.app.def+= ${OTHER}",
"VAR.app.asg+= ${OTHER}",
"VAR.app.app+= ${OTHER}"),
include("included2.mk",
"VAR.def.def?= ${OTHER}",
"VAR.def.asg= ${OTHER}",
"VAR.def.app+= ${OTHER}",
"VAR.asg.def?= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app+= ${OTHER}",
"VAR.app.def?= ${OTHER}",
"VAR.app.asg= ${OTHER}",
"VAR.app.app+= ${OTHER}"))
mklines := get("including.mk")
NewRedundantScope().Check(mklines)
// Since the two included files are independent, there cannot be any
// redundancies between them. These redundancies can only be discovered
// when one of them includes the other.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__independent_different_value(c *check.C) {
t := s.Init(c)
// Only test the ?=, = and += operators since the others are ignored,
// as of March 2019.
include, get := t.SetUpHierarchy()
include("including.mk",
include("included1.mk",
"VAR.def.def?= ${VALUE}",
"VAR.def.asg?= ${VALUE}",
"VAR.def.app?= ${VALUE}",
"VAR.asg.def= ${VALUE}",
"VAR.asg.asg= ${VALUE}",
"VAR.asg.app= ${VALUE}",
"VAR.app.def+= ${VALUE}",
"VAR.app.asg+= ${VALUE}",
"VAR.app.app+= ${VALUE}"),
include("included2.mk",
"VAR.def.def?= ${OTHER}",
"VAR.def.asg= ${OTHER}",
"VAR.def.app+= ${OTHER}",
"VAR.asg.def?= ${OTHER}",
"VAR.asg.asg= ${OTHER}",
"VAR.asg.app+= ${OTHER}",
"VAR.app.def?= ${OTHER}",
"VAR.app.asg= ${OTHER}",
"VAR.app.app+= ${OTHER}"))
mklines := get("including.mk")
NewRedundantScope().Check(mklines)
// Since the two included files are independent, there cannot be any
// redundancies between them. Redundancies can only be discovered
// when one of them includes the other.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__file_hierarchy(c *check.C) {
t := s.Init(c)
include, get := t.SetUpHierarchy()
include("including.mk",
include("other.mk",
"VAR= other"),
include("module.mk",
"VAR= module",
include("version.mk",
"VAR= version"),
include("env.mk",
"VAR= env")))
NewRedundantScope().Check(get("including.mk"))
// No output since the included files are independent.
t.CheckOutputEmpty()
NewRedundantScope().Check(get("other.mk"))
// No output since the file by itself in neither redundant nor
// does it include any other file.
t.CheckOutputEmpty()
NewRedundantScope().Check(get("module.mk"))
// No warning about env.mk because it is independent from version.mk.
// Pkglint only produces warnings when it is very sure that the variable
// definition is really redundant in all cases.
//
// One reason to not warn is that at the point where env.mk is evaluated,
// version.mk had last written to the variable. Since version.mk is
// independent from env.mk, there is nothing redundant here.
// Pkglint doesn't do this, but it could.
//
// Another reason not to warn is that all locations where the variable has
// ever been accessed are saved. And if the current location neither includes
// all of the others nor is included by all of the others, there is at least
// one access that is in an unrelated file. This is what pkglint does.
t.CheckOutputLines(
"WARN: module.mk:1: Variable VAR is overwritten in version.mk:1.")
}
// FIXME: Continue the systematic redundancy tests.
//
// A test where the operators = and += define a variable that afterwards
// is assigned the same value using the ?= operator.
//
// Tests where the variables refer to other variables. These variables may
// be read and written between the relevant assignments.
//
// Tests where the variables are defined conditionally using .if, .else, .endif.
//
// Tests where the variables are defined in a .for loop that might not be
// evaluated at all.
//
// Tests where files are included conditionally and additionally have conditional
// sections, arbitrarily nested.
//
// Tests that show how to suppress the notes about redundant assignments
// and overwritten variables. The explanation must be helpful.
//
// Tests for dynamic variable assignments. For example BUILD_DIRS.NetBSD may
// be modified by any assignment of the form BUILD_DIRS.${var} or even ${var}.
// Without further analysis, pkglint cannot report redundancy warnings for any
// package that uses such variable assignments.
func (s *Suite) Test_RedundantScope__override_after_including(c *check.C) {
t := s.Init(c)
t.CreateFileLines("included.mk",
"OVERRIDE=\tprevious value",
"REDUNDANT=\tredundant")
t.CreateFileLines("including.mk",
".include \"included.mk\"",
"OVERRIDE=\toverridden value",
"REDUNDANT=\tredundant")
t.Chdir(".")
mklines := t.LoadMkInclude("including.mk")
// XXX: The warnings from here are not in the same order as the other warnings.
// XXX: There may be some warnings for the same file separated by warnings for other files.
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: including.mk:3: Definition of REDUNDANT is redundant because of included.mk:2.")
}
func (s *Suite) Test_RedundantScope__redundant_assign_after_including(c *check.C) {
t := s.Init(c)
t.CreateFileLines("included.mk",
"REDUNDANT=\tredundant")
t.CreateFileLines("including.mk",
".include \"included.mk\"",
"REDUNDANT=\tredundant")
t.Chdir(".")
mklines := t.LoadMkInclude("including.mk")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: including.mk:2: Definition of REDUNDANT is redundant because of included.mk:1.")
}
func (s *Suite) Test_RedundantScope__override_in_Makefile_after_including(c *check.C) {
t := s.Init(c)
t.CreateFileLines("module.mk",
"VAR=\tvalue ${OTHER}",
"VAR?=\tvalue ${OTHER}",
"VAR=\tnew value")
t.CreateFileLines("Makefile",
".include \"module.mk\"",
"VAR=\tthe package may overwrite variables from other files")
t.Chdir(".")
mklines := t.LoadMkInclude("Makefile")
// XXX: The warnings from here are not in the same order as the other warnings.
// XXX: There may be some warnings for the same file separated by warnings for other files.
NewRedundantScope().Check(mklines)
// No warning for VAR=... in Makefile since it makes sense to have common files
// with default values for variables, overriding some of them in each package.
t.CheckOutputLines(
"NOTE: module.mk:2: Default assignment of VAR has no effect because of line 1.",
"WARN: module.mk:2: Variable VAR is overwritten in line 3.")
}
func (s *Suite) Test_RedundantScope__default_value_definitely_unused(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"VAR=\tvalue ${OTHER}",
"VAR?=\tdifferent value")
NewRedundantScope().Check(mklines)
// A default assignment after an unconditional assignment is redundant.
// Even more so when the variable is not used between the two assignments.
t.CheckOutputLines(
"NOTE: module.mk:2: Default assignment of VAR has no effect because of line 1.")
}
func (s *Suite) Test_RedundantScope__default_value_overridden(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"VAR?=\tdefault value",
"VAR=\toverridden value")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"WARN: module.mk:1: Variable VAR is overwritten in line 2.")
}
func (s *Suite) Test_RedundantScope__overwrite_same_value(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"VAR=\tvalue ${OTHER}",
"VAR=\tvalue ${OTHER}")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: module.mk:2: Definition of VAR is redundant because of line 1.")
}
func (s *Suite) Test_RedundantScope__conditional_overwrite(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"VAR=\tdefault",
".if ${OPSYS} == NetBSD",
"VAR=\topsys",
".endif")
NewRedundantScope().Check(mklines)
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__overwrite_inside_conditional(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"VAR=\tgeneric",
".if ${OPSYS} == NetBSD",
"VAR=\tignored",
"VAR=\toverwritten",
".endif")
NewRedundantScope().Check(mklines)
// TODO: expected a warning "WARN: module.mk:4: line 3 is ignored"
// Since line 3 and line 4 are in the same basic block, line 3 is definitely ignored.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__conditionally_include(c *check.C) {
t := s.Init(c)
t.CreateFileLines("module.mk",
"VAR=\tgeneric",
".if ${OPSYS} == NetBSD",
". include \"included.mk\"",
".endif")
t.CreateFileLines("included.mk",
"VAR=\tignored",
"VAR=\toverwritten")
mklines := t.LoadMkInclude("module.mk")
NewRedundantScope().Check(mklines)
// TODO: expected a warning "WARN: module.mk:4: line 3 is ignored"
// Since line 3 and line 4 are in the same basic block, line 3 is definitely ignored.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__conditional_default(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"VAR=\tdefault",
".if ${OPSYS} == NetBSD",
"VAR?=\topsys",
".endif")
NewRedundantScope().Check(mklines)
// TODO: WARN: module.mk:3: The value \"opsys\" will never be assigned to VAR because it is defined unconditionally in line 1.
t.CheckOutputEmpty()
}
// These warnings are precise and accurate since the value of VAR is not used between line 2 and 4.
func (s *Suite) Test_RedundantScope__overwrite_same_variable_different_value(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"OTHER=\tvalue before",
"VAR=\tvalue ${OTHER}",
"OTHER=\tvalue after",
"VAR=\tvalue ${OTHER}")
NewRedundantScope().Check(mklines)
// Strictly speaking, line 1 is redundant because OTHER is not evaluated
// at load time and then immediately overwritten in line 3. If the operator
// in line 2 were a := instead of a =, the situation would be clear.
// Pkglint doesn't warn about the redundancy in line 1 because it prefers
// to omit warnings instead of giving wrong advice.
t.CheckOutputLines(
"NOTE: module.mk:4: Definition of VAR is redundant because of line 2.")
}
func (s *Suite) Test_RedundantScope__overwrite_different_value_used_between(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"OTHER=\tvalue before",
"VAR=\tvalue ${OTHER}",
// VAR is used here at load time, therefore it must be defined at this point.
// At this point, VAR uses the \"before\" value of OTHER.
"RESULT1:=\t${VAR}",
"OTHER=\tvalue after",
// VAR is used here again at load time, this time using the \"after\" value of OTHER.
"RESULT2:=\t${VAR}",
// Still this definition is redundant.
"VAR=\tvalue ${OTHER}")
NewRedundantScope().Check(mklines)
// There is nothing redundant here. Each write is followed by a
// corresponding read, except for the last one. That is ok though
// because in pkgsrc the last action of a package is to include
// bsd.pkg.mk, which reads almost all variables.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__procedure_call_to_noop(c *check.C) {
t := s.Init(c)
include, get := t.SetUpHierarchy()
include("mk/pthread.buildlink3.mk",
"CHECK_BUILTIN.pthread:= yes",
include("pthread.builtin.mk",
"# Nothing happens here."),
"CHECK_BUILTIN.pthread:= no")
NewRedundantScope().Check(get("mk/pthread.buildlink3.mk"))
t.CheckOutputLines(
"WARN: mk/pthread.buildlink3.mk:1: Variable CHECK_BUILTIN.pthread is overwritten in line 3.")
}
func (s *Suite) Test_RedundantScope__procedure_call_implemented(c *check.C) {
t := s.Init(c)
include, get := t.SetUpHierarchy()
include("mk/pthread.buildlink3.mk",
"CHECK_BUILTIN.pthread:= yes",
include("pthread.builtin.mk",
"CHECK_BUILTIN.pthread?= no",
".if !empty(CHECK_BUILTIN.pthread:M[Nn][Oo])",
".endif"),
"CHECK_BUILTIN.pthread:= no")
NewRedundantScope().Check(get("mk/pthread.buildlink3.mk"))
// This test is a bit unrealistic. It wrongly assumes that all files from
// an .include directive are actually included by pkglint.
//
// See Package.readMakefile/handleIncludeLine/skip.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__procedure_call_implemented_package(c *check.C) {
t := s.Init(c)
t.SetUpPkgsrc()
t.SetUpPackage("devel/gettext-lib")
t.SetUpPackage("x11/Xaos",
".include \"../../devel/gettext-lib/buildlink3.mk\"")
t.CreateFileLines("devel/gettext-lib/builtin.mk",
MkRcsID,
"",
".include \"../../mk/bsd.fast.prefs.mk\"",
"",
"CHECK_BUILTIN.gettext?=\tno",
".if !empty(CHECK_BUILTIN.gettext:M[nN][oO])",
".endif")
t.CreateFileLines("devel/gettext-lib/buildlink3.mk",
MkRcsID,
"CHECK_BUILTIN.gettext:=\tyes",
".include \"builtin.mk\"",
"CHECK_BUILTIN.gettext:=\tno")
G.Pkgsrc.LoadInfrastructure()
// Checking x11/Xaos instead of devel/gettext-lib avoids warnings
// about the minimal buildlink3.mk file.
G.Check(t.File("x11/Xaos"))
// There is nothing redundant here.
// Up to March 2019, pkglint didn't pass the correct pathnames to Package.included,
// which triggered a wrong note here.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__procedure_call_infrastructure(c *check.C) {
t := s.Init(c)
t.SetUpPackage("x11/alacarte",
".include \"../../mk/pthread.buildlink3.mk\"")
t.CreateFileLines("mk/pthread.buildlink3.mk",
MkRcsID,
"CHECK_BUILTIN.gettext:=\tyes",
".include \"pthread.builtin.mk\"",
"CHECK_BUILTIN.gettext:=\tno")
t.CreateFileLines("mk/pthread.builtin.mk",
MkRcsID,
"CHECK_BUILTIN.gettext?=\tno",
".if !empty(CHECK_BUILTIN.gettext:M[nN][oO])",
".endif")
G.Pkgsrc.LoadInfrastructure()
G.Check(t.File("x11/alacarte"))
// There is nothing redundant here.
//
// 1. pthread.buildlink3.mk sets the variable
// 2. pthread.builtin.mk assigns it a default value
// (which is common practice)
// 3. pthread.builtin.mk then reads it
// (which marks the next write as non-redundant)
// 4. pthread.buildlink3.mk sets the variable again
// (this is considered neither overwriting nor redundant)
//
// Up to March 2019, pkglint complained:
//
// WARN: ~/mk/pthread.buildlink3.mk:2:
// Variable CHECK_BUILTIN.gettext is overwritten in line 4.
//
// The cause for the warning is that when including files from the
// infrastructure, pkglint only includes the outermost level of files.
// If an infrastructure file includes another infrastructure file,
// pkglint skips that, for performance reasons.
//
// This optimization effectively made the .include for pthread.builtin.mk
// a no-op, therefore it was correct to issue a warning here.
//
// Since this warning is wrong, in March 2019 another special rule has
// been added to Package.readMakefile.handleIncludeLine.skip saying that
// including a buildlink3.mk file also includes the corresponding
// builtin.mk file.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__shell_and_eval(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"VAR:=\tvalue ${OTHER}",
"VAR!=\tvalue ${OTHER}")
NewRedundantScope().Check(mklines)
// As of November 2018, pkglint doesn't check redundancies that involve the := or != operators.
//
// What happens here is:
//
// Line 1 evaluates OTHER at load time.
// Line 1 assigns its value to VAR.
// Line 2 evaluates OTHER at load time.
// Line 2 passes its value through the shell and assigns the result to VAR.
//
// Since VAR is defined in line 1, not used afterwards and overwritten in line 2, it is redundant.
// Well, not quite, because evaluating ${OTHER} might have side-effects from :sh or ::= modifiers,
// but these are so rare that they are frowned upon and are not considered by pkglint.
//
// Expected result:
// WARN: module.mk:2: Previous definition of VAR in line 1 is unused.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__shell_and_eval_literal(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("module.mk",
"VAR:=\tvalue",
"VAR!=\tvalue")
NewRedundantScope().Check(mklines)
// Even when := is used with a literal value (which is usually
// only done for procedure calls), the shell evaluation can have
// so many different side effects that pkglint cannot reliably
// help in this situation.
//
// TODO: Why not? The evaluation in line 1 is trivial to analyze.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__included_OPSYS_variable(c *check.C) {
t := s.Init(c)
t.SetUpPackage("category/package",
".include \"../../category/dependency/buildlink3.mk\"",
"CONFIGURE_ARGS+=\tone",
"CONFIGURE_ARGS=\ttwo",
"CONFIGURE_ARGS+=\tthree")
t.SetUpPackage("category/dependency")
t.CreateFileDummyBuildlink3("category/dependency/buildlink3.mk")
t.CreateFileLines("category/dependency/builtin.mk",
MkRcsID,
"CONFIGURE_ARGS.Darwin+=\tdarwin")
G.Check(t.File("category/package"))
t.CheckOutputLines(
"WARN: ~/category/package/Makefile:21: Variable CONFIGURE_ARGS is overwritten in line 22.")
}
func (s *Suite) Test_RedundantScope__if_then_else(c *check.C) {
t := s.Init(c)
mklines := t.SetUpFileMkLines("if-then-else.mk",
MkRcsID,
".if exists(${FILE})",
"OS=\tNetBSD",
".else",
"OS=\tOTHER",
".endif")
NewRedundantScope().Check(mklines)
// These two definitions are of course not redundant since they happen in
// different branches of the same .if statement.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__if_then_else_without_variable(c *check.C) {
t := s.Init(c)
mklines := t.SetUpFileMkLines("if-then-else.mk",
MkRcsID,
".if exists(/nonexistent)",
"IT=\texists",
".else",
"IT=\tdoesn't exist",
".endif")
NewRedundantScope().Check(mklines)
// These two definitions are of course not redundant since they happen in
// different branches of the same .if statement.
// Even though the .if condition does not refer to any variables,
// this still means that the variable assignments are conditional.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__append_then_default(c *check.C) {
t := s.Init(c)
mklines := t.SetUpFileMkLines("append-then-default.mk",
MkRcsID,
"VAR+=\tvalue",
"VAR?=\tvalue")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: ~/append-then-default.mk:3: Default assignment of VAR has no effect because of line 2.")
}
func (s *Suite) Test_RedundantScope__assign_then_default_in_same_file(c *check.C) {
t := s.Init(c)
mklines := t.SetUpFileMkLines("assign-then-default.mk",
MkRcsID,
"VAR=\tvalue",
"VAR?=\tvalue")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"NOTE: ~/assign-then-default.mk:3: " +
"Default assignment of VAR has no effect because of line 2.")
}
func (s *Suite) Test_RedundantScope__eval_then_eval(c *check.C) {
t := s.Init(c)
mklines := t.SetUpFileMkLines("filename.mk",
MkRcsID,
"VAR:=\tvalue",
"VAR:=\tvalue",
"VAR:=\tother")
NewRedundantScope().Check(mklines)
t.CheckOutputLines(
"WARN: ~/filename.mk:2: Variable VAR is overwritten in line 3.",
"WARN: ~/filename.mk:3: Variable VAR is overwritten in line 4.")
}
func (s *Suite) Test_RedundantScope__shell_then_assign(c *check.C) {
t := s.Init(c)
mklines := t.SetUpFileMkLines("filename.mk",
MkRcsID,
"VAR!=\techo echo",
"VAR=\techo echo")
NewRedundantScope().Check(mklines)
// Although the two variable assignments look very similar, they do
// something entirely different. The first executes the echo command,
// and the second just assigns a string. Therefore the actual variable
// values are different, and the second assignment is not redundant.
// It assigns a different value. Nevertheless, the shell command is
// redundant and can be removed since its result is never used.
t.CheckOutputLines(
"WARN: ~/filename.mk:2: Variable VAR is overwritten in line 3.")
}
func (s *Suite) Test_RedundantScope__shell_then_read_then_assign(c *check.C) {
t := s.Init(c)
mklines := t.SetUpFileMkLines("filename.mk",
MkRcsID,
"VAR!=\techo echo",
"OUTPUT:=${VAR}",
"VAR=\techo echo")
NewRedundantScope().Check(mklines)
// No warning since the value is used in-between.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__assign_then_default_in_included_file(c *check.C) {
t := s.Init(c)
t.CreateFileLines("assign-then-default.mk",
MkRcsID,
"VAR=\tvalue",
".include \"included.mk\"")
t.CreateFileLines("included.mk",
MkRcsID,
"VAR?=\tvalue")
mklines := t.LoadMkInclude("assign-then-default.mk")
NewRedundantScope().Check(mklines)
// If assign-then-default.mk:2 is deleted, VAR still has the same value.
t.CheckOutputLines(
"NOTE: ~/assign-then-default.mk:2: Definition of VAR is redundant because of included.mk:2.")
}
func (s *Suite) Test_RedundantScope__conditionally_included_file(c *check.C) {
t := s.Init(c)
t.CreateFileLines("including.mk",
MkRcsID,
"VAR=\tvalue",
".if ${COND}",
". include \"included.mk\"",
".endif")
t.CreateFileLines("included.mk",
MkRcsID,
"VAR?=\tvalue")
mklines := t.LoadMkInclude("including.mk")
NewRedundantScope().Check(mklines)
// The assignment in including.mk:2 is only redundant if included.mk is actually included.
// Therefore both included.mk:2 nor including.mk:2 are relevant.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__procedure_parameters(c *check.C) {
t := s.Init(c)
t.CreateFileLines("mk/pkg-build-options.mk",
MkRcsID,
"USED:=\t${pkgbase}")
t.CreateFileLines("including.mk",
MkRcsID,
"pkgbase=\tpackage1",
".include \"mk/pkg-build-options.mk\"",
"",
"pkgbase=\tpackage2",
".include \"mk/pkg-build-options.mk\"",
"",
"pkgbase=\tpackage3",
".include \"mk/pkg-build-options.mk\"")
mklines := t.LoadMkInclude("including.mk")
NewRedundantScope().Check(mklines)
// This variable is not overwritten since it is used in-between
// by the included file.
t.CheckOutputEmpty()
}
// Branch coverage for info.vari.Constant(). The other tests typically
// make a variable non-constant by adding conditional assignments between
// .if/.endif. But there are other ways. The output of shell commands is
// unpredictable for pkglint (as of March 2019), therefore it treats these
// variables as non-constant.
func (s *Suite) Test_RedundantScope_handleVarassign__shell_followed_by_default(c *check.C) {
t := s.Init(c)
include, get := t.SetUpHierarchy()
include("including.mk",
"VAR!= echo 'hello, world'",
include("included.mk",
"VAR?= hello world"))
NewRedundantScope().Check(get("including.mk"))
// If pkglint should ever learn to interpret simple shell commands, there
// should be a warning for including.mk:2 that the shell command generates
// the default value.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope__overwrite_definition_from_included_file(c *check.C) {
t := s.Init(c)
t.CreateFileLines("included.mk",
MkRcsID,
"WRKSRC=\t${WRKDIR}/${PKGBASE}")
t.CreateFileLines("including.mk",
MkRcsID,
"SUBDIR=\t${WRKSRC}",
".include \"included.mk\"",
"WRKSRC=\t${WRKDIR}/overwritten")
mklines := t.LoadMkInclude("including.mk")
NewRedundantScope().Check(mklines)
// Before pkglint 5.7.2 (2019-03-10), the above setup generated a warning:
//
// WARN: ~/included.mk:2: Variable WRKSRC is overwritten in including.mk:4.
//
// This warning is obviously wrong since the included file must never
// receive a warning. Of course this default definition may be overridden
// by the including file.
//
// The warning was generated because in including.mk:2 the variable WRKSRC
// was used for the first time. Back then, each variable had only a single
// include path. That include path marks where the variable is used and
// defined.
//
// The variable definition at included.mk didn't modify this include path.
// Therefore pkglint wrongly assumed that this variable was only ever
// accessed in including.mk and issued a warning.
//
// To fix this, the RedundantScope now remembers every access to the
// variable, and the redundancy warnings are only issued in cases where
// either all variable accesses are in files including the current file,
// or all variable accesses are in files included by the current file.
t.CheckOutputEmpty()
}
func (s *Suite) Test_RedundantScope_handleVarassign__conditional(c *check.C) {
t := s.Init(c)
scope := NewRedundantScope()
mklines := t.NewMkLines("filename.mk",
MkRcsID,
"VAR=\tvalue",
".if 1",
"VAR=\tconditional",
".endif")
mklines.ForEach(func(mkline MkLine) {
scope.Handle(mkline, mklines.indentation)
})
t.Check(
scope.get("VAR").vari.WriteLocations(),
deepEquals,
[]MkLine{mklines.mklines[1], mklines.mklines[3]})
}
func (s *Suite) Test_includePath_includes(c *check.C) {
t := s.Init(c)
path := func(locations ...string) includePath {
return includePath{locations}
}
var (
m = path("Makefile")
mc = path("Makefile", "Makefile.common")
mco = path("Makefile", "Makefile.common", "other.mk")
mo = path("Makefile", "other.mk")
)
t.Check(m.includes(m), equals, false)
t.Check(m.includes(mc), equals, true)
t.Check(m.includes(mco), equals, true)
t.Check(mc.includes(mco), equals, true)
t.Check(mc.includes(m), equals, false)
t.Check(mc.includes(mo), equals, false)
t.Check(mo.includes(mc), equals, false)
}
func (s *Suite) Test_includePath_equals(c *check.C) {
t := s.Init(c)
path := func(locations ...string) includePath {
return includePath{locations}
}
var (
m = path("Makefile")
mc = path("Makefile", "Makefile.common")
mco = path("Makefile", "Makefile.common", "other.mk")
mo = path("Makefile", "other.mk")
)
t.Check(m.equals(m), equals, true)
t.Check(m.equals(mc), equals, false)
t.Check(m.equals(mco), equals, false)
t.Check(mc.equals(mco), equals, false)
t.Check(mc.equals(m), equals, false)
t.Check(mc.equals(mo), equals, false)
t.Check(mo.equals(mc), equals, false)
}
|