summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/pkglint_test.go
blob: f24cae3c12fe304771688a2394c73f5862037c74 (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
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
1285
1286
1287
1288
1289
1290
package pkglint

import (
	"gopkg.in/check.v1"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"strings"
)

func (pkglint *Pkglint) isUsable() bool { return pkglint.fileCache != nil }

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

	out, err := os.Create(t.CreateFileLines("out").String())
	c.Check(err, check.IsNil)
	outProfiling, err := os.Create(t.CreateFileLines("out.profiling").String())
	c.Check(err, check.IsNil)

	t.SetUpPackage("category/package")
	t.Chdir("category/package")
	t.FinishSetUp()

	runMain := func(out *os.File, commandLine ...string) {
		exitCode := G.Main(out, out, commandLine)
		t.CheckEquals(exitCode, 0)
	}

	runMain(out, "pkglint", ".")
	runMain(outProfiling, "pkglint", "--profiling", ".")

	c.Check(out.Close(), check.IsNil)
	c.Check(outProfiling.Close(), check.IsNil)

	t.CheckOutputEmpty()          // Because all output is redirected.
	t.CheckFileLines("../../out", // See the t.Chdir above.
		"Looks fine.")
	// outProfiling is not checked because it contains timing information.
}

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

	exitCode := t.Main("-h")

	t.CheckEquals(exitCode, 0)
	t.CheckOutputLines(
		"usage: pkglint [options] dir...",
		"",
		"  -C, --check=check,...       enable or disable specific checks",
		"  -d, --debug                 log verbose call traces for debugging",
		"  -e, --explain               explain the diagnostics or give further help",
		"  -f, --show-autofix          show what pkglint can fix automatically",
		"  -F, --autofix               try to automatically fix some errors",
		"  -g, --gcc-output-format     mimic the gcc output format",
		"  -h, --help                  show a detailed usage message",
		"  -I, --dumpmakefile          dump the Makefile after parsing",
		"  -i, --import                prepare the import of a wip package",
		"  -o, --only                  only log diagnostics containing the given text",
		"  -p, --profiling             profile the executing program",
		"  -q, --quiet                 don't show a summary line when finishing",
		"  -r, --recursive             check subdirectories, too",
		"  -s, --source                show the source lines together with diagnostics",
		"  -V, --version               show the version number of pkglint",
		"  -W, --warning=warning,...   enable or disable groups of warnings",
		"",
		"  Flags for -C, --check:",
		"    all      all of the following",
		"    none     none of the following",
		"    global   inter-package checks (disabled)",
		"",
		"  Flags for -W, --warning:",
		"    all       all of the following",
		"    none      none of the following",
		"    extra     enable some extra warnings (disabled)",
		"    perm      warn about unforeseen variable definition and use (disabled)",
		"    quoting   warn about quoting issues (disabled)",
		"    space     warn about inconsistent use of whitespace (disabled)",
		"    style     warn about stylistic issues (disabled)",
		"",
		"  (Prefix a flag with \"no-\" to disable it.)")
}

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

	exitcode := t.Main("--version")

	t.CheckEquals(exitcode, 0)
	t.CheckOutputLines(
		confVersion)
}

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

	exitcode := t.Main()

	// The "." from the error message is the implicit argument added in Pkglint.Main.
	t.CheckEquals(exitcode, 1)
	t.CheckOutputLines(
		"FATAL: .: Must be inside a pkgsrc tree.")
}

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

	exitcode := t.Main("--unknown-option")

	t.CheckEquals(exitcode, 1)
	c.Check(t.Output(), check.Matches,
		`\Qpkglint: unknown option: --unknown-option\E\n`+
			`\Q\E\n`+
			`\Qusage: pkglint [options] dir...\E\n`+
			`(?s).+`)
	// See Test_Pkglint_Main__help for the complete output.
}

