git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Torsten Bögershausen" <tboegi@web.de>
To: git@vger.kernel.org
Cc: tboegi@web.de
Subject: [PATCH 1/4] test: Add target test-lint-shell-syntax
Date: Tue, 1 Jan 2013 22:40:08 +0100	[thread overview]
Message-ID: <201301012240.10722.tboegi@web.de> (raw)

Add the perl script "check-non-portable-shell.pl" to detect non-portable
shell syntax
Many systems use gnu tools which accept an extended syntax in shell scripts,
which is not portable on all systems and causes the test suite to fail.

To prevent contributors using e.g. Linux to add non-portable test code,
"check-non-portable-shell.pl" is run as part of
"make test" or "make in the t/ directory.

"echo -n" is an example of a statement working on Linux,
but not on e.g. Mac OS X.

Beside "echo -n" we check for
"sed -i",
arrays in shell scripts (declare statement),
"which" (use type instead),
or "==" (bash style of =)

Signed-off-by: Torsten Bögershausen <tboegi@web.de>
---
 t/Makefile                    |  7 +++--
 t/check-non-portable-shell.pl | 67 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100755 t/check-non-portable-shell.pl

diff --git a/t/Makefile b/t/Makefile
index 88e289f..7b0c4dc 100644
--- a/t/Makefile
+++ b/t/Makefile
@@ -23,7 +23,7 @@ TGITWEB = $(sort $(wildcard t95[0-9][0-9]-*.sh))
 
 all: $(DEFAULT_TEST_TARGET)
 
-test: pre-clean $(TEST_LINT)
+test: pre-clean test-lint-shell-syntax $(TEST_LINT)
 	$(MAKE) aggregate-results-and-cleanup
 
 prove: pre-clean $(TEST_LINT)
@@ -43,7 +43,7 @@ clean-except-prove-cache:
 clean: clean-except-prove-cache
 	$(RM) .prove
 
-test-lint: test-lint-duplicates test-lint-executable
+test-lint: test-lint-duplicates test-lint-executable test-lint-shell-syntax
 
 test-lint-duplicates:
 	@dups=`echo $(T) | tr ' ' '\n' | sed 's/-.*//' | sort | uniq -d` && \
@@ -55,6 +55,9 @@ test-lint-executable:
 		test -z "$$bad" || { \
 		echo >&2 "non-executable tests:" $$bad; exit 1; }
 
+test-lint-shell-syntax:
+	$(PERL_PATH) check-non-portable-shell.pl $(T)
+
 aggregate-results-and-cleanup: $(T)
 	$(MAKE) aggregate-results
 	$(MAKE) clean
diff --git a/t/check-non-portable-shell.pl b/t/check-non-portable-shell.pl
new file mode 100755
index 0000000..de62ef0
--- /dev/null
+++ b/t/check-non-portable-shell.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl -w
+######################################################################
+# Test t0000..t9999.sh for non portable shell scripts                #
+# Examples are "echo -n" or "sed -i"                                 #
+# This script can be called with one or more filenames as parameters #
+#
+######################################################################
+use strict;
+my $exitcode=0;
+
+sub check_one_file($) {
+	my $lineno=1;
+	my $filename=shift;
+
+	open(FINPUT, "<$filename") || die "Couldn't open filename $filename";
+	my @fdata = <FINPUT>;
+	close(FINPUT);
+
+	while (my $line = shift @fdata) {
+    do {
+      chomp $line;
+      # sed -i
+      if ($line =~ /^\s*sed\s+-i/) {
+        printf("%s:%d:error: \"sed -i not portable\" %s\n", $filename, $lineno, $line);
+				$exitcode=1;
+      }
+      # echo -n
+      if ($line =~ /^\s*echo\s+-n/) {
+        printf("%s:%d:error: \"echo -n not portable\" %s\n", $filename, $lineno, $line);
+				$exitcode=1;
+      }
+      # arrays (declare statement)
+      if ($line =~ /^\s*declare\s+/) {
+        printf("%s:%d:error: \"arrays/declare not portable\" %s\n", $filename, $lineno, $line);
+				$exitcode=1;
+      }
+      # which
+      if ($line =~ /^\s*[^#]\s*which\s/) {
+        printf("%s:%d:error: \"which is not portable (use type)\" %s\n", $filename, $lineno, $line);
+				$exitcode=1;
+      }
+
+      # == (bash style comparison)
+      if ($line =~ /test\s+[^=]*==/) {
+        printf("%s:%d:error: \"== is not portable (use =)\" %s\n", $filename, $lineno, $line);
+				$exitcode=1;
+      }
+
+			$lineno=$lineno+1;
+		}
+  }
+}
+
+
+if ($#ARGV <= 0) {
+	print STDERR  "$0: Check shell scripts for non portable syntax\n";
+	print STDERR  "Example: $0 t[0-9]*.sh\n";
+
+	exit(2);
+}
+
+while (@ARGV) {
+	my $arg = shift @ARGV;
+  check_one_file($arg);
+}
+
+exit($exitcode);
-- 
1.8.0.197.g5a90748

             reply	other threads:[~2013-01-01 21:41 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-01 21:40 Torsten Bögershausen [this message]
2013-01-01 22:07 ` [PATCH 1/4] test: Add target test-lint-shell-syntax Junio C Hamano
2013-01-02  0:14   ` Torsten Bögershausen
2013-01-02  2:22     ` Junio C Hamano
2013-01-02  9:46 ` Jeff King
2013-01-02 16:28   ` Junio C Hamano
2013-01-02 23:14   ` Torsten Bögershausen
2013-01-02 23:22     ` Jeff King
2013-01-02 23:58       ` Torsten Bögershausen
2013-01-03  0:16         ` Junio C Hamano
2013-01-03  0:23           ` Torsten Bögershausen
2013-01-03  2:02             ` Junio C Hamano
2013-01-03  7:17               ` [PATCH] tests: turn on test-lint by default Jeff King
2013-01-03  0:01   ` [PATCH 1/4] test: Add target test-lint-shell-syntax Junio C Hamano
2013-01-03  0:08     ` Junio C Hamano
2013-01-07 17:43       ` Torsten Bögershausen
2013-01-07 18:07         ` Junio C Hamano
2013-01-08  4:11           ` Torsten Bögershausen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201301012240.10722.tboegi@web.de \
    --to=tboegi@web.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).