]> git.proxmox.com Git - pve-installer.git/commitdiff
tests: code-style and error handling fixes for ui2stdio
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Sat, 24 Feb 2024 17:17:10 +0000 (18:17 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Sat, 24 Feb 2024 17:27:48 +0000 (18:27 +0100)
use modern calling style, avoid duplicate use of Test::More module,
handle fork error more visible, handle pipe creation errors and do
that all in one commit as it's just a test and I don't care.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
test/ui2-stdio.pl

index f1c38e8ffe5ad77d22fa10d8153c2dfda3136f83..ae29b79da900e9b2eab3c7fbd8be1bbe088b545a 100755 (executable)
@@ -9,15 +9,17 @@ use Test::More;
 use Proxmox::Log;
 use Proxmox::UI;
 
-pipe(my $parent_reader, my $child_writer) || die;
-pipe(my $child_reader, my $parent_writer) || die;
+pipe(my $parent_reader, my $child_writer) || die "failed to create r/w pipe - $!\n";
+pipe(my $child_reader, my $parent_writer) || die "failed to create w/r pipe - $!\n";
 
 $parent_writer->autoflush(1);
 $child_writer->autoflush(1);
 $child_reader->autoflush(1);
 
 
-if (my $child_pid = fork()) {
+my $child_pid = fork() // die "fork failed - $!\n";
+
+if ($child_pid) {
     # parent, the hypothetical low-level installer
     close($parent_reader);
     close($parent_writer);
@@ -46,48 +48,45 @@ if (my $child_pid = fork()) {
     done_testing();
 } else {
     # child, e.g. the TUI
-    die 'failed to fork?' if !defined($child_pid);
     close($child_reader);
     close($child_writer);
 
-    use Test::More;
-
     my $next_msg = sub {
        chomp(my $msg = <$parent_reader>);
        return from_json($msg, { utf8 => 1 });
     };
 
-    is_deeply(&$next_msg(), { type => 'message', message => 'foo' }, 'should receive message');
-    is_deeply(&$next_msg(), { type => 'error', message => 'bar' }, 'should receive error');
+    is_deeply($next_msg->(), { type => 'message', message => 'foo' }, 'should receive message');
+    is_deeply($next_msg->(), { type => 'error', message => 'bar' }, 'should receive error');
 
-    is_deeply(&$next_msg(), { type => 'prompt', query => 'baz?' }, 'prompt works');
+    is_deeply($next_msg->(), { type => 'prompt', query => 'baz?' }, 'prompt works');
     print $parent_writer "{\"type\":\"prompt-answer\",\"answer\":\"ok\"}\n";
 
-    is_deeply(&$next_msg(), { type => 'prompt', query => 'not baz? :(' }, 'prompt works');
+    is_deeply($next_msg->(), { type => 'prompt', query => 'not baz? :(' }, 'prompt works');
     print $parent_writer "{\"type\":\"prompt-answer\",\"answer\":\"cancel\"}\n";
 
     is_deeply(
-       &$next_msg(), { type => 'finished', state => 'ok', message => 'install successful'},
+       $next_msg->(), { type => 'finished', state => 'ok', message => 'install successful'},
        'should receive successful finished message');
 
     is_deeply(
-       &$next_msg(), { type => 'finished', state => 'err', message => 'install failed'},
+       $next_msg->(), { type => 'finished', state => 'err', message => 'install failed'},
        'should receive failed finished message');
 
     is_deeply(
-       &$next_msg(), { type => 'progress', ratio => 0.2, text => '20% done' },
+       $next_msg->(), { type => 'progress', ratio => 0.2, text => '20% done' },
        'should get 20% done progress message');
 
     is_deeply(
-       &$next_msg(), { type => 'progress', ratio => 0.2, text => '' },
+       $next_msg->(), { type => 'progress', ratio => 0.2, text => '' },
        'should get progress continuation message');
 
     is_deeply(
-       &$next_msg(), { type => 'progress', ratio => 0.99, text => '99% done' },
+       $next_msg->(), { type => 'progress', ratio => 0.99, text => '99% done' },
        'should get 99% done progress message');
 
     is_deeply(
-       &$next_msg(), { type => 'progress', ratio => 1, text => 'done' },
+       $next_msg->(), { type => 'progress', ratio => 1, text => 'done' },
        'should get 100% done progress message');
 
     done_testing();