// Demonstrates which infrastructure files are necessary to actually run
// pkglint in a realistic scenario.
//
// Especially covers Pkglint.ShowSummary and Pkglint.checkReg.
func (s *Suite) Test_Pkglint_Main__complete_package(c *check.C) {
	t := s.Init(c)

	// Since the general infrastructure setup is useful for several tests,
	// it is available as a separate method.
	//
	// In this test, several of the infrastructure files are later
	// overwritten with more realistic and interesting content.
	// This is typical of the pkglint tests.
	t.SetUpPkgsrc()

	t.CreateFileLines("doc/CHANGES-2018",
		CvsID,
		"",
		"Changes to the packages collection and infrastructure in 2018:",
		"",
		"\tUpdated sysutils/checkperms to 1.10 [rillig 2018-01-05]")

	// See Pkgsrc.loadSuggestedUpdates.
	t.CreateFileLines("doc/TODO",
		CvsID,
		"",
		"Suggested package updates",
		"",
		"\to checkperms-1.13 [supports more file formats]")

	// The MASTER_SITES in the package Makefile are searched here.
	// See Pkgsrc.loadMasterSites.
	t.CreateFileLines("mk/fetch/sites.mk",
		MkCvsID,
		"",
		"MASTER_SITE_GITHUB+=\thttps://github.com/")

	// After setting up the pkgsrc infrastructure, the files for
	// a complete pkgsrc package are created individually.
	//
	// In this test each file is created manually for demonstration purposes.
	// Other tests typically call t.SetUpPackage, which does most of the work
	// shown here while allowing to adjust the package Makefile a little bit.

	// The existence of this file makes the category "sysutils" valid,
	// so that it can be used in CATEGORIES in the package Makefile.
	// The category "tools" on the other hand is not valid.
	t.CreateFileLines("sysutils/Makefile",
		MkCvsID)

	// The package Makefile in this test is quite simple, containing just the
	// standard variable definitions. The data for checking the variable
	// values is partly defined in the pkgsrc infrastructure files
	// (as defined in the previous lines), and partly in the pkglint
	// code directly. Many details can be found in vartypecheck.go.
	t.CreateFileLines("sysutils/checkperms/Makefile",
		MkCvsID,
		"",
		"DISTNAME=\tcheckperms-1.11",
		"CATEGORIES=\tsysutils tools",
		"MASTER_SITES=\t${MASTER_SITE_GITHUB:=rillig/}",
		"",
		"MAINTAINER=\tpkgsrc-users@NetBSD.org",
		"HOMEPAGE=\thttps://github.com/rillig/checkperms/",
		"COMMENT=\tCheck file permissions",
		"LICENSE=\t2-clause-bsd",
		"",
		".include \"../../mk/bsd.pkg.mk\"")

	t.CreateFileLines("sysutils/checkperms/MESSAGE",
		"===========================================================================",
		CvsID,
		"",
		"After installation, this package has to be configured in a special way.",
		"",
		"===========================================================================")

	t.CreateFileLines("sysutils/checkperms/PLIST",
		PlistCvsID,
		"bin/checkperms",
		"man/man1/checkperms.1")

	t.CreateFileLines("sysutils/checkperms/README",
		"When updating this package, test the pkgsrc bootstrap.")

	t.CreateFileLines("sysutils/checkperms/TODO",
		"Make the package work on MS-DOS")

	t.CreateFileLines("sysutils/checkperms/patches/patch-checkperms.c",
		CvsID,
		"",
		"A simple patch demonstrating that pkglint checks for missing",
		"removed lines. The hunk headers says that one line is to be",
		"removed, but in fact, there is no deletion line below it.",
		"",
		"--- a/checkperms.c",
		"+++ b/checkperms.c",
		"@@ -1,1 +1,3 @@", // at line 1, delete 1 line; at line 1, add 3 lines
		"+// Header 1",
		"+// Header 2",
		"+// Header 3")
	t.CreateFileLines("sysutils/checkperms/distinfo",
		CvsID,
		"",
		"SHA1 (checkperms-1.12.tar.gz) = 34c084b4d06bcd7a8bba922ff57677e651eeced5",
		"RMD160 (checkperms-1.12.tar.gz) = cd95029aa930b6201e9580b3ab7e36dd30b8f925",
		"SHA512 (checkperms-1.12.tar.gz) = "+
			"43e37b5963c63fdf716acdb470928d7e21a7bdfddd6c85cf626a11acc7f45fa5"+
			"2a53d4bcd83d543150328fe8cec5587987d2d9a7c5f0aaeb02ac1127ab41f8ae",
		"Size (checkperms-1.12.tar.gz) = 6621 bytes",
		"SHA1 (patch-checkperms.c) = asdfasdf") // Invalid SHA-1 checksum

	t.Main("-Wall", "-Call", "sysutils/checkperms")

	t.CheckOutputLines(
		"NOTE: ~/sysutils/checkperms/Makefile:3: "+
			"Package version \"1.11\" is greater than the latest \"1.10\" "+
			"from ../../doc/CHANGES-2018:5.",
		"WARN: ~/sysutils/checkperms/Makefile:3: "+
			"This package should be updated to 1.13 (supports more file formats; see ../../doc/TODO:5).",
		"ERROR: ~/sysutils/checkperms/Makefile:4: Invalid category \"tools\".",
		"ERROR: ~/sysutils/checkperms/README: Packages in main pkgsrc must not have a README file.",
		"ERROR: ~/sysutils/checkperms/TODO: Packages in main pkgsrc must not have a TODO file.",
		"ERROR: ~/sysutils/checkperms/distinfo:7: SHA1 hash of patches/patch-checkperms.c differs "+
			"(distinfo has asdfasdf, patch file has e775969de639ec703866c0336c4c8e0fdd96309c).",
		"WARN: ~/sysutils/checkperms/patches/patch-checkperms.c:12: Premature end of patch hunk "+
			"(expected 1 lines to be deleted and 0 lines to be added).",
		"4 errors, 2 warnings and 1 note found.",
		t.Shquote("(Run \"pkglint -e -Wall -Call %s\" to show explanations.)", "sysutils/checkperms"),
		t.Shquote("(Run \"pkglint -fs -Wall -Call %s\" to show what can be fixed automatically.)", "sysutils/checkperms"),
		t.Shquote("(Run \"pkglint -F -Wall -Call %s\" to automatically fix some issues.)", "sysutils/checkperms"))
}

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

	t.SetUpPkgsrc()
	t.CreateFileLines("filename.mk",
		"")

	exitcode := t.Main("-Wall", "--autofix", "filename.mk")

	t.CheckOutputLines(
		"AUTOFIX: ~/filename.mk:1: Inserting a line \"" + MkCvsID + "\" before this line.")
	t.CheckEquals(exitcode, 0)
}

