|
| 1 | +package BS; |
| 2 | + |
| 3 | +=head1 NAME |
| 4 | +
|
| 5 | +issue/1508.needs_arg-propagation.tau.t - Check that the `needs()` function correctly propogates run-time task arguments |
| 6 | +
|
| 7 | +=head1 DESCRIPTION |
| 8 | +
|
| 9 | +Check that the `needs()` function does indeed correctly propogate |
| 10 | +run-time task arguments (%params and @args) from the calling task down to the "needed" tasks. |
| 11 | +
|
| 12 | +=head1 DETAILS |
| 13 | +
|
| 14 | + * AUTHOR / DATE : [tabulon]@[2021-09-26] |
| 15 | + * RELATES-TO : [github issue #1508](https://github.com/RexOps/Rex/issues/1508#issue-1007457392) |
| 16 | + * INSPIRED from : t/needs.t |
| 17 | +
|
| 18 | +=cut |
| 19 | + |
| 20 | +use Test::More; |
| 21 | +use Rex::Commands; |
| 22 | + |
| 23 | +{ |
| 24 | +package T; # Helper package (for cutting down boilerplate in tests) |
| 25 | + use Storable; |
| 26 | + |
| 27 | + sub track_taskrun { |
| 28 | + my %opts = ref $_[-1] eq 'HASH' ? %{; pop } : (); |
| 29 | + my $argv = $opts{argv}; |
| 30 | + my @prereqs = (@_); |
| 31 | + for (@prereqs) { |
| 32 | + my $file = "${_}.txt"; |
| 33 | + Storable::store($argv, $file); |
| 34 | + } |
| 35 | + } |
| 36 | + sub check_needed { |
| 37 | + my %opts = ref $_[-1] eq 'HASH' ? %{; pop } : (); |
| 38 | + my $argv = $opts{argv}; |
| 39 | + my $do_unlink = delete $opts{unlink} // 1; |
| 40 | + my @prereqs = (@_); |
| 41 | + |
| 42 | + for (@prereqs) { |
| 43 | + my $file = "${_}.txt"; |
| 44 | + |
| 45 | + -f "$file" or die; |
| 46 | + my $propagated_argv = Storable::retrieve("$file"); |
| 47 | + Test::More::_deep_check($propagated_argv, $argv) or die; |
| 48 | + |
| 49 | + unlink("$file") if ($do_unlink); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +{ |
| 56 | + |
| 57 | + package MyTest; |
| 58 | + use strict; |
| 59 | + use warnings; |
| 60 | + use Rex::Commands; |
| 61 | + |
| 62 | + $::QUIET = 1; |
| 63 | + |
| 64 | + task test1 => sub { |
| 65 | + T::track_taskrun(test1 => {argv => \@_}); |
| 66 | + }; |
| 67 | + |
| 68 | + task test2 => sub { |
| 69 | + T::track_taskrun(test2 => {argv => \@_}); |
| 70 | + }; |
| 71 | + |
| 72 | + 1; |
| 73 | +} |
| 74 | + |
| 75 | +{ |
| 76 | + |
| 77 | + package Nested::Module; |
| 78 | + |
| 79 | + use strict; |
| 80 | + use warnings; |
| 81 | + |
| 82 | + use Rex::Commands; |
| 83 | + |
| 84 | + task test => sub { |
| 85 | + T::track_taskrun(test => {argv => \@_}); |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +{ |
| 90 | + |
| 91 | + package Rex::Module; |
| 92 | + |
| 93 | + use strict; |
| 94 | + use warnings; |
| 95 | + |
| 96 | + use Rex::Commands; |
| 97 | + |
| 98 | + task test => sub { |
| 99 | + T::track_taskrun(test => {argv => \@_}); |
| 100 | + }; |
| 101 | +} |
| 102 | + |
| 103 | +task test => sub { |
| 104 | + needs MyTest; |
| 105 | + |
| 106 | + T::check_needed($_, {argv => \@_}) for (qw/test1 test2/); |
| 107 | +}; |
| 108 | + |
| 109 | +task test2 => sub { |
| 110 | + needs MyTest "test2"; |
| 111 | + |
| 112 | + T::check_needed($_, {argv => \@_}) for (qw/test2/); |
| 113 | +}; |
| 114 | + |
| 115 | +task test3 => sub { |
| 116 | + needs "test4"; |
| 117 | + |
| 118 | + T::check_needed($_, {argv => \@_}) for (qw/test4/); |
| 119 | +}; |
| 120 | + |
| 121 | +task test4 => sub { |
| 122 | + T::track_taskrun(test4 => {argv => \@_}); |
| 123 | +}; |
| 124 | + |
| 125 | +task test5 => sub { |
| 126 | + needs Nested::Module test; |
| 127 | + |
| 128 | + T::check_needed($_, {argv => \@_}) for (qw/test/); |
| 129 | +}; |
| 130 | + |
| 131 | + |
| 132 | +task test6 => sub { |
| 133 | + needs Rex::Module "test"; |
| 134 | + |
| 135 | + T::check_needed($_, {argv => \@_}) for (qw/test /); |
| 136 | +}; |
| 137 | + |
| 138 | + |
| 139 | +TODO: { |
| 140 | + local $TODO = "Resolve issue N° XXX: needs() should propogate current task parameters/args"; |
| 141 | + |
| 142 | + my $task_list = Rex::TaskList->create; |
| 143 | + my $run_list = Rex::RunList->instance; |
| 144 | + $run_list->parse_opts(qw/test test2 test3 test5 test6/); |
| 145 | + |
| 146 | + |
| 147 | + for my $task ( $run_list->tasks ) { |
| 148 | + my $name=$task->name; |
| 149 | + my %prms=("${name}_greet" => "Hello ${name}"); |
| 150 | + my @args=("${name}.arg.0", "${name}.arg.1", "${name}.arg.2"); |
| 151 | + |
| 152 | + $task_list->run($task); |
| 153 | + |
| 154 | + my @summary = $task_list->get_summary; |
| 155 | + is_deeply $summary[-1]->{exit_code}, 0, $task->name; |
| 156 | + $run_list->increment_current_index; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +done_testing; |
0 commit comments