Debian Codegraph @

t/unit/Test2/Harness/Runner/Job.t

raw
1 use Test2::V0 -target => 'Test2::Harness::Runner::Job';
2 # HARNESS-DURATION-SHORT
3 # HARNESS-NO-PRELOAD
4
5 use File::Temp qw/tempdir/;
6 use File::Spec;
7 use Test2::Harness::Settings;
8
9 my $tmp = tempdir(CLEANUP => 1);
10
11 {
12 package MockRunner;
13 sub new { bless {}, shift }
14 sub all_libs { () }
15 }
16
17 {
18 package MockRun;
19 sub new { bless {}, shift }
20 sub run_dir { $tmp }
21 sub run_id { 'mock-run' }
22 sub test_args { undef }
23 }
24
25 sub make_settings {
26 my (%overrides) = @_;
27
28 my $settings = Test2::Harness::Settings->new();
29 $settings->define_prefix('debug');
30 $settings->debug->vivify_field('dummy');
31 $settings->debug->field(dummy => $overrides{dummy} // 0);
32
33 $settings->define_prefix('runner');
34 for my $field (qw/fail_on_resource_skip nytprof/) {
35 $settings->runner->vivify_field($field);
36 }
37 $settings->runner->field(fail_on_resource_skip => $overrides{fail_on_resource_skip} // 0);
38 $settings->runner->field(nytprof => 0);
39
40 return $settings;
41 }
42
43 sub make_job {
44 my (%args) = @_;
45
46 my $job_dir = File::Spec->catdir($tmp, "job_$$" . ++$main::JOB_COUNT);
47 mkdir $job_dir;
48
49 # Create required files in job_dir so open_file calls succeed
50 for my $f (qw/stdout stderr/) {
51 open my $fh, '>', File::Spec->catfile($job_dir, $f) or die $!;
52 close $fh;
53 }
54 # Create stdin file
55 open my $fh, '>', File::Spec->catfile($job_dir, 'stdin') or die $!;
56 close $fh;
57
58 my $settings = $args{settings} // make_settings(%{$args{settings_overrides} // {}});
59
60 my $task = $args{task} // {
61 job_id => "job-$$-$main::JOB_COUNT",
62 file => __FILE__,
63 %{$args{task_overrides} // {}},
64 };
65
66 my $runner = $args{runner} // MockRunner->new;
67 my $run = $args{run} // MockRun->new;
68
69 my $job = $CLASS->new(
70 task => $task,
71 runner => $runner,
72 run => $run,
73 settings => $settings,
74 );
75
76 # Pre-set job_dir so file operations work
77 $job->{+Test2::Harness::Runner::Job::JOB_DIR()} = $job_dir;
78
79 return $job;
80 }
81
82 # Create shared temp files for mock file handles
83 my $devnull = File::Spec->catfile($tmp, 'devnull');
84 open my $dn, '>', $devnull or die $!;
85 close $dn;
86
87 # Mock methods on the Job class that spawn_params calls, so we don't
88 # need to set up the full settings/runner/run chain.
89 my $mock = mock $CLASS => (
90 override => [
91 cli_includes => sub { () },
92 switches => sub { () },
93 cli_options => sub { () },
94 args => sub { () },
95 ch_dir => sub { '' },
96 env_vars => sub { {} },
97 out_file => sub { $devnull },
98 err_file => sub { $devnull },
99 in_file => sub { $devnull },
100 ],
101 );
102
103 subtest 'via returns undef for resource_skip' => sub {
104 my $job = make_job(
105 task_overrides => {resource_skip => ['SomeResource']},
106 settings_overrides => {dummy => 0},
107 );
108
109 is($job->via, undef, "via() returns undef when task has resource_skip");
110 };
111
112 subtest 'spawn_params with resource_skip (default: skip)' => sub {
113 my $job = make_job(
114 task_overrides => {resource_skip => ['Port', 'Database']},
115 settings_overrides => {fail_on_resource_skip => 0},
116 );
117
118 my $params = $job->spawn_params;
119 my $command = $params->{command};
120
121 # Find the -e argument in the command array
122 my ($e_idx) = grep { !ref($command->[$_]) && $command->[$_] eq '-e' } 0 .. $#$command;
123
124 ok(defined $e_idx, "Found -e flag in command");
125 my $script = $command->[$e_idx + 1];
126 like($script, qr/1\.\.0 # SKIP/, "Generates TAP skip output");
127 like($script, qr/Some resources are not available: Port, Database/, "Skip message lists unavailable resources");
128 unlike($script, qr/not ok/, "Does not contain a failure");
129 };
130
131 subtest 'spawn_params with resource_skip and --fail-on-resource-skip' => sub {
132 my $job = make_job(
133 task_overrides => {resource_skip => ['Port', 'Database']},
134 settings_overrides => {fail_on_resource_skip => 1},
135 );
136
137 my $params = $job->spawn_params;
138 my $command = $params->{command};
139
140 my ($e_idx) = grep { !ref($command->[$_]) && $command->[$_] eq '-e' } 0 .. $#$command;
141
142 ok(defined $e_idx, "Found -e flag in command");
143 my $script = $command->[$e_idx + 1];
144 like($script, qr/1\.\.1/, "Generates TAP with 1 test planned");
145 like($script, qr/not ok 1/, "Generates a TAP failure");
146 like($script, qr/Some resources are not available: Port, Database/, "Failure message lists unavailable resources");
147 unlike($script, qr/# SKIP/, "Does not contain a skip directive");
148 };
149
150 subtest 'spawn_params with no resource_skip runs test normally' => sub {
151 my $job = make_job(
152 settings_overrides => {fail_on_resource_skip => 1},
153 );
154
155 my $params = $job->spawn_params;
156 my $command = $params->{command};
157
158 # Should contain a coderef (the run_file callback), not -e
159 my $has_coderef = grep { ref($_) eq 'CODE' } @$command;
160 ok($has_coderef, "Command contains a CODE ref (run_file) when no resource_skip");
161
162 my $has_e = grep { !ref($_) && $_ eq '-e' } @$command;
163 ok(!$has_e, "Command does not contain -e flag");
164 };
165
166 subtest 'spawn_params dummy mode still skips even with fail_on_resource_skip' => sub {
167 my $job = make_job(
168 settings_overrides => {dummy => 1, fail_on_resource_skip => 1},
169 );
170
171 my $params = $job->spawn_params;
172 my $command = $params->{command};
173
174 my ($e_idx) = grep { !ref($command->[$_]) && $command->[$_] eq '-e' } 0 .. $#$command;
175
176 ok(defined $e_idx, "Found -e flag in command");
177 my $script = $command->[$e_idx + 1];
178 like($script, qr/1\.\.0 # SKIP dummy mode/, "Dummy mode produces a skip, not a failure");
179 unlike($script, qr/not ok/, "No failure for dummy mode");
180 };
181
182 subtest 'spawn_params resource_skip with single resource' => sub {
183 my $job = make_job(
184 task_overrides => {resource_skip => ['Port']},
185 settings_overrides => {fail_on_resource_skip => 1},
186 );
187
188 my $params = $job->spawn_params;
189 my $command = $params->{command};
190
191 my ($e_idx) = grep { !ref($command->[$_]) && $command->[$_] eq '-e' } 0 .. $#$command;
192
193 ok(defined $e_idx, "Found -e flag in command");
194 my $script = $command->[$e_idx + 1];
195 like($script, qr/not ok 1 - Some resources are not available: Port/, "Failure message with single resource");
196 };
197
198 subtest 'TAP output is valid when executed' => sub {
199 # Verify the generated -e scripts produce valid TAP when run
200
201 subtest 'skip TAP' => sub {
202 my $job = make_job(
203 task_overrides => {resource_skip => ['Port']},
204 settings_overrides => {fail_on_resource_skip => 0},
205 );
206
207 my $params = $job->spawn_params;
208 my $command = $params->{command};
209 my ($e_idx) = grep { !ref($command->[$_]) && $command->[$_] eq '-e' } 0 .. $#$command;
210 my $script = $command->[$e_idx + 1];
211
212 my $output = `$^X -e '$script'`;
213 is($output, "1..0 # SKIP Some resources are not available: Port", "Skip TAP output is correct");
214 };
215
216 subtest 'fail TAP' => sub {
217 my $job = make_job(
218 task_overrides => {resource_skip => ['Port']},
219 settings_overrides => {fail_on_resource_skip => 1},
220 );
221
222 my $params = $job->spawn_params;
223 my $command = $params->{command};
224 my ($e_idx) = grep { !ref($command->[$_]) && $command->[$_] eq '-e' } 0 .. $#$command;
225 my $script = $command->[$e_idx + 1];
226
227 my $output = `$^X -e '$script'`;
228 like($output, qr/^1\.\.1\nnot ok 1 - Some resources are not available: Port$/s, "Fail TAP output is correct");
229 };
230 };
231
232 done_testing;