• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gugod / App-perlbrew / 10641658258

31 Aug 2024 02:27AM UTC coverage: 80.317% (+0.1%) from 80.187%
10641658258

push

github

3191 of 3973 relevant lines covered (80.32%)

80.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.05
/t/command-exec.t
1
#!/usr/bin/env perl
2
use Test2::V0;
1✔
3
use Test2::Tools::Spec;
1✔
4
use Test2::Tools::Compare qw(bag);
5

1✔
6
use FindBin;
1✔
7
use lib $FindBin::Bin;
1✔
8
use App::perlbrew;
×
9
require 'test2_helpers.pl';
1✔
10
use PerlbrewTestHelpers qw(stderr_is);
11

×
12
mock_perlbrew_install("perl-5.12.3");
×
13
mock_perlbrew_install("perl-5.12.4");
1✔
14
mock_perlbrew_install("perl-5.14.1");
1✔
15
mock_perlbrew_install("perl-5.14.2");
16

17
describe 'perlbrew exec perl -E "say 42"' => sub {
18
    it "invokes all perls" => sub {
19
        mocked(
20
            App::perlbrew->new(qw(exec perl -E), "say 42"),
21
            sub {
1✔
22
                my ($mock, $app) = @_;
23

1✔
24
                my @perls = $app->installed_perls;
25

26
                $mock->expects("do_system_with_exit_code")->exactly(4)->returns(
27
                    sub {
1✔
28
                        my ($self, @args) = @_;
29

1✔
30
                        is \@args, ["perl", "-E", "say 42"], "arguments";
31

1✔
32
                        my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});
33

4✔
34
                        my $perl_installation = shift @perls;
35

4✔
36
                        is $perlbrew_bin_path,      App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "bin")->stringify(), "perlbrew_bin_path";
4✔
37
                        is $perlbrew_perl_bin_path, App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", $perl_installation->{name}, "bin")->stringify(), "perls/". $perl_installation->{name} . "/bin";
38

4✔
39
                        return 0;
40
                    }
1✔
41
                );
42

4✔
43
                $app->run;
44
            }
1✔
45
        );
1✔
46
    };
1✔
47
};
48

49
describe 'perlbrew exec --with perl-5.12.3 perl -E "say 42"' => sub {
50
    it "invokes perl-5.12.3/bin/perl" => sub {
51
        mocked(
52
            App::perlbrew->new(qw(exec --with perl-5.12.3 perl -E), "say 42"),
53
            sub {
1✔
54
                my ($mock, $app) = @_;
55

56
                $mock->expects("do_system_with_exit_code")->returns(
57
                    sub {
1✔
58
                        my ($self, @args) = @_;
59

1✔
60
                        is \@args, ["perl", "-E", "say 42"];
61

1✔
62
                        my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});
63

1✔
64
                        is $perlbrew_bin_path,      App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "bin")->stringify;
1✔
65
                        is $perlbrew_perl_bin_path, App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.12.3", "bin")->stringify;
66

1✔
67
                        return 0;
68
                    }
1✔
69
                );
70

1✔
71
                $app->run;
72
            }
1✔
73
        );
4✔
74
    };
4✔
75
};
76

77
describe 'perlbrew exec --with perl-5.14.1,perl-5.12.3,perl-5.14.2 perl -E "say 42"' => sub {
78
    it "invokes each perl in the specified order" => sub {
79
        mocked(
80
            App::perlbrew->new(qw(exec --with), "perl-5.14.1 perl-5.12.3 perl-5.14.2", qw(perl -E), "say 42"),
81
            sub {
1✔
82
                my ($mock, $app) = @_;
83

1✔
84
                my @perl_paths;
85

86
                $mock->expects("do_system_with_exit_code")->exactly(3)->returns(
87
                    sub {
1✔
88
                        my ($self, @args) = @_;
1✔
89
                        my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});
1✔
90
                        push @perl_paths, $perlbrew_perl_bin_path;
3✔
91
                        return 0;
92
                    }
1✔
93
                );
94

3✔
95
                $app->run;
96

3✔
97
                is \@perl_paths, [
98
                    App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.14.1", "bin")->stringify,
99
                    App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.12.3", "bin")->stringify,
100
                    App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.14.2", "bin")->stringify,
101
                ];
102
            }
1✔
103
        );
1✔
104
    };
1✔
105
};
106