// Run pkglint in a realistic environment.
//
//  env \
//  PKGLINT_TESTDIR="..." \
//  PKGLINT_TESTCMDLINE="-r" \
//  go test -covermode=count -test.coverprofile pkglint.cov
//
//  go tool cover -html=pkglint.cov -o coverage.html
//
// To measure the branch coverage of pkglint checking a complete pkgsrc installation,
// install https://github.com/rillig/gobco and adjust the following code:
//
//  env \
//  PKGLINT_TESTDIR=C:/Users/rillig/git/pkgsrc \
//  PKGLINT_TESTCMDLINE="-r -Wall -Call -e" \
//  gobco -test.covermode=count \
//      -test.coverprofile=pkglint-pkgsrc.pprof \
//      -timeout=3600s -check.f '^Test_Pkglint__realistic' \
//      > pkglint-pkgsrc.out
//
// See https://github.com/rillig/gobco for the tool to measure the branch coverage.
func (s *Suite) Test_Pkglint_Main__realistic(c *check.C) {
	if cwd := os.Getenv("PKGLINT_TESTDIR"); cwd != "" {
		err := os.Chdir(cwd)
		c.Assert(err, check.IsNil)
	}

	cmdline := os.Getenv("PKGLINT_TESTCMDLINE")
	if cmdline != "" {
		G.Main(os.Stdout, os.Stderr, append([]string{"pkglint"}, strings.Fields(cmdline)...))
	}
}

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

	t.SetUpPkgsrc()
	t.Chdir(".")

	t.Main("--profiling")

	// Pkglint always writes the profiling data into the current directory.
	// TODO: Make the location of the profiling log a mandatory parameter.
	t.CheckEquals(NewPath("pkglint.pprof").IsFile(), true)

	err := os.Remove("pkglint.pprof")
	c.Check(err, check.IsNil)

	// Everything but the first few lines of output is not easily testable
	// or not interesting enough, since that info includes the exact timing
	// that the top time-consuming regular expressions took.
	firstOutput := strings.Split(t.Output(), "\n")[0]
	t.CheckEquals(firstOutput, "ERROR: Makefile: Cannot be read.")
}

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

	t.Chdir(".")
	t.CreateFileLines("pkglint.pprof/file")

	exitcode := t.Main("--profiling")

	t.CheckEquals(exitcode, 1)
	t.CheckOutputMatches(
		`ERROR: pkglint\.pprof: Cannot create profiling file: open pkglint\.pprof: .*`)
}

// Branch coverage for Logger.Logf, the level != Fatal case.
func (s *Suite) Test_Pkglint_prepareMainLoop__fatal(c *check.C) {
	t := s.Init(c)

	t.Chdir(".")
	t.Main("--profiling", t.File("does-not-exist").String())

	t.CheckOutputLines(
		"fileCache: 0 hits, 0 misses",
		"FATAL: does-not-exist: Must be inside a pkgsrc tree.")
}

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

	exitcode := G.ParseCommandLine([]string{"pkglint", "-Wall", "--only", ":Q", "--version"})

	if exitcode != -1 {
		t.CheckEquals(exitcode, 0)
	}
	t.CheckDeepEquals(G.Opts.LogOnly, []string{":Q"})
	t.CheckOutputLines(
		confVersion)
}

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

	t.CreateFileLines("empty")

	G.Check(t.File("."))

	// In a realistic scenario, pkglint will only reach this point
	// when the first command line argument is valid but a following
	// argument is outside the pkgsrc tree.
	//
	// If the first argument is already outside of any pkgsrc tree,
	// pkglint will exit with a fatal error message since it doesn't
	// know where to load the infrastructure files from.
	t.CheckOutputLines(
		"ERROR: ~: Cannot determine the pkgsrc root directory for \"~\".")
}

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

	t.SetUpPkgsrc()
	t.CreateFileLines("category/package/CVS/Entries")
	t.FinishSetUp()

	G.Check(t.File("category/package"))

	// Empty directories are silently skipped.
	t.CheckOutputEmpty()
}

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

	t.SetUpPkgsrc()
	t.CreateFileLines("category/package/files/README.md")
	t.FinishSetUp()

	G.Check(t.File("category/package/files"))

	// This diagnostic is not really correct, but it's an edge case anyway.
	t.CheckOutputLines(
		"ERROR: ~/category/package/files: Cannot check directories outside a pkgsrc tree.")
}

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

	t.SetUpPkgsrc()
	t.CreateFileDummyPatch("category/package/patches/patch-README.md")
	t.FinishSetUp()

	G.Check(t.File("category/package/patches"))

	// This diagnostic is not really correct, but it's an edge case anyway.
	t.CheckOutputLines(
		"ERROR: ~/category/package/patches: Cannot check directories outside a pkgsrc tree.")
}

// See devel/libtool for an example package that uses manual patches.
func (s *Suite) Test_Pkglint_Check__manual_patch(c *check.C) {
	t := s.Init(c)

	t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/patches/unknown-file")
	t.CreateFileLines("category/package/patches/manual-configure")
	t.FinishSetUp()

	G.Check(t.File("category/package"))

	// Pkglint doesn't inspect the manual patch files, it also doesn't mark them as unknown files.
	t.CheckOutputLines(
		"WARN: ~/category/package/patches/unknown-file: Patch files should be named \"patch-\", " +
			"followed by letters, '-', '_', '.', and digits only.")
}

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

	t.SetUpPkgsrc()
	t.FinishSetUp()

	G.Check(G.Pkgsrc.File("doc/TODO"))

	// The file doc/TODO cannot be checked explicitly and individually.
	// It is loaded as part of the pkgsrc infrastructure and is thus
	// checked implicitly whenever a package or an individual file is checked.
	t.CheckOutputLines(
		"WARN: ~/doc/TODO: Unexpected file found.")
}

