| 1 |
use Test2::V0 -target => 'App::Yath::Util'; |
| 2 |
use Test2::Tools::Spec; |
| 3 |
|
| 4 |
use Test2::Util qw/CAN_REALLY_FORK/; |
| 5 |
use Test2::Tools::GenTemp qw/gen_temp/; |
| 6 |
use Test2::Harness::Util qw/clean_path/; |
| 7 |
use File::Temp qw/tempfile/; |
| 8 |
use Cwd qw/cwd/; |
| 9 |
use POSIX qw/_exit/; |
| 10 |
|
| 11 |
use File::Spec; |
| 12 |
|
| 13 |
use App::Yath::Util qw{ |
| 14 |
find_pfile |
| 15 |
is_generated_test_pl |
| 16 |
fit_to_width |
| 17 |
isolate_stdout |
| 18 |
find_yath |
| 19 |
find_in_updir |
| 20 |
}; |
| 21 |
|
| 22 |
imported_ok qw{ |
| 23 |
find_pfile |
| 24 |
is_generated_test_pl |
| 25 |
fit_to_width |
| 26 |
isolate_stdout |
| 27 |
find_yath |
| 28 |
find_in_updir |
| 29 |
}; |
| 30 |
|
| 31 |
my $initial_dir = cwd(); |
| 32 |
after_each chdir => sub { |
| 33 |
chdir($initial_dir); |
| 34 |
}; |
| 35 |
|
| 36 |
tests find_yath => sub { |
| 37 |
local $App::Yath::Script::SCRIPT = 'foobar'; |
| 38 |
is(find_yath, 'foobar', "Use \$App::Yath::Script::SCRIPT if set"); |
| 39 |
|
| 40 |
$App::Yath::Script::SCRIPT = undef; |
| 41 |
|
| 42 |
my $tmp = gen_temp('bin' => {'yath' => 'xxx'}); |
| 43 |
my $yath = clean_path(File::Spec->catfile($tmp, 'bin', 'yath')); |
| 44 |
eval { chmod(0755, File::Spec->catfile($tmp, 'bin', 'yath')); 1 } or warn $@; |
| 45 |
|
| 46 |
{ |
| 47 |
local $ENV{YATH_SCRIPT} = $yath; |
| 48 |
is(find_yath, $yath, "found the yath script named by \$ENV{YATH_SCRIPT}"); |
| 49 |
is($App::Yath::Script::SCRIPT, $yath, "cached result"); |
| 50 |
} |
| 51 |
|
| 52 |
# An uninstalled dist: the script lives in blib/script, next to the libs |
| 53 |
# in blib/lib. A local::lib style tree pairs lib/perl5 with bin. |
| 54 |
my $tmp2 = gen_temp( |
| 55 |
blib => {lib => {}, script => {yath => 'xxx'}}, |
| 56 |
lib => {perl5 => {}}, |
| 57 |
bin => {yath => 'xxx'}, |
| 58 |
); |
| 59 |
my $blib_yath = clean_path(File::Spec->catfile($tmp2, 'blib', 'script', 'yath')); |
| 60 |
my $bin_yath = clean_path(File::Spec->catfile($tmp2, 'bin', 'yath')); |
| 61 |
eval { chmod(0755, $blib_yath, $bin_yath); 1 } or warn $@; |
| 62 |
|
| 63 |
# Each search source is checked in isolation: only what the params provide |
| 64 |
# is visible to find_yath. |
| 65 |
my $find = sub { |
| 66 |
my %params = @_; |
| 67 |
|
| 68 |
$App::Yath::Script::SCRIPT = undef; |
| 69 |
|
| 70 |
local %ENV = %ENV; |
| 71 |
delete $ENV{YATH_SCRIPT}; |
| 72 |
$ENV{YATH_SCRIPT} = $params{env_script} if $params{env_script}; |
| 73 |
$ENV{PATH} = defined $params{path} ? $params{path} : ''; |
| 74 |
|
| 75 |
local @INC = @{$params{inc} || []}; |
| 76 |
local *App::Yath::Util::Config = $params{config} || {}; |
| 77 |
|
| 78 |
return dies { find_yath } if $params{dies}; |
| 79 |
return find_yath; |
| 80 |
}; |
| 81 |
|
| 82 |
# An empty dir as a config path, so the error has a candidate to list. |
| 83 |
my $empty = gen_temp(); |
| 84 |
my $not_found = File::Spec->catfile($empty, 'yath'); |
| 85 |
|
| 86 |
my $err = $find->(dies => 1, config => {scriptdir => $empty}); |
| 87 |
like($err, qr/Could not find the yath script/, "No yath found"); |
| 88 |
like($err, qr/^\s+\Q$not_found\E$/m, "Error reports each path it checked"); |
| 89 |
like($err, qr/^Cwd:/m, "Error reports the current directory"); |
| 90 |
like($err, qr/^PATH:/m, "Error reports PATH"); |
| 91 |
like($err, qr/^PERL5LIB:/m, "Error reports PERL5LIB"); |
| 92 |
like($err, qr/^\@INC:/m, "Error reports \@INC"); |
| 93 |
|
| 94 |
is($find->(env_script => $yath), $yath, "Found it via \$ENV{YATH_SCRIPT}"); |
| 95 |
|
| 96 |
is( |
| 97 |
$find->(env_script => $yath, inc => [File::Spec->catdir($tmp2, 'blib', 'lib')]), |
| 98 |
$yath, |
| 99 |
"\$ENV{YATH_SCRIPT} outranks the search paths", |
| 100 |
); |
| 101 |
|
| 102 |
is( |
| 103 |
$find->(inc => [File::Spec->catdir($tmp2, 'blib', 'lib')]), |
| 104 |
$blib_yath, |
| 105 |
"Found blib/script beside a blib/lib in \@INC", |
| 106 |
); |
| 107 |
|
| 108 |
is( |
| 109 |
$find->(inc => [File::Spec->catdir($tmp2, 'lib', 'perl5')]), |
| 110 |
$bin_yath, |
| 111 |
"Found bin beside a lib/perl5 in \@INC", |
| 112 |
); |
| 113 |
|
| 114 |
is( |
| 115 |
$find->(config => {scriptdir => File::Spec->catdir($tmp, 'bin')}), |
| 116 |
$yath, |
| 117 |
"Found it in a config path", |
| 118 |
); |
| 119 |
|
| 120 |
is($find->(path => File::Spec->catdir($tmp, 'bin')), $yath, "Found it in PATH"); |
| 121 |
|
| 122 |
# App::Yath::Script re-execs into a checkout's own script and names it in |
| 123 |
# YATH_SCRIPT, so find_yath does not look for one itself. |
| 124 |
my $checkout = gen_temp('scripts' => {'yath' => 'xxx'}); |
| 125 |
eval { chmod(0755, File::Spec->catfile($checkout, 'scripts', 'yath')); 1 } or warn $@; |
| 126 |
|
| 127 |
chdir($checkout) or die "$!"; |
| 128 |
like( |
| 129 |
$find->(dies => 1), |
| 130 |
qr/Could not find the yath script/, |
| 131 |
"A scripts/ dir in the current directory is not searched", |
| 132 |
); |
| 133 |
chdir($initial_dir) or die "$!"; |
| 134 |
|
| 135 |
# The script is run as an argument to perl, so it need not be executable, |
| 136 |
# except when PATH is what named it. |
| 137 |
my $tmp3 = gen_temp(bin => {yath => 'xxx'}); |
| 138 |
my $unexecutable = clean_path(File::Spec->catfile($tmp3, 'bin', 'yath')); |
| 139 |
eval { chmod(0644, $unexecutable); 1 } or warn $@; |
| 140 |
|
| 141 |
is( |
| 142 |
$find->(config => {scriptdir => File::Spec->catdir($tmp3, 'bin')}), |
| 143 |
$unexecutable, |
| 144 |
"A config path yath does not need the exec bit", |
| 145 |
); |
| 146 |
|
| 147 |
like( |
| 148 |
$find->(dies => 1, path => File::Spec->catdir($tmp3, 'bin')), |
| 149 |
qr/Could not find the yath script/, |
| 150 |
"A PATH yath without the exec bit is not used", |
| 151 |
); |
| 152 |
|
| 153 |
# An uninstalled script is never in a config path, a lib/perl5 pairing is a |
| 154 |
# guess, so they sit on either side of the config paths. |
| 155 |
is( |
| 156 |
$find->( |
| 157 |
inc => [File::Spec->catdir($tmp2, 'blib', 'lib')], |
| 158 |
config => {scriptdir => File::Spec->catdir($tmp, 'bin')}, |
| 159 |
), |
| 160 |
$blib_yath, |
| 161 |
"A blib/script dir outranks a config path", |
| 162 |
); |
| 163 |
|
| 164 |
is( |
| 165 |
$find->( |
| 166 |
inc => [File::Spec->catdir($tmp2, 'lib', 'perl5')], |
| 167 |
config => {scriptdir => File::Spec->catdir($tmp, 'bin')}, |
| 168 |
), |
| 169 |
$yath, |
| 170 |
"A config path outranks a lib/perl5 pairing", |
| 171 |
); |
| 172 |
}; |
| 173 |
|
| 174 |
tests isolate_stdout => sub { |
| 175 |
my ($stdout_r, $stdout_w, $stderr_r, $stderr_w); |
| 176 |
pipe($stdout_r, $stdout_w) or die "Could not open pipe: $!"; |
| 177 |
pipe($stderr_r, $stderr_w) or die "Could not open pipe: $!"; |
| 178 |
|
| 179 |
my $pid = fork; |
| 180 |
die "Could not fork" unless defined $pid; |
| 181 |
|
| 182 |
unless ($pid) { # child |
| 183 |
close($stdout_r); |
| 184 |
close($stderr_r); |
| 185 |
open(STDOUT, '>&', $stdout_w) or die "Could not redirect STDOUT"; |
| 186 |
open(STDERR, '>&', $stderr_w) or die "Could not redirect STDOUT"; |
| 187 |
my $fh = isolate_stdout(); |
| 188 |
|
| 189 |
print $fh "Should go to STDOUT\n"; |
| 190 |
print "Should go to STDERR 1\n"; |
| 191 |
print STDOUT "Should go to STDERR 2\n"; |
| 192 |
print STDERR "Should go to STDERR 3\n"; |
| 193 |
|
| 194 |
# Flush and _exit() instead of exit(): global destruction in this |
| 195 |
# process can write to these pipes (Math::Random::MT::Auto complains |
| 196 |
# about a double DESTROY on some systems) and the parent asserts on |
| 197 |
# exactly what came through. |
| 198 |
close($fh) or _exit(255); |
| 199 |
close(STDOUT) or _exit(255); |
| 200 |
close(STDERR) or _exit(255); |
| 201 |
|
| 202 |
_exit(0); |
| 203 |
} |
| 204 |
|
| 205 |
close($stdout_w); |
| 206 |
close($stderr_w); |
| 207 |
waitpid($pid, 0); |
| 208 |
is($?, 0, "Clean exit"); |
| 209 |
|
| 210 |
is( |
| 211 |
[<$stdout_r>], |
| 212 |
["Should go to STDOUT\n"], |
| 213 |
"Got expected STDOUT" |
| 214 |
); |
| 215 |
is( |
| 216 |
[<$stderr_r>], |
| 217 |
[ |
| 218 |
"Should go to STDERR 1\n", |
| 219 |
"Should go to STDERR 2\n", |
| 220 |
"Should go to STDERR 3\n", |
| 221 |
], |
| 222 |
"Got expected STDERR" |
| 223 |
); |
| 224 |
} if CAN_REALLY_FORK; |
| 225 |
|
| 226 |
subtest is_generated_test_pl => sub { |
| 227 |
ok(!is_generated_test_pl(__FILE__), "This is not a generated test file"); |
| 228 |
|
| 229 |
my ($fh, $name) = tempfile(UNLINK => 1); |
| 230 |
print $fh "use strict;\nuse warnings;\n# THIS IS A GENERATED YATH RUNNER TEST\ndfasdafas\n"; |
| 231 |
close($fh); |
| 232 |
ok(is_generated_test_pl($name), "Found a generated file"); |
| 233 |
}; |
| 234 |
|
| 235 |
subtest find_in_updir => sub { |
| 236 |
my $tmp = gen_temp( |
| 237 |
thefile => 'xxx', |
| 238 |
nest => { |
| 239 |
nest_a => { thefile => 'xxx' }, |
| 240 |
nest_b => {}, |
| 241 |
}, |
| 242 |
); |
| 243 |
|
| 244 |
chdir(File::Spec->catdir($tmp, 'nest', 'nest_a')) or die "$!"; |
| 245 |
my $file = File::Spec->catfile($tmp, 'nest', 'nest_a', 'thefile'); |
| 246 |
like(find_in_updir('thefile'), qr{\Q$file\E$}, "Found file in expected spot"); |
| 247 |
|
| 248 |
chdir(File::Spec->catdir($tmp, 'nest', 'nest_b')) or die "$!"; |
| 249 |
$file = File::Spec->catfile($tmp, 'thefile'); |
| 250 |
like(find_in_updir('thefile'), qr{\Q$file\E$}, "Found file in expected spot"); |
| 251 |
}; |
| 252 |
|
| 253 |
subtest fit_to_width => sub { |
| 254 |
is(fit_to_width(100, " ", "hello there"), "hello there", "No change for short string"); |
| 255 |
is(fit_to_width(2, " ", "hello there"), "hello\nthere", "Split across multiple lines"); |
| 256 |
|
| 257 |
is( |
| 258 |
fit_to_width(20, " ", "hello there, this is a longer string that needs splitting."), |
| 259 |
"hello there, this is\na longer string that\nneeds splitting.", |
| 260 |
"Split across multiple lines" |
| 261 |
); |
| 262 |
|
| 263 |
is( |
| 264 |
fit_to_width(100, " ", ["hello there", "this is a", "longer string that", "needs no splitting."]), |
| 265 |
"hello there this is a longer string that needs no splitting.", |
| 266 |
"Split across multiple lines" |
| 267 |
); |
| 268 |
|
| 269 |
is( |
| 270 |
fit_to_width(50, " ", ["hello there", "this is a", "longer string that", "needs splitting."]), |
| 271 |
"hello there this is a longer string that\nneeds splitting.", |
| 272 |
"Split across multiple lines" |
| 273 |
); |
| 274 |
}; |
| 275 |
|
| 276 |
done_testing; |