107
describe 'perlbrew exec --with perl-5.14.1,perl-foobarbaz, ' => sub {
108
    it "ignore the unrecognized 'perl-foobarbaz'" => sub {
109
        mocked(
110
            App::perlbrew->new(qw(exec --with), "perl-5.14.1 perl-foobarbaz", qw(perl -E), "say 42"),
111
            sub {
1✔
112
                my ($mock, $app) = @_;
113

1✔
114
                my @perl_paths;
115

116
                $mock->expects("do_system_with_exit_code")->returns(
117
                    sub {
1✔
118
                        my ($self, @args) = @_;
1✔
119
                        my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});
1✔
120
                        push @perl_paths, $perlbrew_perl_bin_path;
1✔
121
                        return 0;
122
                    }
1✔
123
                );
124

1✔
125
                $app->run;
126

1✔
127
                is \@perl_paths, [
128
                    App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.14.1", "bin")->stringify(),
129
                ];
130
            },
131
        )
1✔
132
    };
3✔
133
};
134

135
describe 'perlbrew exec --with perl-5.14.1,5.14.1 ' => sub {
136
    it "exec 5.14.1 twice, since that is what is specified" => sub {
137
        mocked(
138
            App::perlbrew->new(qw(exec --with), "perl-5.14.1 5.14.1", qw(perl -E), "say 42"),
139
            sub {
1✔
140
                my ($mock, $app) = @_;
141

1✔
142
                my @perl_paths;
143

144
                $mock->expects("do_system_with_exit_code")->exactly(2)->returns(
145
                    sub {
1✔
146
                        my ($self, @args) = @_;
1✔
147
                        my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});
1✔
148
                        push @perl_paths, $perlbrew_perl_bin_path;
2✔
149
                        return 0;
150
                    }
1✔
151
                );
152

2✔
153
                $app->run;
154

2✔
155
                is \@perl_paths, [
156
                    App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.14.1", "bin")->stringify,
157
                    App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.14.1", "bin")->stringify,
158
                ];
159
            }
160
        )
1✔
161
    };
1✔
162
};
163

