Skip to content

Commit 4249d85

Browse files
committed
Merge branch 'tb/test-shell-lint'
Check for common mistakes in the test scripts, based on simple pattern-matching. * tb/test-shell-lint: test: Add check-non-portable-shell.pl
2 parents 6a37cee + c7ce70a commit 4249d85

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

t/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ TEST_LINT ?= test-lint-duplicates test-lint-executable
1717

1818
# Shell quote;
1919
SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
20+
PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))
2021

2122
T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh))
2223
TSVN = $(sort $(wildcard t91[0-9][0-9]-*.sh))
@@ -44,7 +45,7 @@ clean-except-prove-cache:
4445
clean: clean-except-prove-cache
4546
$(RM) .prove
4647

47-
test-lint: test-lint-duplicates test-lint-executable
48+
test-lint: test-lint-duplicates test-lint-executable test-lint-shell-syntax
4849

4950
test-lint-duplicates:
5051
@dups=`echo $(T) | tr ' ' '\n' | sed 's/-.*//' | sort | uniq -d` && \
@@ -56,6 +57,9 @@ test-lint-executable:
5657
test -z "$$bad" || { \
5758
echo >&2 "non-executable tests:" $$bad; exit 1; }
5859

60+
test-lint-shell-syntax:
61+
@'$(PERL_PATH_SQ)' check-non-portable-shell.pl $(T)
62+
5963
aggregate-results-and-cleanup: $(T)
6064
$(MAKE) aggregate-results
6165
$(MAKE) clean
@@ -88,7 +92,7 @@ test-results:
8892
mkdir -p test-results
8993

9094
test-results/git-smoke.tar.gz: test-results
91-
$(PERL_PATH) ./harness \
95+
'$(PERL_PATH_SQ)' ./harness \
9296
--archive="test-results/git-smoke.tar.gz" \
9397
$(T)
9498

t/check-non-portable-shell.pl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/perl
2+
3+
# Test t0000..t9999.sh for non portable shell scripts
4+
# This script can be called with one or more filenames as parameters
5+
6+
use strict;
7+
use warnings;
8+
9+
my $exit_code=0;
10+
11+
sub err {
12+
my $msg = shift;
13+
print "$ARGV:$.: error: $msg: $_\n";
14+
$exit_code = 1;
15+
}
16+
17+
while (<>) {
18+
chomp;
19+
/^\s*sed\s+-i/ and err 'sed -i is not portable';
20+
/^\s*echo\s+-n/ and err 'echo -n is not portable (please use printf)';
21+
/^\s*declare\s+/ and err 'arrays/declare not portable';
22+
/^\s*[^#]\s*which\s/ and err 'which is not portable (please use type)';
23+
/test\s+[^=]*==/ and err '"test a == b" is not portable (please use =)';
24+
# this resets our $. for each file
25+
close ARGV if eof;
26+
}
27+
exit $exit_code;

0 commit comments

Comments
 (0)