// This test covers the different code paths for deciding whether a directory
// should be checked as the top-level, a category or a package.
func (s *Suite) Test_Pkglint_Check(c *check.C) {
	t := s.Init(c)

	t.SetUpVartypes()
	t.CreateFileLines("mk/misc/category.mk")
	t.CreateFileLines("mk/bsd.pkg.mk")
	t.CreateFileLines("category/package/Makefile")
	t.CreateFileLines("category/Makefile",
		MkCvsID,
		"",
		"COMMENT=\tCategory\u0007",
		"",
		"SUBDIR+=\tpackage",
		"",
		".include \"../mk/misc/category.mk\"")
	t.CreateFileLines("Makefile",
		MkCvsID,
		"COMMENT=\tToplevel\u0005")

	G.Check(t.File("."))

	t.CheckOutputLines(
		"WARN: ~/Makefile:2: Line contains invalid characters (U+0005).")

	G.Check(t.File("category"))

	t.CheckOutputLines(
		"WARN: ~/category/Makefile:3: Line contains invalid characters (U+0007).",
		"WARN: ~/category/Makefile:3: COMMENT contains invalid characters (U+0007).")

	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"ERROR: ~/category/package/Makefile: Must not be empty.")

	G.Check(t.File("category/package/nonexistent"))

	t.CheckOutputLines(
		"ERROR: ~/category/package/nonexistent: No such file or directory.")
}

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

	t.SetUpCommandLine("-Call", "-Wall,no-space", "--import")
	pkg := t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/work/log")
	t.CreateFileLines("category/package/Makefile~")
	t.CreateFileLines("category/package/Makefile.orig")
	t.CreateFileLines("category/package/Makefile.rej")
	t.FinishSetUp()

	G.Check(pkg)

	t.CheckOutputLines(
		"ERROR: ~/category/package/Makefile.orig: Must be cleaned up before committing the package.",
		"ERROR: ~/category/package/Makefile.rej: Must be cleaned up before committing the package.",
		"ERROR: ~/category/package/Makefile~: Must be cleaned up before committing the package.",
		"ERROR: ~/category/package/work: Must be cleaned up before committing the package.")
}

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

	G.checkMode("/dev/null", os.ModeDevice)

	t.CheckOutputLines(
		"ERROR: /dev/null: No such file or directory.")
}

// A package that is very incomplete may produce lots of warnings.
// This case is unrealistic since most packages are either generated by url2pkg
// or copied from an existing working package.
func (s *Suite) Test_Pkglint_checkdirPackage(c *check.C) {
	t := s.Init(c)

	t.Chdir("category/package")
	t.CreateFileLines("Makefile",
		MkCvsID)

	G.checkdirPackage(".")

	t.CheckOutputLines(
		"WARN: Makefile: This package should have a PLIST file.",
		"WARN: distinfo: A package that downloads files should have a distinfo file.",
		"ERROR: Makefile: Each package must define its LICENSE.",
		"WARN: Makefile: Each package should define a COMMENT.")
}

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

	t.SetUpPkgsrc()
	t.CreateFileLines("category/Makefile")
	t.CreateFileLines("other/package/Makefile",
		MkCvsID)
	t.CreateFileLines("other/package/PLIST",
		PlistCvsID,
		"bin/program")
	t.CreateFileLines("other/package/distinfo",
		CvsID,
		"",
		"SHA1 (patch-aa) = da39a3ee5e6b4b0d3255bfef95601890afd80709")
	t.CreateFileLines("category/package/patches/patch-aa",
		CvsID)
	t.Chdir("category/package")
	t.CreateFileLines("Makefile",
		MkCvsID,
		"",
		"CATEGORIES=\tcategory",
		"",
		"COMMENT=\tComment",
		"LICENSE=\t2-clause-bsd",
		"PKGDIR=\t\t../../other/package")
	t.FinishSetUp()

	// DISTINFO_FILE is resolved relative to PKGDIR,
	// the other locations are resolved relative to the package base directory.
	G.checkdirPackage(".")

	t.CheckOutputLines(
		"ERROR: patches/patch-aa:1: Patch files must not be empty.")
}

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

	pkg := t.SetUpPackage("category/package")
	t.CreateFileDummyPatch("category/package/patches/patch-aa")
	t.Remove("category/package/distinfo")
	t.FinishSetUp()

	G.Check(pkg)

	t.CheckOutputLines(
		"WARN: ~/category/package/distinfo: A package that downloads files should have a distinfo file.",
		"WARN: ~/category/package/distinfo: A package with patches should have a distinfo file.")
}

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

	t.Chdir("category/package")
	t.CreateFileLines("Makefile",
		MkCvsID,
		"",
		"META_PACKAGE=\tyes")
	t.SetUpVartypes()

	G.checkdirPackage(".")

	// No error about missing LICENSE since meta-packages don't need a license.
	// They are so simple that there is no reason to have any license.
	t.CheckOutputLines(
		"WARN: Makefile: Each package should define a COMMENT.")
}

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

	pkg := t.SetUpPackage("category/package",
		".include \"../../mk/bsd.prefs.mk\"",
		"",
		"RUBY_VERSIONS_ACCEPTED=\t22 23 24 25", // As of 2018.
		".for rv in ${RUBY_VERSIONS_ACCEPTED}",
		"RUBY_VER?=\t\t${rv}",
		".endfor",
		"",
		"RUBY_PKGDIR=\t../../lang/ruby-${RUBY_VER}-base",
		"DISTINFO_FILE=\t${RUBY_PKGDIR}/distinfo")
	t.FinishSetUp()

	// As of January 2019, pkglint cannot resolve the location of DISTINFO_FILE completely
	// because the variable \"rv\" comes from a .for loop.
	//
	// TODO: iterate over variables in simple .for loops like the above.
	// TODO: when implementing the above, take care of deeply nested loops (42.zip).
	G.Check(pkg)

	t.CheckOutputEmpty()

	// Just for code coverage.
	t.DisableTracing()
	G.Check(pkg)
	t.CheckOutputEmpty()
}

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

	t.SetUpCommandLine("-Wall,no-space")
	pkg := t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/ALTERNATIVES",
		"bin/wrapper bin/wrapper-impl")
	t.FinishSetUp()

	G.Check(pkg)

	t.CheckOutputLines(
		"ERROR: ~/category/package/ALTERNATIVES:1: "+
			"Alternative implementation \"bin/wrapper-impl\" must appear in the PLIST.",
		"ERROR: ~/category/package/ALTERNATIVES:1: "+
			"Alternative implementation \"bin/wrapper-impl\" must be an absolute path.")
}

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

	t.SetUpPackage("category/package",
		"DISTINFO_FILE=\tnonexistent")
	t.FinishSetUp()

	G.Check(t.File("category/package"))

	t.CheckOutputLines(
		"WARN: ~/category/package/nonexistent: A package that downloads files should have a distinfo file.",
		"ERROR: ~/category/package/Makefile:20: Relative path \"nonexistent\" does not exist.")
}

