tests: use stderr_like() instead of home-grown STDERR capturing

The STDERR capturing in testutil just reinvents Test::Output which
we already use in chkstow.t, so it's pointless to reinvent that wheel.
This commit is contained in:
Adam Spiers 2024-04-06 11:12:15 +01:00
parent ebfbb6cc13
commit bca711fac2
4 changed files with 41 additions and 59 deletions

View file

@ -372,18 +372,18 @@ subtest("stowing to stow dir should fail", sub {
make_path('stow/pkg14/stow/pkg15'); make_path('stow/pkg14/stow/pkg15');
make_file('stow/pkg14/stow/pkg15/node15'); make_file('stow/pkg14/stow/pkg15/node15');
capture_stderr(); stderr_like(
$stow->plan_stow('pkg14'); sub { $stow->plan_stow('pkg14'); },
qr/WARNING: skipping target which was current stow directory stow/,
"stowing to stow dir should give warning"
);
is($stow->get_tasks, 0, 'no tasks to process'); is($stow->get_tasks, 0, 'no tasks to process');
is($stow->get_conflict_count, 0); is($stow->get_conflict_count, 0);
ok( ok(
! -l 'stow/pkg15' ! -l 'stow/pkg15'
=> "stowing to stow dir should fail" => "stowing to stow dir should fail"
); );
like($stderr,
qr/WARNING: skipping target which was current stow directory stow/
=> "stowing to stow dir should give warning");
uncapture_stderr();
}); });
subtest("stow a simple tree minimally when cwd isn't target", sub { subtest("stow a simple tree minimally when cwd isn't target", sub {

View file

@ -28,7 +28,6 @@ use Carp qw(croak);
use File::Basename; use File::Basename;
use File::Path qw(make_path remove_tree); use File::Path qw(make_path remove_tree);
use File::Spec; use File::Spec;
use IO::Scalar;
use Test::More; use Test::More;
use Stow; use Stow;
@ -38,7 +37,6 @@ use base qw(Exporter);
our @EXPORT = qw( our @EXPORT = qw(
$ABS_TEST_DIR $ABS_TEST_DIR
$TEST_DIR $TEST_DIR
$stderr
init_test_dirs init_test_dirs
cd cd
new_Stow new_compat_Stow new_Stow new_compat_Stow
@ -46,25 +44,11 @@ our @EXPORT = qw(
remove_dir remove_file remove_link remove_dir remove_file remove_link
cat_file cat_file
is_link is_dir_not_symlink is_nonexistent_path is_link is_dir_not_symlink is_nonexistent_path
capture_stderr uncapture_stderr
); );
our $TEST_DIR = 'tmp-testing-trees'; our $TEST_DIR = 'tmp-testing-trees';
our $ABS_TEST_DIR = File::Spec->rel2abs('tmp-testing-trees'); our $ABS_TEST_DIR = File::Spec->rel2abs('tmp-testing-trees');
our $stderr;
my $tied_err;
sub capture_stderr {
undef $stderr;
$tied_err = tie *STDERR, 'IO::Scalar', \$stderr;
}
sub uncapture_stderr {
undef $tied_err;
untie *STDERR;
}
sub init_test_dirs { sub init_test_dirs {
-d "t" or croak "Was expecting tests to be run from root of repo\n"; -d "t" or croak "Was expecting tests to be run from root of repo\n";

View file

@ -183,8 +183,11 @@ make_path('stow/pkg8a/stow2/pkg8b');
make_file('stow/pkg8a/stow2/pkg8b/file8b'); make_file('stow/pkg8a/stow2/pkg8b/file8b');
make_link('stow2/pkg8b', '../stow/pkg8a/stow2/pkg8b'); make_link('stow2/pkg8b', '../stow/pkg8a/stow2/pkg8b');
capture_stderr(); stderr_like(
$stow->plan_unstow('pkg8a'); sub { $stow->plan_unstow('pkg8a'); },
qr/WARNING: skipping marked Stow directory stow2/
=> "unstowing from ourself should skip stow"
);
is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg8a'); is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg8a');
ok( ok(
$stow->get_conflict_count == 0 && $stow->get_conflict_count == 0 &&
@ -192,10 +195,6 @@ ok(
readlink('stow2/pkg8b') eq '../stow/pkg8a/stow2/pkg8b' readlink('stow2/pkg8b') eq '../stow/pkg8a/stow2/pkg8b'
=> q(don't unlink any nodes under another stow directory) => q(don't unlink any nodes under another stow directory)
); );
like($stderr,
qr/WARNING: skipping marked Stow directory stow2/
=> "unstowing from ourself should skip stow");
uncapture_stderr();
# #
# overriding already stowed documentation # overriding already stowed documentation

View file

@ -161,18 +161,17 @@ subtest("Don't unlink anything under the stow directory", sub {
make_file('stow/pkg7a/stow/pkg7b/file7b'); make_file('stow/pkg7a/stow/pkg7b/file7b');
make_link('stow/pkg7b', '../stow/pkg7a/stow/pkg7b'); make_link('stow/pkg7b', '../stow/pkg7a/stow/pkg7b');
capture_stderr(); stderr_like(
$stow->plan_unstow('pkg7b'); sub { $stow->plan_unstow('pkg7b'); },
qr/WARNING: skipping target which was current stow directory stow/
=> "warn when unstowing from ourself"
);
is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg7b'); is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg7b');
is($stow->get_conflict_count, 0); is($stow->get_conflict_count, 0);
ok(-l 'stow/pkg7b'); ok(-l 'stow/pkg7b');
ok(readlink('stow/pkg7b') eq '../stow/pkg7a/stow/pkg7b' ok(readlink('stow/pkg7b') eq '../stow/pkg7a/stow/pkg7b'
=> q(don't unlink any nodes under the stow directory) => q(don't unlink any nodes under the stow directory)
); );
like($stderr,
qr/WARNING: skipping target which was current stow directory stow/
=> "warn when unstowing from ourself");
uncapture_stderr();
}); });
subtest("Don't unlink any nodes under another stow directory", sub { subtest("Don't unlink any nodes under another stow directory", sub {
@ -187,28 +186,28 @@ subtest("Don't unlink any nodes under another stow directory", sub {
make_file('stow/pkg8a/stow2/pkg8b/file8b'); make_file('stow/pkg8a/stow2/pkg8b/file8b');
make_link('stow2/pkg8b', '../stow/pkg8a/stow2/pkg8b'); make_link('stow2/pkg8b', '../stow/pkg8a/stow2/pkg8b');
capture_stderr(); stderr_like(
$stow->plan_unstow('pkg8a'); sub { $stow->plan_unstow('pkg8a'); },
qr/WARNING: skipping target which was current stow directory stow/
=> "warn when skipping unstowing"
);
is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg8a'); is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg8a');
is($stow->get_conflict_count, 0); is($stow->get_conflict_count, 0);
ok(-l 'stow2/pkg8b'); ok(-l 'stow2/pkg8b');
ok(readlink('stow2/pkg8b') eq '../stow/pkg8a/stow2/pkg8b' ok(readlink('stow2/pkg8b') eq '../stow/pkg8a/stow2/pkg8b'
=> q(don't unlink any nodes under another stow directory) => q(don't unlink any nodes under another stow directory)
); );
like($stderr,
qr/WARNING: skipping target which was current stow directory stow/
=> "warn when skipping unstowing");
uncapture_stderr();
}); });
# This will be used by subsequent tests # This will be used by subsequent tests
sub check_protected_dirs_skipped { sub check_protected_dirs_skipped {
my $coderef = shift;
my $stderr = stderr_from { $coderef->(); };
for my $dir (qw{stow stow2}) { for my $dir (qw{stow stow2}) {
like($stderr, like($stderr,
qr/WARNING: skipping marked Stow directory $dir/ qr/WARNING: skipping marked Stow directory $dir/
=> "warn when skipping marked directory $dir"); => "warn when skipping marked directory $dir");
} }
uncapture_stderr();
} }
subtest("overriding already stowed documentation", sub { subtest("overriding already stowed documentation", sub {
@ -224,14 +223,14 @@ subtest("overriding already stowed documentation", sub {
make_path('../stow/pkg9b/man9/man1'); make_path('../stow/pkg9b/man9/man1');
make_file('../stow/pkg9b/man9/man1/file9.1'); make_file('../stow/pkg9b/man9/man1/file9.1');
capture_stderr(); check_protected_dirs_skipped(
$stow->plan_unstow('pkg9b'); sub { $stow->plan_unstow('pkg9b'); }
);
$stow->process_tasks(); $stow->process_tasks();
is($stow->get_conflict_count, 0); is($stow->get_conflict_count, 0);
ok(!-l 'man9/man1/file9.1' ok(!-l 'man9/man1/file9.1'
=> 'overriding existing documentation files' => 'overriding existing documentation files'
); );
check_protected_dirs_skipped();
}); });
subtest("deferring to already stowed documentation", sub { subtest("deferring to already stowed documentation", sub {
@ -250,14 +249,14 @@ subtest("deferring to already stowed documentation", sub {
make_path('../stow/pkg10c/man10/man1'); make_path('../stow/pkg10c/man10/man1');
make_file('../stow/pkg10c/man10/man1/file10a.1'); make_file('../stow/pkg10c/man10/man1/file10a.1');
capture_stderr(); check_protected_dirs_skipped(
$stow->plan_unstow('pkg10c'); sub { $stow->plan_unstow('pkg10c'); }
);
is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg10c'); is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg10c');
is($stow->get_conflict_count, 0); is($stow->get_conflict_count, 0);
ok(readlink('man10/man1/file10a.1') eq '../../../stow/pkg10a/man10/man1/file10a.1' ok(readlink('man10/man1/file10a.1') eq '../../../stow/pkg10a/man10/man1/file10a.1'
=> 'defer to existing documentation files' => 'defer to existing documentation files'
); );
check_protected_dirs_skipped();
}); });
subtest("Ignore temp files", sub { subtest("Ignore temp files", sub {
@ -271,25 +270,25 @@ subtest("Ignore temp files", sub {
make_path('man12/man1'); make_path('man12/man1');
make_link('man12/man1/file12.1' => '../../../stow/pkg12/man12/man1/file12.1'); make_link('man12/man1/file12.1' => '../../../stow/pkg12/man12/man1/file12.1');
capture_stderr(); check_protected_dirs_skipped(
$stow->plan_unstow('pkg12'); sub { $stow->plan_unstow('pkg12'); }
);
$stow->process_tasks(); $stow->process_tasks();
is($stow->get_conflict_count, 0); is($stow->get_conflict_count, 0);
ok(!-e 'man12/man1/file12.1' => 'ignore temp files'); ok(!-e 'man12/man1/file12.1' => 'ignore temp files');
check_protected_dirs_skipped();
}); });
subtest("Unstow an already unstowed package", sub { subtest("Unstow an already unstowed package", sub {
plan tests => 4; plan tests => 4;
my $stow = new_compat_Stow(); my $stow = new_compat_Stow();
capture_stderr(); check_protected_dirs_skipped(
$stow->plan_unstow('pkg12'); sub { $stow->plan_unstow('pkg12'); }
);
is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12'); is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12');
ok( ok(
$stow->get_conflict_count == 0 $stow->get_conflict_count == 0
=> 'unstow already unstowed package pkg12' => 'unstow already unstowed package pkg12'
); );
check_protected_dirs_skipped();
}); });
subtest("Unstow a never stowed package", sub { subtest("Unstow a never stowed package", sub {
@ -299,14 +298,14 @@ subtest("Unstow a never stowed package", sub {
mkdir("$TEST_DIR/target"); mkdir("$TEST_DIR/target");
my $stow = new_compat_Stow(); my $stow = new_compat_Stow();
capture_stderr(); check_protected_dirs_skipped(
$stow->plan_unstow('pkg12'); sub { $stow->plan_unstow('pkg12'); }
);
is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12 which was never stowed'); is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12 which was never stowed');
ok( ok(
$stow->get_conflict_count == 0 $stow->get_conflict_count == 0
=> 'unstow never stowed package pkg12' => 'unstow never stowed package pkg12'
); );
check_protected_dirs_skipped();
}); });
subtest("Unstowing when target contains a real file shouldn't be an issue", sub { subtest("Unstowing when target contains a real file shouldn't be an issue", sub {
@ -314,8 +313,9 @@ subtest("Unstowing when target contains a real file shouldn't be an issue", sub
make_file('man12/man1/file12.1'); make_file('man12/man1/file12.1');
my $stow = new_compat_Stow(); my $stow = new_compat_Stow();
capture_stderr(); check_protected_dirs_skipped(
$stow->plan_unstow('pkg12'); sub { $stow->plan_unstow('pkg12'); }
);
is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12 for third time'); is($stow->get_tasks, 0, 'no tasks to process when unstowing pkg12 for third time');
%conflicts = $stow->get_conflicts; %conflicts = $stow->get_conflicts;
ok($stow->get_conflict_count == 1); ok($stow->get_conflict_count == 1);
@ -323,7 +323,6 @@ subtest("Unstowing when target contains a real file shouldn't be an issue", sub
=~ m!existing target is neither a link nor a directory: man12/man1/file12\.1! =~ m!existing target is neither a link nor a directory: man12/man1/file12\.1!
=> 'unstow pkg12 for third time' => 'unstow pkg12 for third time'
); );
check_protected_dirs_skipped();
}); });
subtest("unstow a simple tree minimally when cwd isn't target", sub { subtest("unstow a simple tree minimally when cwd isn't target", sub {