164
describe 'exec exit code' => sub {
165
    describe "logging" => sub {
166
        it "should work" => sub {
167
            mocked(
168
                App::perlbrew->new(qw(exec --with), "perl-5.14.1", qw(perl -E), "somesub 42"),
169
                sub {
1✔
170
                    my ($mock, $app) = @_;
171

1✔
172
                    $mock->expects("format_info_output")->exactly(1)->returns("format_info_output_value\n");
1✔
173
                    $mock->expects("do_system_with_exit_code")->exactly(1)->returns(7<<8);
174

1✔
175
                    my $mock2 = mocked('App::perlbrew');
1✔
176
                    $mock2->expects("do_exit_with_error_code")->exactly(1)->returns(sub { die "simulate exit\n" });
177

178
                    stderr_is sub {
1✔
179
                        eval { $app->run; 1; };
1✔
180
                    }, <<"OUT";
181
Command [perl -E 'somesub 42'] terminated with exit code 7 (\$? = 1792) under the following perl environment:
182
format_info_output_value
183
OUT
184

1✔
185
                    $mock2->verify;
186
                }
187
            )
1✔
188
        };
189

190
        it "should be quiet if asked" => sub {
×
191
            my $app = App::perlbrew->new(qw(exec --quiet --with), "perl-5.14.1", qw(perl -E), "somesub 42");
192

1✔
193
            my $mock = mocked($app);
1✔
194
            $mock->expects("format_info_output")->exactly(0)->returns('should not be called!');
1✔
195
            $mock->expects("do_system_with_exit_code")->exactly(1)->returns(7<<8);
196

1✔
197
            my $mock2 = mocked('App::perlbrew');
1✔
198
            $mock2->expects("do_exit_with_error_code")->exactly(1)->returns(sub { die "simulate exit\n" });
199

200
            stderr_is sub {
1✔
201
                eval { $app->run; 1; };
1✔
202
            }, '';
203

1✔
204
            $mock->verify;
1✔
205
            $mock2->verify;
1✔
206
        };
207

208
        it "should format info output for right perl" => sub {
1✔
209
            my $app = App::perlbrew->new(qw(exec --with), "perl-5.14.1", qw(perl -E), "somesub 42");
210

1✔
211
            my $mock = mocked($app);
212
            $mock->expects("format_info_output")->exactly(1)->returns(sub {
1✔
213
                my ($self) = @_;
1✔
214
                is $self->current_env, 'perl-5.14.1';
1✔
215
                like $self->installed_perl_executable('perl-5.14.1'), qr/perl-5.14.1/;
1✔
216
                "format_info_output_value\n";
1✔
217
            });
1✔
218
            $mock->expects("do_system_with_exit_code")->exactly(1)->returns(7<<8);
219

1✔
220
            my $mock2 = mocked('App::perlbrew');
221
            $mock2->expects("do_exit_with_error_code")->exactly(1)->returns(sub {
1✔
222
                die "simulate exit\n";
1✔
223
            });
224

1✔
225
            eval { $app->run; 1; };
226

1✔
227
            $mock->verify;
1✔
228
            $mock2->verify;
×
229
        };
1✔
230
    };
231

232
    describe "no halt-on-error" => sub {
233
        it "should exit with success code when several perls ran" => sub {
1✔
234
            my $mock2 = mocked('App::perlbrew')->expects("do_exit_with_error_code")->never;
235

236
            mocked(
237
                App::perlbrew->new(qw(exec --with), "perl-5.14.1 perl-5.14.1", qw(perl -E), "say 42"),
238
                sub {
1✔
239
                    my ($mock, $app) = @_;
1✔
240
                    $mock->expects("do_system_with_exit_code")->exactly(2)->returns(0);
1✔
241
                    $app->run;
242
                }
1✔
243
            );
244

1✔
245
            $mock2->verify;
1✔
246
        };
247

248
        it "should exit with error code " => sub {
1✔
249
            my $app = App::perlbrew->new(qw(exec --with), "perl-5.14.1", qw(perl -E), "say 42");
250

1✔
251
            my $mock = mocked($app);
1✔
252
            $mock->expects("format_info_output")->exactly(1)->returns('');
1✔
253
            $mock->expects("do_system_with_exit_code")->exactly(1)->returns(3<<8);
254

255
            my $mock2 = mocked('App::perlbrew')->expects("do_exit_with_error_code")->exactly(1)->returns(sub {
1✔
256
                my ($self, $code) = @_;
1✔
257
                is $code, 1; # exit with error, but don't propogate exact failure codes
1✔
258
                die "simulate exit\n";
1✔
259
            });
260

261

1✔
262
            ok !eval { $app->run; 1; };
1✔
263
            is $@, "simulate exit\n";
264

1✔
265
            $mock->verify;
×
266
            $mock2->verify;
1✔
267
        };
268

269
        it "should exit with error code when several perls ran" => sub {
1✔
270
            my $app = App::perlbrew->new(qw(exec --with), "perl-5.14.1 perl-5.14.1", qw(perl -E), "say 42");
271

1✔
272
            my $mock = mocked($app);
1✔
273
            $mock->expects("format_info_output")->exactly(1)->returns('');
1✔
274
            my $calls = 0;
275
            $mock->expects("do_system_with_exit_code")->exactly(2)->returns(sub {
1✔
276
                $calls++;
1✔
277
                return 0 if ($calls == 2); # second exec call successed
1✔
278
                return 3<<8; # first exec failed
1✔
279
            });
280

281
            my $mock2 = mocked('App::perlbrew')->expects("do_exit_with_error_code")->exactly(1)->returns(sub {
2✔
282
                my ($self, $code) = @_;
1✔
283
                is $code, 1; # exit with error, but don't propogate exact failure codes
1✔
284
                die "simulate exit\n";
2✔
285
            });
286

1✔
287
            ok !eval { $app->run; 1; };
1✔
288
            is $@, "simulate exit\n";
289

1✔
290
            $mock->verify;
×
291
            $mock2->verify;
1✔
292
        };
×
293
    };
294

295
    describe "halt-on-error" => sub {
296
        it "should exit with success code " => sub {
1✔
297
            my $app = App::perlbrew->new(qw(exec --halt-on-error --with), "perl-5.14.1", qw(perl -E), "say 42");
1✔
298
            my $mock = mocked('App::perlbrew')->expects("do_exit_with_error_code")->never;
1✔
299
            my $mock2 = mocked($app)->expects("do_system_with_exit_code")->exactly(1)->returns(0);
1✔
300
            $app->run;
1✔
301
            $mock->verify;
1✔
302
            $mock2->verify;
1✔
303
        };
304

305
        it "should exit with error code " => sub {
1✔
306
            my $app = App::perlbrew->new(qw(exec --halt-on-error --with), "perl-5.14.1", qw(perl -E), "say 42");
307

1✔
308
            my $mock = mocked($app);
1✔
309
            $mock->expects("format_info_output")->exactly(1)->returns('');
1✔
310
            $mock->expects("do_system_with_exit_code")->exactly(1)->returns(3<<8);
311

312
            my $mock2 = mocked('App::perlbrew')->expects("do_exit_with_error_code")->exactly(1)->returns(sub {
1✔
313
                my ($self, $code) = @_;
1✔
314
                is $code, 3;
1✔
315
                die "simulate exit\n";
1✔
316
            });
317

1✔
318
            ok !eval { $app->run; 1; };
1✔
319
            is $@, "simulate exit\n";
320

1✔
321
            $mock->verify;
×
322
            $mock2->verify;
1✔
323
        };
324

325
        it "should exit with code 255 if program terminated with signal or something" => sub {
1✔
326
            my $app = App::perlbrew->new(qw(exec --halt-on-error --with), "perl-5.14.1", qw(perl -E), "say 42");
327

1✔
328
            my $mock = mocked($app);
1✔
329
            $mock->expects("format_info_output")->exactly(1)->returns('');
1✔
330
            $mock->expects("do_system_with_exit_code")->exactly(1)->returns(-1);
331

332
            my $mock2 = mocked('App::perlbrew')->expects("do_exit_with_error_code")->exactly(1)->returns(sub {
1✔
333
                my ($self, $code) = @_;
1✔
334
                is $code, 255;
1✔
335
                die "simulate exit\n";
1✔
336
            });
337

1✔
338
            ok !eval { $app->run; 1; };
1✔
339
            is $@, "simulate exit\n";
340

1✔
341
            $mock->verify;
×
342
            $mock2->verify;
1✔
343
        };
344

345
        it "should exit with error code when several perls ran" => sub {
1✔
346
            my $app = App::perlbrew->new(qw(exec --halt-on-error --with), "perl-5.14.1 perl-5.14.1", qw(perl -E), "say 42");
347

1✔
348
            my $mock = mocked($app);
1✔
349
            $mock->expects("format_info_output")->exactly(1)->returns('');
1✔
350
            my $calls = 0;
351
            $mock->expects("do_system_with_exit_code")->exactly(2)->returns(sub {
1✔
352
                $calls++;
1✔
353
                return 7<<8 if $calls == 2;
1✔
354
                return 0;
1✔
355
            });
356

357
            my $mock2 = mocked('App::perlbrew')->expects("do_exit_with_error_code")->exactly(1)->returns(sub {
2✔
358
                my ($self, $code) = @_;
1✔
359
                is $code, 7;
1✔
360
                die "simulate exit\n";
2✔
361
            });
362

1✔
363
            ok !eval { $app->run; 1; };
1✔
364
            is $@, "simulate exit\n";
365

1✔
366
            $mock->verify;
×
367
            $mock2->verify;
1✔
368
        };
1✔
369
    };
2✔
370
};
371