// Pkglint must never be trapped in an endless loop, even when
// resolving the value of a variable that refers back to itself.
func (s *Suite) Test_resolveVariableRefs__circular_reference(c *check.C) {
	t := s.Init(c)

	mkline := t.NewMkLine("filename.mk", 1, "VAR=\t1:${VAR}+ 2:${VAR}")
	G.Pkg = NewPackage(t.File("category/pkgbase"))
	G.Pkg.vars.Define("VAR", mkline)

	// TODO: It may be better to define MkLines.Resolve and Package.Resolve,
	//  to clearly state the scope of the involved variables.
	resolved := resolveVariableRefs(nil, "the a:${VAR} b:${VAR}")

	// TODO: The ${VAR} after "b:" should also be expanded since there
	//  is no recursion.
	t.CheckEquals(resolved, "the a:1:${VAR}+ 2:${VAR} b:${VAR}")
}

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

	mkline1 := t.NewMkLine("filename.mk", 10, "FIRST=\t${SECOND}")
	mkline2 := t.NewMkLine("filename.mk", 11, "SECOND=\t${THIRD}")
	mkline3 := t.NewMkLine("filename.mk", 12, "THIRD=\tgot it")
	G.Pkg = NewPackage(t.File("category/pkgbase"))
	G.Pkg.vars.Define("FIRST", mkline1)
	G.Pkg.vars.Define("SECOND", mkline2)
	G.Pkg.vars.Define("THIRD", mkline3)

	// TODO: Add a similar test in which some of the variables are defined
	//  conditionally or with differing values, just to see what pkglint does
	//  in such a case.
	resolved := resolveVariableRefs(nil, "you ${FIRST}")

	t.CheckEquals(resolved, "you got it")
}

// Usually, a dot in a variable name means a parameterized form.
// In this case, it is part of a version number. Resolving these
// variables from the scope works nevertheless.
func (s *Suite) Test_resolveVariableRefs__special_chars(c *check.C) {
	t := s.Init(c)

	mkline := t.NewMkLine("filename.mk", 10, "_=x11")
	G.Pkg = NewPackage(t.File("category/pkg"))
	G.Pkg.vars.Define("GST_PLUGINS0.10_TYPE", mkline)

	resolved := resolveVariableRefs(nil, "gst-plugins0.10-${GST_PLUGINS0.10_TYPE}/distinfo")

	t.CheckEquals(resolved, "gst-plugins0.10-x11/distinfo")
}

