summaryrefslogtreecommitdiff
path: root/tests/thelp.pl
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2019-10-12 16:22:01 -0400
committerPaul Smith <psmith@gnu.org>2019-12-16 09:31:31 -0500
commit3822f77c1d10f4c0789a0262aab35adb00c8d22d (patch)
tree2509783777ca88606d596b3c669ec67614f8e1d9 /tests/thelp.pl
parent1cf3932a39057789ca525afb02bcfc823b238cb7 (diff)
downloadmake-git-3822f77c1d10f4c0789a0262aab35adb00c8d22d.tar.gz
Rename jhelp.pl to thelp.pl and make it a generic test helper.
* tests/thelp.pl: Rename from tests/jhelp.pl. (op): Use names instead of options for the operations. (op): Add new operations for sleep, mkdir, and rm. (op): Enhance wait to time out * tests/run_make_tests.pl: Add a new #HELPER# replacement (subst_make_string): Use fully-qualified path to thelp.pl * tests/scripts/features/parallelism: Update to use thelp.pl and the new named operations. Use thelp.pl sleep instead of system-specific sleep commands. * tests/scripts/features/output-sync: Update to use thelp.pl instead of complex shell scripts. * Makefile.am: Distribute tests/thelp.pl instead of tests/jhelp.pl
Diffstat (limited to 'tests/thelp.pl')
-rwxr-xr-xtests/thelp.pl113
1 files changed, 113 insertions, 0 deletions
diff --git a/tests/thelp.pl b/tests/thelp.pl
new file mode 100755
index 00000000..d8aaa667
--- /dev/null
+++ b/tests/thelp.pl
@@ -0,0 +1,113 @@
+#!/usr/bin/env perl
+# -*-perl-*-
+#
+# This script helps us write tests in a portable way, without relying on a lot
+# of shell features. Since we already have Perl to run the tests, use that.
+#
+# The arguments represent a set of steps that will be run one at a time.
+# Each step consists of an operator and argument.
+#
+# It supports the following operators:
+# out <word> : echo <word> to stdout
+# file <word> : echo <word> to stdout AND create the file <word>
+# dir <word> : echo <word> to stdout AND create the directory <word>
+# rm <word> : echo <word> to stdout AND delete the file/directory <word>
+# wait <word> : wait for a file named <word> to exist
+# tmout <secs> : Change the timeout for waiting. Default is 4 seconds.
+# sleep <secs> : Sleep for <secs> seconds then echo <secs>
+# fail <err> : echo <err> to stdout then exit with error code err
+#
+# If given -q only the "out" command generates output.
+
+# Force flush
+$| = 1;
+
+my $quiet = 0;
+my $timeout = 4;
+
+sub op {
+ my ($op, $nm) = @_;
+
+ defined $nm or die "Missing value for $op\n";
+
+ if ($op eq 'out') {
+ print "$nm\n";
+ return 1;
+ }
+
+ # Show the output before creating the file
+ if ($op eq 'file') {
+ print "file $nm\n" unless $quiet;
+ open(my $fh, '>', $nm) or die "$nm: open: $!\n";
+ close(my $fh);
+ return 1;
+ }
+
+ # Show the output before creating the directory
+ if ($op eq 'dir') {
+ print "dir $nm\n" unless $quiet;
+ mkdir($nm) or die "$nm: mkdir: $!\n";
+ return 1;
+ }
+
+ # Show the output after removing the file
+ if ($op eq 'rm') {
+ if (-f $nm) {
+ unlink($nm) or die "$nm: unlink: $!\n";
+ } elsif (-d $nm) {
+ rmdir($nm) or die "$nm: rmdir: $!\n";
+ } else {
+ die "$nm: not file or directory: $!\n";
+ }
+ print "rm $nm\n" unless $quiet;
+ return 1;
+ }
+
+ if ($op eq 'tmout') {
+ $timeout = $nm;
+ print "tmout $nm\n" unless $quiet;
+ return 1;
+ }
+
+ # Show the output after the file exists
+ if ($op eq 'wait') {
+ my $start = time();
+ my $end = $start + $timeout;
+ while (time() <= $end) {
+ if (-f $nm) {
+ print "wait $nm\n" unless $quiet;
+ return 1;
+ }
+ select(undef, undef, undef, 0.1);
+ }
+ die "wait $nm: timeout after ".(time()-$start-1)." seconds\n";
+ }
+
+ # Show the output after sleeping
+ if ($op eq 'sleep') {
+ sleep($nm);
+ print "sleep $nm\n" unless $quiet;
+ return 1;
+ }
+
+ if ($op eq 'fail') {
+ print "fail $nm\n";
+ exit($nm);
+ }
+
+ die("Invalid command: $op $nm\n");
+}
+
+if (@ARGV && $ARGV[0] eq '-q') {
+ $quiet = 1;
+ shift;
+}
+
+while (@ARGV) {
+ if (op($ARGV[0], $ARGV[1])) {
+ shift;
+ shift;
+ }
+}
+
+exit(0);