From 07633a0dc25ae39da9c71f69f73623393cb07bf5 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Tue, 24 May 2011 22:21:05 +0200 Subject: [PATCH] tests: make t/59-socketpaths exit gracefully Increases reported line coverage from 60.7% to 60.9% --- testcases/t/59-socketpaths.t | 10 +++++++--- testcases/t/lib/i3test.pm | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/testcases/t/59-socketpaths.t b/testcases/t/59-socketpaths.t index 3c9f90eb..9fc77910 100644 --- a/testcases/t/59-socketpaths.t +++ b/testcases/t/59-socketpaths.t @@ -34,7 +34,9 @@ ok(-d $folder, "folder $folder exists"); my $socketpath = "$folder/ipc-socket." . $process->pid; 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. @@ -52,7 +54,9 @@ ok(-d "$rtdir/i3", "$rtdir/i3 exists and is a directory"); $socketpath = "$rtdir/i3/ipc-socket." . $process->pid; 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 @@ -73,6 +77,6 @@ sleep 1; 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; diff --git a/testcases/t/lib/i3test.pm b/testcases/t/lib/i3test.pm index 4e6989fc..2dbc83b8 100644 --- a/testcases/t/lib/i3test.pm +++ b/testcases/t/lib/i3test.pm @@ -10,10 +10,11 @@ use AnyEvent::I3; use List::Util qw(first); use List::MoreUtils qw(lastval); use Time::HiRes qw(sleep); +use Try::Tiny; use v5.10; 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(); @@ -175,4 +176,21 @@ sub does_i3_live { 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 -- 2.39.5