// Just for code coverage.
func (s *Suite) Test_CheckFileOther__no_tracing(c *check.C) {
	t := s.Init(c)

	t.DisableTracing()

	CheckFileOther(t.File("filename.mk"))

	t.CheckOutputLines(
		"ERROR: ~/filename.mk: Cannot be read.")
}

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

	t.SetUpVartypes()
	lines := t.NewLines("DESCR",
		"word "+strings.Repeat("X", 80),
		strings.Repeat("X", 90), // No warning since there are no spaces.
		"", "", "", "", "", "", "", "10",
		"Try ${PREFIX}",
		"", "", "", "", "", "", "", "", "20",
		"... expressions like ${key} to ... ${unfinished",
		"", "", "", "", "", "", "", "", "30")

	CheckLinesDescr(lines)

	// The package author may think that variables like ${PREFIX}
	// are expanded in DESCR files too, but that doesn't happen.
	//
	// Variables that are not well-known in pkgsrc are not warned
	// about since these are probably legitimate examples, as seen
	// in devel/go-properties/DESCR.
	t.CheckOutputLines(
		"WARN: DESCR:1: Line too long (should be no more than 80 characters).",
		"NOTE: DESCR:11: Variables are not expanded in the DESCR file.",
		"WARN: DESCR:25: File too long (should be no more than 24 lines).")
}

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

	lines := t.NewLines("MESSAGE",
		"one line")

	CheckLinesMessage(lines)

	t.CheckOutputLines(
		"WARN: MESSAGE:1: File too short.")
}

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

	lines := t.NewLines("MESSAGE",
		strings.Repeat("=", 75))

	CheckLinesMessage(lines)

	t.CheckOutputLines(
		"WARN: MESSAGE:1: File too short.")
}

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

	lines := t.NewLines("MESSAGE",
		"1",
		"2",
		"3",
		"4",
		"5")

	CheckLinesMessage(lines)

	t.CheckOutputLines(
		"WARN: MESSAGE:1: Expected a line of exactly 75 \"=\" characters.",
		"ERROR: MESSAGE:1: Expected \"$"+"NetBSD$\".",
		"WARN: MESSAGE:5: Expected a line of exactly 75 \"=\" characters.")
}

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

	t.SetUpCommandLine("-Wall", "--autofix")
	lines := t.SetUpFileLines("MESSAGE",
		"1",
		"2",
		"3",
		"4",
		"5")

	CheckLinesMessage(lines)

	t.CheckOutputLines(
		"AUTOFIX: ~/MESSAGE:1: Inserting a line \"=============================="+
			"=============================================\" before this line.",
		"AUTOFIX: ~/MESSAGE:1: Inserting a line \"$"+"NetBSD$\" before this line.",
		"AUTOFIX: ~/MESSAGE:5: Inserting a line \"=============================="+
			"=============================================\" after this line.")
	t.CheckFileLines("MESSAGE",
		"===========================================================================",
		CvsID,
		"1",
		"2",
		"3",
		"4",
		"5",
		"===========================================================================")
}

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

	hline := strings.Repeat("=", 75)
	t.SetUpPackage("category/package",
		"MESSAGE_SRC=\t../../category/package/MESSAGE.common",
		"MESSAGE_SRC+=\t${.CURDIR}/MESSAGE")
	t.CreateFileLines("category/package/MESSAGE.common",
		hline,
		CvsID,
		"common line")
	t.CreateFileLines("category/package/MESSAGE",
		hline)

	t.Main("category/package")

	t.CheckOutputLines(
		"Looks fine.")
}

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

	CheckFileMk(t.File("filename.mk"))

	t.CheckOutputLines(
		"ERROR: ~/filename.mk: Cannot be read.")
}

// Demonstrates that an ALTERNATIVES file can be tested individually,
// without any dependencies on a whole package or a PLIST file.
func (s *Suite) Test_Pkglint_checkReg__alternatives(c *check.C) {
	t := s.Init(c)

	t.SetUpPkgsrc()
	lines := t.SetUpFileLines("category/package/ALTERNATIVES",
		"bin/tar bin/gnu-tar")

	t.Main(lines.Filename.String())

	t.CheckOutputLines(
		"ERROR: ~/category/package/ALTERNATIVES:1: Alternative implementation \"bin/gnu-tar\" must be an absolute path.",
		"1 error found.",
		t.Shquote("(Run \"pkglint -e %s\" to show explanations.)", "category/package/ALTERNATIVES"))
}

// Just for branch coverage.
func (s *Suite) Test_Pkglint_checkReg__file_not_found(c *check.C) {
	t := s.Init(c)

	t.Chdir(".")

	G.checkReg("buildlink3.mk", "buildlink3.mk", 3)
	G.checkReg("DESCR", "DESCR", 3)
	G.checkReg("distinfo", "distinfo", 3)
	G.checkReg("MESSAGE", "MESSAGE", 3)
	G.checkReg("patches/patch-aa", "patch-aa", 3)
	G.checkReg("PLIST", "PLIST", 3)

	t.CheckOutputLines(
		"ERROR: buildlink3.mk: Cannot be read.",
		"ERROR: DESCR: Cannot be read.",
		"ERROR: distinfo: Cannot be read.",
		"ERROR: MESSAGE: Cannot be read.",
		"ERROR: patches/patch-aa: Cannot be read.",
		"ERROR: PLIST: Cannot be read.")
}

