Testsuite: Try harder to locate the tools
authorHeiko Schlittermann (HS12-RIPE) <hs@schlittermann.de>
Thu, 18 Jan 2018 21:55:15 +0000 (22:55 +0100)
committerHeiko Schlittermann (HS12-RIPE) <hs@schlittermann.de>
Thu, 18 Jan 2018 21:55:15 +0000 (22:55 +0100)
test/Makefile.in
test/src/locate.pl [new file with mode: 0644]

index 0f229a2cc24f253f7eecc937f03a679d5842d5fd..edcc4ab78c6eb416ce9a2cdb66f78641744e4cee 100644 (file)
@@ -27,7 +27,7 @@ all:            binaries sysbinaries
 binaries:      $(BINARIES)
 
 sysbinaries:   FORCE binaries
-               sh -x bin/locate initdb postgres pg_ctl mysqld
+               bin/locate initdb postgres pg_ctl mysqld
                ls -la bin.sys
 
 # Compile and link the programs:
@@ -82,7 +82,7 @@ bin/showids:    $(SRC)/showids.c Makefile
                $(CC) $(CFLAGS) $(LDFLAGS) -o bin/showids $(SRC)/showids.c
 
 bin/locate:     $(SRC)/locate.sh Makefile
-               cp $(SRC)/locate.sh bin/locate
+               cp $(SRC)/locate.pl bin/locate
                chmod 0755 bin/locate
 
 clean:;         rm -rf $(BINARIES) bin.sys
diff --git a/test/src/locate.pl b/test/src/locate.pl
new file mode 100644 (file)
index 0000000..6f752c1
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use File::Find;
+use Cwd;
+
+my @dirs = grep { /^\// && -d } split(/:/, $ENV{PATH}), qw(
+  /bin
+  /usr/bin
+  /usr/sbin
+  /usr/libexec
+  /usr/local/bin
+  /usr/local/sbin
+  /usr/local
+  /opt
+);
+
+my %path = map { $_ => locate($_, @dirs) } @ARGV;
+
+mkdir 'bin.sys'
+  or die "bin.sys: $!"
+  if not -d 'bin.sys';
+
+foreach my $tool (keys %path) {
+    next if not defined $path{$tool};
+    print "$tool $path{$tool}\n";
+
+    unlink "bin.sys/$tool";
+    symlink $path{$tool}, "bin.sys/$tool"
+      or warn "bin.sys/$tool -> $path{$tool}: $!\n";
+}
+
+sub locate {
+    my ($tool, @dirs) = @_;
+
+    # use die to break out of the find as soon
+    # as we found it
+    my $cwd = cwd;
+    eval {
+        find(
+            sub {
+                return unless $tool eq $_ and -x $_ and -f _;
+                die { found => $File::Find::name };
+            },
+            @dirs
+        );
+    };
+    chdir $cwd;
+
+    return (ref $@ eq ref {} and $@->{found}) ? $@->{found} : undef;
+}