tests: make t/59-socketpaths exit gracefully

Increases reported line coverage from 60.7% to 60.9%
This commit is contained in:
Michael Stapelberg 2011-05-24 22:21:05 +02:00
parent 2c68c234ea
commit 07633a0dc2
2 changed files with 26 additions and 4 deletions

View File

@ -34,7 +34,9 @@ ok(-d $folder, "folder $folder exists");
my $socketpath = "$folder/ipc-socket." . $process->pid; my $socketpath = "$folder/ipc-socket." . $process->pid;
ok(-S $socketpath, "file $socketpath exists and is a socket"); ok(-S $socketpath, "file $socketpath exists and is a socket");
kill(9, $process->pid) or die "could not kill i3"; exit_gracefully($process->pid, $socketpath);
sleep 0.25;
##################################################################### #####################################################################
# XDG_RUNTIME_DIR case: socket gets created in $XDG_RUNTIME_DIR/i3/ipc-socket.<pid> # XDG_RUNTIME_DIR case: socket gets created in $XDG_RUNTIME_DIR/i3/ipc-socket.<pid>
@ -52,7 +54,9 @@ ok(-d "$rtdir/i3", "$rtdir/i3 exists and is a directory");
$socketpath = "$rtdir/i3/ipc-socket." . $process->pid; $socketpath = "$rtdir/i3/ipc-socket." . $process->pid;
ok(-S $socketpath, "file $socketpath exists and is a socket"); ok(-S $socketpath, "file $socketpath exists and is a socket");
kill(9, $process->pid) or die "could not kill i3"; exit_gracefully($process->pid, $socketpath);
sleep 0.25;
##################################################################### #####################################################################
# configuration file case: socket gets placed whereever we specify # configuration file case: socket gets placed whereever we specify
@ -73,6 +77,6 @@ sleep 1;
ok(-S $socketpath, "file $socketpath exists and is a socket"); ok(-S $socketpath, "file $socketpath exists and is a socket");
kill(9, $process->pid) or die "could not kill i3"; exit_gracefully($process->pid, $socketpath);
done_testing; done_testing;

View File

@ -10,10 +10,11 @@ use AnyEvent::I3;
use List::Util qw(first); use List::Util qw(first);
use List::MoreUtils qw(lastval); use List::MoreUtils qw(lastval);
use Time::HiRes qw(sleep); use Time::HiRes qw(sleep);
use Try::Tiny;
use v5.10; use v5.10;
use Exporter (); use Exporter ();
our @EXPORT = qw(get_workspace_names get_unused_workspace fresh_workspace get_ws_content get_ws get_focused open_empty_con open_standard_window get_dock_clients cmd does_i3_live); our @EXPORT = qw(get_workspace_names get_unused_workspace fresh_workspace get_ws_content get_ws get_focused open_empty_con open_standard_window get_dock_clients cmd does_i3_live exit_gracefully);
my $tester = Test::Builder->new(); my $tester = Test::Builder->new();
@ -175,4 +176,21 @@ sub does_i3_live {
return $ok; return $ok;
} }
# Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
sub exit_gracefully {
my ($pid, $socketpath) = @_;
$socketpath ||= '/tmp/nestedcons';
my $exited = 0;
try {
say "Exiting i3 cleanly...";
i3($socketpath)->command('exit')->recv;
$exited = 1;
};
if (!$exited) {
kill(9, $pid) or die "could not kill i3";
}
}
1 1