// Just for branch coverage.
func (s *Suite) Test_Pkglint_checkReg__no_tracing(c *check.C) {
	t := s.Init(c)

	t.Chdir(".")
	t.DisableTracing()

	G.checkReg("patches/manual-aa", "manual-aa", 4)

	t.CheckOutputEmpty()
}

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

	t.SetUpPackage("category/package")
	t.Chdir("category/package")
	t.CreateFileLines("log")

	t.Main()

	t.CheckOutputLines(
		"WARN: log: Unexpected file found.",
		"1 warning found.")
}

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

	t.SetUpCommandLine("-Call", "-Wall,no-space")
	pkg := t.SetUpPackage("category/package")
	t.CreateFileLines("category/package/INSTALL",
		"#! /bin/sh")
	t.CreateFileLines("category/package/DEINSTALL",
		"#! /bin/sh")
	t.FinishSetUp()

	G.Check(pkg)

	t.CheckOutputEmpty()
}

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

	t.CreateFileLines("category/Makefile",
		MkCvsID)

	t.CreateFileLines("category/package/files/README",
		"Extra file that is installed later.")
	t.CreateFileDummyPatch("category/package/patches/patch-README")
	t.CreateFileLines("category/package/Makefile",
		MkCvsID,
		"CATEGORIES=category",
		"",
		"COMMENT=Comment",
		"LICENSE=2-clause-bsd")
	t.CreateFileLines("category/package/PLIST",
		PlistCvsID,
		"bin/program")
	t.CreateFileLines("category/package/README",
		"This package ...")
	t.CreateFileLines("category/package/TODO",
		"Make this package work.")
	t.CreateFileLines("category/package/distinfo",
		CvsID,
		"",
		"SHA1 (patch-README) = ebbf34b0641bcb508f17d5a27f2bf2a536d810ac")

	// Copy category/package/** to wip/package.
	err := filepath.Walk(
		t.File("category/package").String(),
		func(pathname string, info os.FileInfo, err error) error {
			if info.Mode().IsRegular() {
				src := filepath.ToSlash(pathname)
				dst := strings.Replace(src, "category/package", "wip/package", 1)
				text, e := ioutil.ReadFile(src)
				c.Check(e, check.IsNil)
				_ = os.MkdirAll(path.Dir(dst), 0700)
				e = ioutil.WriteFile(dst, []byte(text), 0600)
				c.Check(e, check.IsNil)
			}
			return err
		})
	c.Check(err, check.IsNil)

	t.SetUpPkgsrc()
	t.Chdir(".")

	t.Main("category/package", "wip/package")

	t.CheckOutputLines(
		"ERROR: category/package/README: Packages in main pkgsrc must not have a README file.",
		"ERROR: category/package/TODO: Packages in main pkgsrc must not have a TODO file.",
		"2 errors found.")

	t.Main("--import", "category/package", "wip/package")

	t.CheckOutputLines(
		"ERROR: category/package/README: Packages in main pkgsrc must not have a README file.",
		"ERROR: category/package/TODO: Packages in main pkgsrc must not have a TODO file.",
		"ERROR: wip/package/README: Must be cleaned up before committing the package.",
		"ERROR: wip/package/TODO: Must be cleaned up before committing the package.",
		"4 errors found.")
}

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

	t.CreateFileDummyPatch("category/Makefile/patches/index")

	G.checkReg(t.File("category/Makefile/patches/index"), "index", 4)

	t.CheckOutputLines(
		"WARN: ~/category/Makefile/patches/index: " +
			"Patch files should be named \"patch-\", followed by letters, '-', '_', '.', and digits only.")
}

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

	t.CreateFileDummyPatch("category/package/patches/patch-compiler.mk")
	t.Chdir("category/package")

	G.checkReg(t.File("patches/patch-compiler.mk"), "patch-compiler.mk", 4)

	t.CheckOutputEmpty()
}

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

	t.CreateFileLines("category/package/files/index")

	G.checkReg(t.File("category/package/files/index"), "index", 4)

	// These files are ignored since they could contain anything.
	t.CheckOutputEmpty()
}

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

	t.CreateFileLines("category/package/spec")
	t.CreateFileLines("regress/package/spec")

	G.checkReg(t.File("category/package/spec"), "spec", 3)
	G.checkReg(t.File("regress/package/spec"), "spec", 3)

	t.CheckOutputLines(
		"WARN: ~/category/package/spec: Only packages in regress/ may have spec files.")
}

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

	filename := t.CreateFileLines("file.mk")
	err := filename.Chmod(0555)
	assertNil(err, "")

	G.checkExecutable(filename, 0555)

	t.CheckOutputLines(
		"WARN: ~/file.mk: Should not be executable.")

	t.SetUpCommandLine("--autofix")

	G.checkExecutable(filename, 0555)

	t.CheckOutputMatches(
		"AUTOFIX: ~/file.mk: Clearing executable bits")

	// On Windows, this is effectively a no-op test since there is no
	// execute-bit. The only relevant permissions bit is whether a
	// file is readonly or not.
	st, err := filename.Lstat()
	if t.Check(err, check.IsNil) {
		t.CheckEquals(st.Mode()&0111, os.FileMode(0))
	}
}

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

	filename := t.File("file.mk")

	G.checkExecutable(filename, 0555)

	t.CheckOutputLines(
		"WARN: ~/file.mk: Should not be executable.")

	t.SetUpCommandLine("--autofix")

	G.checkExecutable(filename, 0555)

	t.CheckOutputMatches(
		"AUTOFIX: ~/file.mk: Clearing executable bits",
		`ERROR: ~/file.mk: Cannot clear executable bits: chmod ~/file.mk: .*`)
}

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

	t.CreateFileLines("CVS/Entries",
		"/file.mk//modified//")
	filename := t.File("file.mk")

	G.checkExecutable(filename, 0555)

	// See the "Too late" comment in Pkglint.checkExecutable.
	t.CheckOutputEmpty()
}

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

	mkline := t.NewMkLine("dummy.mk", 123, "DUMMY=\tvalue")
	mklines := t.NewMkLines("Makefile", MkCvsID)
	global := G.Pkgsrc.Tools.Define("tool", "TOOL", mkline)
	local := mklines.Tools.Define("tool", "TOOL", mkline)

	global.Validity = Nowhere
	local.Validity = AtRunTime

	loadTimeTool, loadTimeUsable := G.Tool(mklines, "tool", LoadTime)
	runTimeTool, runTimeUsable := G.Tool(mklines, "tool", RunTime)

	t.CheckEquals(loadTimeTool, local)
	t.CheckEquals(loadTimeUsable, false)
	t.CheckEquals(runTimeTool, local)
	t.CheckEquals(runTimeUsable, true)
}

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

	mklines := t.NewMkLines("Makefile", MkCvsID)
	t.SetUpTool("tool", "", Nowhere)

	loadTimeTool, loadTimeUsable := G.Tool(mklines, "tool", LoadTime)
	runTimeTool, runTimeUsable := G.Tool(mklines, "tool", RunTime)

	// The tool is returned even though it cannot be used at the moment.
	// The calling code must explicitly check for usability.

	t.CheckEquals(loadTimeTool.String(), "tool:::Nowhere")
	t.CheckEquals(loadTimeUsable, false)
	t.CheckEquals(runTimeTool.String(), "tool:::Nowhere")
	t.CheckEquals(runTimeUsable, false)
}

