-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkglint_test.go
1591 lines (1244 loc) · 46.7 KB
/
pkglint_test.go
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
package pkglint
import (
"gopkg.in/check.v1"
"os"
"path"
"path/filepath"
"strings"
)
func (p *Pkglint) isUsable() bool { return p.fileCache != nil }
func (s *Suite) Test_Pkglint_Main(c *check.C) {
t := s.Init(c)
out, err := os.Create(t.CreateFileLines("out").String())
t.CheckNil(err)
outProfiling, err := os.Create(t.CreateFileLines("out.profiling").String())
t.CheckNil(err)
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", ".")
t.CheckNil(out.Close())
t.CheckNil(outProfiling.Close())
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",
" -n, --network enable checks that need network access",
" -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",
" error treat warnings as errors (disabled)",
" extra enable some extra warnings (disabled)",
" perm warn about unforeseen variable definition and use (disabled)",
" quoting warn about quoting 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/DESCR",
"Description")
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.",
"",
"--- checkperms.c",
"+++ 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,
"",
"BLAKE2s (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/TODO: Packages in main pkgsrc must not have a TODO file.",
"ERROR: ~/sysutils/checkperms/distinfo:6: SHA1 hash of patches/patch-checkperms.c differs "+
"(distinfo has asdfasdf, patch file has bcfb79696cb6bf4d2222a6d78a530e11bf1c0cea).",
"WARN: ~/sysutils/checkperms/patches/patch-checkperms.c:12: Premature end of patch hunk "+
"(expected 1 line to be deleted and 0 lines to be added).",
"3 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 + "\" above 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 -p -s -e" \
// gobco \
// -test=-test.covermode=count
// -test=-test.coverprofile="C:/Users/rillig/go/src/github.com/rillig/pkglint/v23/stats-go.txt"
// -test=-timeout=3600s \
// -test=-check.f="^Test_Pkglint_Main__realistic" \
// -stats="stats-gobco.json" \
// > out
//
// Note that the path to -test.coverprofile must be absolute, since gobco
// runs "go test" in a temporary directory.
//
// See https://github.com/rillig/gobco for the tool to measure the branch coverage.
func (s *Suite) Test_Pkglint_Main__realistic(c *check.C) {
t := s.Init(c)
if cwd := os.Getenv("PKGLINT_TESTDIR"); cwd != "" {
err := os.Chdir(cwd)
t.AssertNil(err)
}
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(NewCurrPath("pkglint.pprof").IsFile(), true)
err := os.Remove("pkglint.pprof")
t.CheckNil(err)
// 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: .*`)
}
// The option -Wall does not imply -Werror.
func (s *Suite) Test_Pkglint_Main__Wall(c *check.C) {
t := s.Init(c)
t.SetUpPackage("category/package",
"UNUSED=\tvalue")
t.Chdir("category/package")
exitcode := t.Main("-Wall")
t.CheckEquals(exitcode, 0)
t.CheckOutputLines(
"WARN: Makefile:20: Variable \"UNUSED\" is defined but not used.",
"1 warning found.",
"(Run \"pkglint -e -Wall\" to show explanations.)")
}
func (s *Suite) Test_Pkglint_Main__Werror_no_warning(c *check.C) {
t := s.Init(c)
t.SetUpPackage("category/package")
t.Chdir("category/package")
exitcode := t.Main("-Werror")
t.CheckEquals(exitcode, 0)
t.CheckOutputLines(
"Looks fine.")
}
func (s *Suite) Test_Pkglint_Main__Werror_warning(c *check.C) {
t := s.Init(c)
t.SetUpPackage("category/package",
"UNUSED=\tvalue")
t.Chdir("category/package")
exitcode := t.Main("-Werror")
t.CheckEquals(exitcode, 1)
t.CheckOutputLines(
"WARN: Makefile:20: Variable \"UNUSED\" is defined but not used.",
"1 warning found.",
"(Run \"pkglint -e -Werror\" to show explanations.)")
}
func (s *Suite) Test_Pkglint_Main__Werror_error(c *check.C) {
t := s.Init(c)
t.SetUpPackage("category/package",
"DEPENDS+=\tother>=0:../../category/does-not-exist")
t.Chdir("category/package")
exitcode := t.Main("-Werror")
t.CheckEquals(exitcode, 1)
t.CheckOutputLines(
"ERROR: Makefile:20: Relative path "+
"\"../../category/does-not-exist/Makefile\" "+
"does not exist.",
"1 error found.")
}
// 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.Logger.Opts.Only, []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 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 character \"U+0005\".")
G.Check(t.File("category"))
t.CheckOutputLines(
"WARN: ~/category/Makefile:3: Line contains invalid character \"U+0007\".",
"WARN: ~/category/Makefile:3: COMMENT contains invalid character \"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", "--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/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__incomplete_package(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.",
"ERROR: Makefile: Each package must define its LICENSE.",
"WARN: Makefile: Each package should define a COMMENT.",
"ERROR: Makefile: Each package must have a DESCR file.")
}
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.",
"ERROR: Makefile: Each package must have a DESCR file.")
}
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 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",
"PATCHDIR=\t${RUBY_PKGDIR}/patches")
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)
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.",
"WARN: ~/category/package/Makefile:20: DISTINFO_FILE \"nonexistent\" has no corresponding PATCHDIR.")
}
// 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_resolveExprs__circular_reference(c *check.C) {
t := s.Init(c)
mkline := t.NewMkLine("filename.mk", 1, "VAR=\t1:${VAR}+ 2:${VAR}")
pkg := NewPackage(t.File("category/pkgbase"))
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 := resolveExprs("the a:${VAR} b:${VAR}", nil, pkg)
// 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_resolveExprs__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")
pkg := NewPackage(t.File("category/pkgbase"))
pkg.vars.Define("FIRST", mkline1)
pkg.vars.Define("SECOND", mkline2)
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 := resolveExprs("you ${FIRST}", nil, pkg)
t.CheckEquals(resolved, "you got it")
}
func (s *Suite) Test_resolveExprs__scope_precedence(c *check.C) {
t := s.Init(c)
mklines := t.NewMkLines("filename.mk",
MkCvsID,
"ORIGIN=\tfilename.mk")
mklines.collectVariables(false, false)
pkg := NewPackage(t.File("category/package"))
pkg.vars.Define("ORIGIN", t.NewMkLine("other.mk", 123, "ORIGIN=\tpackage"))
resolved := resolveExprs("From ${ORIGIN}", mklines, pkg)
t.CheckEquals(resolved, "From filename.mk")
}
// 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_resolveExprs__special_chars(c *check.C) {
t := s.Init(c)
mkline := t.NewMkLine("filename.mk", 10, "_=x11")
pkg := NewPackage(t.File("category/pkg"))
pkg.vars.Define("GST_PLUGINS0.10_TYPE", mkline)
resolved := resolveExprs("gst-plugins0.10-${GST_PLUGINS0.10_TYPE}/distinfo", nil, pkg)
t.CheckEquals(resolved, "gst-plugins0.10-x11/distinfo")
}
func (s *Suite) Test_resolveExprs__indeterminate(c *check.C) {
t := s.Init(c)
pkg := NewPackage(G.Pkgsrc.File("category/package"))
pkg.vars.Define("PKGVAR", t.NewMkLine("filename.mk", 123, "PKGVAR!=\tcommand"))
mklines := t.NewMkLinesPkg("filename.mk", pkg,
"VAR!=\tcommand")
mklines.collectVariables(false, false)
resolved := resolveExprs("${VAR} ${PKGVAR}", mklines, nil)
// VAR and PKGVAR are defined, but since they contain the result of
// a shell command, their value is indeterminate.
// Therefore, they are not replaced.
t.CheckEquals(resolved, "${VAR} ${PKGVAR}")
}
// 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 like \"${PREFIX}\" are not expanded in the DESCR file.",
"WARN: DESCR:25: File too long (should be no more than 24 lines).")
}
// The package author may think that variables like ${PREFIX}
// are expanded in DESCR files too, but that doesn't happen.
func (s *Suite) Test_CheckLinesDescr__variables(c *check.C) {
t := s.Init(c)
t.SetUpVartypes()
test := func(text string, diagnostics ...string) {
lines := t.NewLines("DESCR",
text)
CheckLinesDescr(lines)
t.CheckOutput(diagnostics)
}
test("${PREFIX}",
"NOTE: DESCR:1: Variables like \"${PREFIX}\" "+
"are not expanded in the DESCR file.")
// Variables in parentheses are unusual in pkgsrc.
// Therefore, they are not worth being mentioned.
test("$(PREFIX)", nil...)
// 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.
test("${UNDEFINED}", nil...)
test("$<", nil...)
// This one occurs in a few Perl packages.
test("$@", nil...)
}
func (s *Suite) Test_CheckLinesDescr__TODO(c *check.C) {
t := s.Init(c)
t.SetUpVartypes()
test := func(text string, diagnostics ...string) {
lines := t.NewLines("DESCR",
text)
CheckLinesDescr(lines)
t.CheckOutput(diagnostics)
}
// See pkgtools/url2pkg/files/url2pkg.py
test("TODO: Fill in a short description of the package",
"ERROR: DESCR:1: DESCR files must not have TODO lines.")
test("TODO-list is an organizer for everything you need to do, now or later",
nil...)
}
func (s *Suite) Test_CheckLinesMessage__one_line_of_text(c *check.C) {
t := s.Init(c)
lines := t.NewLines("MESSAGE",
"one line")
CheckLinesMessage(lines, nil)
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, nil)
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, nil)
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, nil)
t.CheckOutputLines(
"AUTOFIX: ~/MESSAGE:1: Inserting a line \"=============================="+
"=============================================\" above this line.",
"AUTOFIX: ~/MESSAGE:1: Inserting a line \"$"+"NetBSD$\" above this line.",
"AUTOFIX: ~/MESSAGE:5: Inserting a line \"=============================="+
"=============================================\" below this line.")
t.CheckFileLines("MESSAGE",
"===========================================================================",
CvsID,
"1",
"2",
"3",
"4",
"5",
"===========================================================================")
}
func (s *Suite) Test_CheckLinesMessage__common(c *check.C) {
t := s.Init(c)