Skip to content

Commit c7ce70a

Browse files
tboegigitster
authored andcommitted
test: Add check-non-portable-shell.pl
Add the perl script "check-non-portable-shell.pl" to detect non-portable shell syntax. "echo -n" is an example of a shell command working on Linux, but not on Mac OS X. These shell commands are checked and reported as error: - "echo -n" (printf should be used) - "sed -i" (GNUism; use a temp file instead) - "declare" (bashism, often used with arrays) - "which" (unreliable exit status and output; use type instead) - "test a == b" (bashism for "test a = b") "make test-lint-shell-syntax" can be used to run only the check. Helped-By: Jeff King <[email protected]> Signed-off-by: Torsten Bögershausen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d41784 commit c7ce70a

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
@@ -16,6 +16,7 @@ DEFAULT_TEST_TARGET ?= test
1616

1717
# Shell quote;
1818
SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
19+
PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))
1920

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

46-
test-lint: test-lint-duplicates test-lint-executable
47+
test-lint: test-lint-duplicates test-lint-executable test-lint-shell-syntax
4748

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

59+
test-lint-shell-syntax:
60+
@'$(PERL_PATH_SQ)' check-non-portable-shell.pl $(T)
61+
5862
aggregate-results-and-cleanup: $(T)
5963
$(MAKE) aggregate-results
6064
$(MAKE) clean
@@ -87,7 +91,7 @@ test-results:
8791
mkdir -p test-results
8892

8993
test-results/git-smoke.tar.gz: test-results
90-
$(PERL_PATH) ./harness \
94+
'$(PERL_PATH_SQ)' ./harness \
9195
--archive="test-results/git-smoke.tar.gz" \
9296
$(T)
9397

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)