372
describe "minimal perl version" => sub {
373
    it "only executes the needed version" => sub {
1✔
374
        my @perl_paths;
1✔
375
        my $app = App::perlbrew->new(qw(exec --min 5.014), qw(perl -E), "say 42");
376

377
        my $mock = mocked($app)->expects("do_system_with_exit_code")->exactly(2)->returns(sub {
1✔
378
            my ($self, @args) = @_;
1✔
379
            my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});
1✔
380
            push @perl_paths, $perlbrew_perl_bin_path;
2✔
381
            return 0;
1✔
382
        });
383

2✔
384
        $app->run;
385

386
        is \@perl_paths, bag {
2✔
387
            item(App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.14.2", "bin")->stringify());
388
            item(App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.14.1", "bin")->stringify());
389
            end();
390
        };
391

2✔
392
        $mock->verify;
1✔
393
    };
1✔
394
};
395

396
describe "maximum perl version" => sub {
397
    it "only executes the needed version" => sub {
1✔
398
        my @perl_paths;
1✔
399
        my $app = App::perlbrew->new(qw(exec --max 5.014), qw(perl -E), "say 42");
400

401
        my $mock = mocked($app)->expects("do_system_with_exit_code")->exactly(2)->returns(sub {
1✔
402
            my ($self, @args) = @_;
1✔
403
            my ($perlbrew_bin_path, $perlbrew_perl_bin_path, @paths) = split(":", $ENV{PATH});
1✔
404
            push @perl_paths, $perlbrew_perl_bin_path;
2✔
405
            return 0;
1✔
406
        });
407

2✔
408
        $app->run;
409

410
        # Don't care about the order, just the fact all of them were visited
2✔
411
        is \@perl_paths, bag {
412
            item(App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.12.4", "bin")->stringify());
413
            item(App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT, "perls", "perl-5.12.3", "bin")->stringify());
414
        };
415

2✔
416
        $mock->verify;
1✔
417
    };
1✔
418
};
419

1✔
420
done_testing;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc