2023-01-29 21:35:16 +00:00
|
|
|
package TestFunctions;
|
|
|
|
# vi: et sts=4 sw=4 ts=4
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
require Archive::Tar;
|
|
|
|
use Cwd qw/
|
|
|
|
abs_path
|
|
|
|
chdir
|
|
|
|
getcwd
|
|
|
|
/;
|
|
|
|
use File::Basename qw/
|
|
|
|
dirname
|
|
|
|
/;
|
|
|
|
require File::Temp;
|
|
|
|
|
|
|
|
use Exporter;
|
|
|
|
our @ISA = qw/ Exporter /;
|
|
|
|
our @EXPORT = qw/
|
|
|
|
are_hardlinked
|
|
|
|
file_exists
|
|
|
|
filemtime
|
|
|
|
has_mtime
|
|
|
|
mktempdir
|
|
|
|
prep_tar
|
|
|
|
run_script
|
|
|
|
run_script_capture
|
|
|
|
/;
|
|
|
|
|
|
|
|
use constant SCRIPT => $ENV{SCRIPT} // abs_path dirname(__FILE__) . '/../simplify_static_dir-main.pl';
|
|
|
|
|
|
|
|
sub are_hardlinked {
|
2023-07-20 17:37:07 +00:00
|
|
|
my ($starter, @files) = @_;
|
2023-01-29 21:35:16 +00:00
|
|
|
|
|
|
|
my $gen_ident = sub {
|
|
|
|
my ($dev, $ino) = stat $_[0];
|
|
|
|
return "$dev:$ino";
|
|
|
|
};
|
|
|
|
|
2023-07-20 17:41:07 +00:00
|
|
|
my $starter_ident = $gen_ident->($starter);
|
2023-07-20 17:37:07 +00:00
|
|
|
foreach my $file (@files) {
|
2023-07-20 17:41:07 +00:00
|
|
|
if ($gen_ident->($file) ne $starter_ident) {
|
2023-01-29 21:35:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub file_exists {
|
2023-07-20 17:37:07 +00:00
|
|
|
my @files = @_;
|
|
|
|
foreach my $file (@files) {
|
2023-01-29 21:35:16 +00:00
|
|
|
unless (-e $file) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub filemtime {
|
2023-07-20 17:37:07 +00:00
|
|
|
my $file = shift;
|
|
|
|
return (stat $file)[9];
|
2023-01-29 21:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub has_mtime {
|
2023-07-20 17:37:07 +00:00
|
|
|
my ($mtime, @files) = @_;
|
|
|
|
foreach my $file (@files) {
|
2023-07-20 17:41:07 +00:00
|
|
|
if (filemtime($file) != $mtime) {
|
2023-01-29 21:35:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub mktempdir {
|
|
|
|
return File::Temp->newdir(
|
|
|
|
TEMPLATE => 'tests.XXXXXX',
|
|
|
|
TMPDIR => 1,
|
|
|
|
CLEANUP => 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub prep_tar {
|
|
|
|
my $tarball = shift // (dirname(__FILE__) . '/t.tar');
|
|
|
|
|
2023-07-20 17:41:07 +00:00
|
|
|
my $td = mktempdir();
|
2023-01-29 21:35:16 +00:00
|
|
|
|
|
|
|
# Note: Using chdir from Cwd automatically keeps $ENV{PWD} up-to-date (just
|
|
|
|
# in case)
|
2023-07-20 17:41:07 +00:00
|
|
|
my $oldpwd = getcwd();
|
2023-01-29 21:35:16 +00:00
|
|
|
|
|
|
|
chdir $td;
|
|
|
|
my $tar = Archive::Tar->new;
|
|
|
|
$tar->read($tarball);
|
|
|
|
$tar->extract();
|
|
|
|
chdir $oldpwd;
|
|
|
|
|
|
|
|
return $td;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run_script_capture {
|
2023-07-20 17:37:07 +00:00
|
|
|
my @args = @_;
|
|
|
|
my @cmd = (SCRIPT, @args);
|
2023-01-29 21:35:16 +00:00
|
|
|
|
|
|
|
use IPC::Open3 qw/ open3 /;
|
|
|
|
my $stderr = File::Temp->new(
|
|
|
|
TMPDIR => 1,
|
|
|
|
CLEANUP => 1,
|
|
|
|
);
|
|
|
|
my $stdout = File::Temp->new(
|
|
|
|
TMPDIR => 1,
|
|
|
|
CLEANUP => 1,
|
|
|
|
);
|
|
|
|
my $in = '';
|
|
|
|
local *CATCHOUT = $stdout;
|
|
|
|
local *CATCHERR = $stderr;
|
|
|
|
print STDERR "+ @cmd\n";
|
|
|
|
my $pid = open3 $in, '>&CATCHOUT', '>&CATCHERR', @cmd;
|
|
|
|
waitpid $pid, 0;
|
|
|
|
seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
|
|
|
|
|
|
|
|
return (
|
|
|
|
$?,
|
|
|
|
(join "\n", <CATCHOUT>),
|
|
|
|
(join "\n", <CATCHERR>)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub run_script {
|
2023-07-20 17:37:07 +00:00
|
|
|
my @args = @_;
|
|
|
|
print STDERR '+ ' . SCRIPT . " @args\n";
|
|
|
|
return system SCRIPT, @args;
|
2023-01-29 21:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|