summaryrefslogtreecommitdiff
path: root/pkgtools
diff options
context:
space:
mode:
authorrillig <rillig@pkgsrc.org>2022-08-03 16:15:49 +0000
committerrillig <rillig@pkgsrc.org>2022-08-03 16:15:49 +0000
commit029faa5cdeeb1ef26462aa77e9092ad21919a109 (patch)
treeef595d5331441edfe90d3fd036a34bfbb253f5b3 /pkgtools
parentfe44f63e3a68e4d55a88f55325c72a9573b92650 (diff)
downloadpkgsrc-029faa5cdeeb1ef26462aa77e9092ad21919a109.tar.gz
lintpkgsrc: test parsing of package makefiles
Diffstat (limited to 'pkgtools')
-rw-r--r--pkgtools/lintpkgsrc/Makefile4
-rwxr-xr-xpkgtools/lintpkgsrc/files/lintpkgsrc.pl11
-rw-r--r--pkgtools/lintpkgsrc/files/t/glob.t47
-rw-r--r--pkgtools/lintpkgsrc/files/t/parse_makefile.t56
-rw-r--r--pkgtools/lintpkgsrc/files/t/pkgversion.t3
5 files changed, 118 insertions, 3 deletions
diff --git a/pkgtools/lintpkgsrc/Makefile b/pkgtools/lintpkgsrc/Makefile
index d058ed059de..35b16eb9438 100644
--- a/pkgtools/lintpkgsrc/Makefile
+++ b/pkgtools/lintpkgsrc/Makefile
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.43 2022/07/30 16:20:18 rillig Exp $
+# $NetBSD: Makefile,v 1.44 2022/08/03 16:15:49 rillig Exp $
PKGNAME= lintpkgsrc-4.99
CATEGORIES= pkgtools
@@ -9,6 +9,7 @@ COMMENT= Sanity checks on the complete pkgsrc tree
DEPENDS+= digest>=20010101:../../pkgtools/digest
TEST_DEPENDS+= p5-Capture-Tiny>=0:../../devel/p5-Capture-Tiny
+TEST_DEPENDS+= p5-File-Slurp>=0:../../devel/p5-File-Slurp
CONFLICTS+= pkglint<4.82
USE_TOOLS+= perl:run
@@ -37,6 +38,7 @@ do-build:
do-test:
${RUN} cd ${WRKSRC}/t; \
for test in ./*.t; do \
+ echo "Testing $${test#./}"; \
perl "$$test"; \
done
diff --git a/pkgtools/lintpkgsrc/files/lintpkgsrc.pl b/pkgtools/lintpkgsrc/files/lintpkgsrc.pl
index 722d0d5f2d6..1d4f46a822d 100755
--- a/pkgtools/lintpkgsrc/files/lintpkgsrc.pl
+++ b/pkgtools/lintpkgsrc/files/lintpkgsrc.pl
@@ -1,6 +1,6 @@
#!@PERL5@
-# $NetBSD: lintpkgsrc.pl,v 1.42 2022/07/30 18:20:23 rillig Exp $
+# $NetBSD: lintpkgsrc.pl,v 1.43 2022/08/03 16:15:49 rillig Exp $
# Written by David Brownlee <abs@netbsd.org>.
#
@@ -361,6 +361,8 @@ sub pkgversioncmp($$$) {
eval "$cmp $test 0";
}
+# Return a copy of $line in which trivial variable expressions are replaced
+# with the variable values.
sub parse_expand_vars($$) {
my ($line, $vars) = @_;
@@ -1807,4 +1809,11 @@ sub main() {
}
}
+if (caller()) {
+ # To allow easy testing of the code.
+ # TODO: reduce the use of global variables, or make them accessible
+ # to the tests.
+ $default_vars = {};
+}
+
main() unless caller();
diff --git a/pkgtools/lintpkgsrc/files/t/glob.t b/pkgtools/lintpkgsrc/files/t/glob.t
new file mode 100644
index 00000000000..0472d331b55
--- /dev/null
+++ b/pkgtools/lintpkgsrc/files/t/glob.t
@@ -0,0 +1,47 @@
+# $NetBSD: glob.t,v 1.1 2022/08/03 16:15:49 rillig Exp $
+
+use strict;
+use warnings;
+use Test;
+
+BEGIN { plan tests => 12; }
+
+require('../lintpkgsrc.pl');
+
+sub test_glob2regex() {
+
+ ok(glob2regex('*'), '^.*$');
+
+ ok(glob2regex('?'), '^.$');
+
+ # The '' means that the regular expression equals the glob.
+ ok(glob2regex('[a-z]'), '');
+
+ # The '' means that the regular expression equals the glob.
+ ok(glob2regex('[a-z0-9]'), '');
+
+ # The '' means that the regular expression equals the glob.
+ ok(glob2regex('[a-z0-9_]'), '');
+
+ ok(glob2regex('*.[ch]'), '^.*\.[ch]$');
+
+ ok(glob2regex('{one,two}'), '^(one|two)$');
+
+ ok(glob2regex('{{thi,fou}r,fif}teen'), '^((thi|fou)r|fif)teen$');
+
+ # There is an unbalanced '}' at the very end.
+ ok(glob2regex('{{thi,fou}r,fif}teen}'), undef);
+
+ # XXX: Why is '+' turned into '.'?
+ ok(glob2regex('a+b|c'), '^a.b\|c$');
+
+ # XXX: Typo in the code, the case '\\+' is unreachable.
+ # Escaping the backslash works nevertheless.
+ ok(glob2regex('a\[b*'), '^a\[b.*$');
+
+ # XXX: Depending on the exact implementation, the '\n' may be
+ # interpreted as a newline, a literal 'n' or a literal '\' 'n'.
+ ok(glob2regex('a\n*'), '^a\n.*$');
+}
+
+test_glob2regex();
diff --git a/pkgtools/lintpkgsrc/files/t/parse_makefile.t b/pkgtools/lintpkgsrc/files/t/parse_makefile.t
new file mode 100644
index 00000000000..37b4e6b9581
--- /dev/null
+++ b/pkgtools/lintpkgsrc/files/t/parse_makefile.t
@@ -0,0 +1,56 @@
+# $NetBSD: parse_makefile.t,v 1.1 2022/08/03 16:15:49 rillig Exp $
+
+use strict;
+use warnings;
+use File::Slurp;
+use File::Temp;
+use Test;
+
+BEGIN { plan tests => 6; }
+
+require('../lintpkgsrc.pl');
+
+sub test_parse_expand_vars() {
+ my %vars = (
+ CFLAGS => '${CFLAGS_OPT} ${CFLAGS_WARN} ${CFLAGS_ERR}',
+ CFLAGS_WARN => '${CFLAGS_WARN_ALL}',
+ CFLAGS_OPT => '-Os',
+ CFLAGS_ERR => '${CFLAGS_WARN_ALL:M*error=*}',
+ );
+
+ my $cflags = parse_expand_vars('<${CFLAGS}>', \%vars);
+
+ ok($cflags, '<-Os M_a_G_i_C_uNdEfInEd ${CFLAGS_WARN_ALL:M*error=*}>')
+}
+
+sub test_parse_makefile_vars() {
+ my $dir = File::Temp->newdir();
+ my $file = "$dir/filename.mk";
+
+ write_file($file,
+ "# comment\n",
+ "VAR=\tvalue\n",
+ "COMMENT=\tvalue#comment\n",
+ "MULTI=\tone\\\n",
+ "\ttwo\\\n",
+ "three#comment\n"
+ );
+
+ my $vars = parse_makefile_vars($file, undef);
+
+ ok(
+ join(', ', sort keys %$vars),
+ '.CURDIR, BSD_PKG_MK, COMMENT, MULTI, VAR');
+ ok($vars->{BSD_PKG_MK}, 'YES');
+
+ # FIXME: must be 'value'
+ ok($vars->{COMMENT}, 'valu');
+
+ # FIXME: must be 'one two three'
+ ok($vars->{MULTI}, "on\ttwthree#comment");
+
+ ok($vars->{VAR}, 'value');
+}
+
+test_parse_expand_vars();
+test_parse_makefile_vars();
diff --git a/pkgtools/lintpkgsrc/files/t/pkgversion.t b/pkgtools/lintpkgsrc/files/t/pkgversion.t
index e83a703aaae..651663e6ca2 100644
--- a/pkgtools/lintpkgsrc/files/t/pkgversion.t
+++ b/pkgtools/lintpkgsrc/files/t/pkgversion.t
@@ -1,4 +1,5 @@
-# $NetBSD: pkgversion.t,v 1.2 2022/07/30 17:06:29 rillig Exp $
+# $NetBSD: pkgversion.t,v 1.3 2022/08/03 16:15:49 rillig Exp $
+
use strict;
use warnings;
use Test;