// TODO: Document the purpose of this test.
func (s *Suite) Test_Pkglint_Tool__lookup_by_varname(c *check.C) {
	t := s.Init(c)

	mkline := t.NewMkLine("dummy.mk", 123, "DUMMY=\tvalue")
	mklines := t.NewMkLines("Makefile", MkCvsID)
	global := G.Pkgsrc.Tools.Define("tool", "TOOL", mkline)
	local := mklines.Tools.Define("tool", "TOOL", mkline)

	global.Validity = Nowhere
	local.Validity = AtRunTime

	loadTimeTool, loadTimeUsable := G.Tool(mklines, "${TOOL}", LoadTime)
	runTimeTool, runTimeUsable := G.Tool(mklines, "${TOOL}", RunTime)

	t.CheckEquals(loadTimeTool, local)
	t.CheckEquals(loadTimeUsable, false)
	t.CheckEquals(runTimeTool, local)
	t.CheckEquals(runTimeUsable, true)
}

// TODO: Document the purpose of this test.
func (s *Suite) Test_Pkglint_Tool__lookup_by_varname_fallback(c *check.C) {
	t := s.Init(c)

	mklines := t.NewMkLines("Makefile", MkCvsID)
	G.Pkgsrc.Tools.def("tool", "TOOL", false, Nowhere, nil)

	loadTimeTool, loadTimeUsable := G.Tool(mklines, "${TOOL}", LoadTime)
	runTimeTool, runTimeUsable := G.Tool(mklines, "${TOOL}", RunTime)

	t.CheckEquals(loadTimeTool.String(), "tool:TOOL::Nowhere")
	t.CheckEquals(loadTimeUsable, false)
	t.CheckEquals(runTimeTool.String(), "tool:TOOL::Nowhere")
	t.CheckEquals(runTimeUsable, false)
}

// TODO: Document the purpose of this test.
func (s *Suite) Test_Pkglint_Tool__lookup_by_varname_fallback_runtime(c *check.C) {
	t := s.Init(c)

	mklines := t.NewMkLines("Makefile", MkCvsID)
	G.Pkgsrc.Tools.def("tool", "TOOL", false, AtRunTime, nil)

	loadTimeTool, loadTimeUsable := G.Tool(mklines, "${TOOL}", LoadTime)
	runTimeTool, runTimeUsable := G.Tool(mklines, "${TOOL}", RunTime)

	t.CheckEquals(loadTimeTool.String(), "tool:TOOL::AtRunTime")
	t.CheckEquals(loadTimeUsable, false)
	t.CheckEquals(runTimeTool.String(), "tool:TOOL::AtRunTime")
	t.CheckEquals(runTimeUsable, true)
}

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

	mkline := t.NewMkLine("dummy.mk", 123, "DUMMY=\tvalue")
	mklines := t.NewMkLines("Makefile", MkCvsID)
	global := G.Pkgsrc.Tools.Define("tool", "TOOL", mkline)
	local := mklines.Tools.Define("tool", "TOOL", mkline)

	global.Validity = Nowhere
	local.Validity = AtRunTime

	t.CheckEquals(G.ToolByVarname(mklines, "TOOL"), local)
}

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

	mklines := t.NewMkLines("Makefile", MkCvsID)
	G.Pkgsrc.Tools.def("tool", "TOOL", false, AtRunTime, nil)

	t.CheckEquals(G.ToolByVarname(mklines, "TOOL").String(), "tool:TOOL::AtRunTime")
}

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

	t.CreateFileLines("CVS/Entries",
		"/invalid/",
		"must be silently ignored",
		"/name/revision/timestamp/options/tagdate")

	t.CheckEquals(isCommitted(t.File("name")), true)

	t.CheckOutputLines(
		"ERROR: ~/CVS/Entries:1: Invalid line: /invalid/")
}

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

	t.CreateFileLines("CVS/Entries",
		"/invalid/",
		"must be silently ignored",
		"/name//modified//",
		"/removed//modified//")

	t.CreateFileLines("CVS/Entries.Log",
		"A /invalid/",
		"A /added//modified//",
		"must be silently ignored",
		"R /invalid/",
		"R /removed//modified//")

	t.CheckEquals(isCommitted(t.File("name")), true)
	t.CheckEquals(isCommitted(t.File("added")), true)
	t.CheckEquals(isCommitted(t.File("removed")), false)

	t.CheckOutputLines(
		"ERROR: ~/CVS/Entries:1: Invalid line: /invalid/",
		"ERROR: ~/CVS/Entries.Log:1: Invalid line: A /invalid/",
		"ERROR: ~/CVS/Entries.Log:4: Invalid line: R /invalid/")
}

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

	t.SetUpPackage("category/package1",
		"PKGNAME=\t${DISTNAME:@v@${v}@}") // Make the package name non-obvious.
	t.SetUpPackage("category/package2",
		"PKGNAME=\t${DISTNAME:@v@${v}@}") // Make the package name non-obvious.
	t.CreateFileDummyBuildlink3("category/package1/buildlink3.mk")
	t.Copy("category/package1/buildlink3.mk", "category/package2/buildlink3.mk")
	t.Chdir(".")
	t.FinishSetUp()

	G.InterPackage.Enable()
	G.Check("category/package1")
	G.Check("category/package2")

	t.CheckOutputLines(
		"ERROR: category/package2/buildlink3.mk:3: Duplicate package identifier " +
			"\"package1\" already appeared in ../../category/package1/buildlink3.mk:3.")
}