| 1 |
use Test2::V0 -target => 'App::Yath'; |
| 2 |
use Data::Dumper; |
| 3 |
use Carp; |
| 4 |
|
| 5 |
use App::Yath; |
| 6 |
|
| 7 |
use Test2::Harness::Util qw/clean_path/; |
| 8 |
use Test2::Tools::GenTemp qw/gen_temp/; |
| 9 |
|
| 10 |
$ENV{'YATH_SELF_TEST'} = 1; |
| 11 |
|
| 12 |
subtest init => sub { |
| 13 |
my $one = $CLASS->new(argv => [foo => 'bar']); |
| 14 |
isa_ok($one, $CLASS); |
| 15 |
|
| 16 |
isa_ok($one->settings, 'Test2::Harness::Settings'); |
| 17 |
|
| 18 |
is($one->settings->harness->script, clean_path(__FILE__), "Yath script set to this test file"); |
| 19 |
|
| 20 |
is($one->_argv, [foo => 'bar'], "Grabbed argv"); |
| 21 |
|
| 22 |
is($one->config, {}, "Default empty config"); |
| 23 |
|
| 24 |
my $two = App::Yath->new(); |
| 25 |
is($two->_argv, [], "default to empty argv"); |
| 26 |
}; |
| 27 |
|
| 28 |
{ |
| 29 |
require App::Yath::Command; |
| 30 |
$INC{'App/Yath/Command/NOGEN.pm'} = __FILE__; |
| 31 |
$INC{'App/Yath/Command/GEN.pm'} = __FILE__; |
| 32 |
|
| 33 |
package App::Yath::Command::NOGEN; |
| 34 |
use App::Yath::Options; |
| 35 |
|
| 36 |
option 'verbose' => ( |
| 37 |
type => 'c', |
| 38 |
prefix => 'foo', |
| 39 |
short => 'v', |
| 40 |
); |
| 41 |
post sub { $main::POST++ }; |
| 42 |
|
| 43 |
use Test2::Harness::Util::HashBase qw/settings argv/; |
| 44 |
our @ISA = ('App::Yath::Command'); |
| 45 |
|
| 46 |
sub run { 123 } |
| 47 |
|
| 48 |
package App::Yath::Command::GEN; |
| 49 |
|
| 50 |
our @ISA = ('App::Yath::Command::NOGEN'); |
| 51 |
|
| 52 |
sub generate_run_sub { ('ran gen_run_sub', @_) } |
| 53 |
} |
| 54 |
|
| 55 |
subtest generate_run_sub => sub { |
| 56 |
my $one = $CLASS->new(argv => ['GEN']); |
| 57 |
|
| 58 |
my @out = $one->generate_run_sub('main::RUNSUB'); |
| 59 |
is( |
| 60 |
\@out, |
| 61 |
[ |
| 62 |
'ran gen_run_sub', |
| 63 |
'App::Yath::Command::GEN', |
| 64 |
'main::RUNSUB', |
| 65 |
[], |
| 66 |
exact_ref($one->settings), |
| 67 |
['GEN'], |
| 68 |
], |
| 69 |
"Ran command generate_run_sub with correct args" |
| 70 |
); |
| 71 |
|
| 72 |
my $two = $CLASS->new(argv => ['NOGEN', '-vv']); |
| 73 |
|
| 74 |
$two->generate_run_sub('main::RUNSUB'); |
| 75 |
is($two->settings->foo->verbose, 2, "Set verbose with CLI args"); |
| 76 |
ok(defined(&main::RUNSUB), "Added the runsub to the provided symbol"); |
| 77 |
is(main::RUNSUB(), 123, "runsub does what we expect (runs the command run method) and we get the exit value"); |
| 78 |
is($main::POST, 1, "Ran post-process callbacks"); |
| 79 |
}; |
| 80 |
|
| 81 |
subtest run_command => sub { |
| 82 |
my $one = $CLASS->new(); |
| 83 |
|
| 84 |
my $cmd = mock {run => undef, name => 'acmd'}; |
| 85 |
|
| 86 |
is( |
| 87 |
dies { $one->run_command($cmd) }, |
| 88 |
"Command 'acmd' did not return an exit value.\n", |
| 89 |
"Command must return an exit value" |
| 90 |
); |
| 91 |
|
| 92 |
$cmd->{run} = 12; |
| 93 |
|
| 94 |
is($one->run_command($cmd), 12, "Returned the proper exit code"); |
| 95 |
}; |
| 96 |
|
| 97 |
subtest command_class => sub { |
| 98 |
my $one = $CLASS->new(argv => ['GEN']); |
| 99 |
is($one->command_class, 'App::Yath::Command::GEN', "Got command class from args"); |
| 100 |
|
| 101 |
$one->{_command_class} = 'foo'; |
| 102 |
|
| 103 |
is($one->command_class, "foo", "A cache is used"); |
| 104 |
}; |
| 105 |
|
| 106 |
subtest load_command => sub { |
| 107 |
my $one = $CLASS->new(); |
| 108 |
|
| 109 |
is($one->load_command('GEN'), 'App::Yath::Command::GEN', "Works for valid command (inline)"); |
| 110 |
is($one->load_command('test'), 'App::Yath::Command::test', "Works for valid command (real)"); |
| 111 |
|
| 112 |
is($one->load_command('gsdfgsdfgsd', check_only => 1), undef, "Missing module is ok in 'check_only' mode"); |
| 113 |
|
| 114 |
is( |
| 115 |
dies { $one->load_command('dgfsdfgsdf') }, |
| 116 |
"yath command 'dgfsdfgsdf' not found. (did you forget to install App::Yath::Command::dgfsdfgsdf?)\n", |
| 117 |
"Correct message for missing command" |
| 118 |
); |
| 119 |
|
| 120 |
is( |
| 121 |
dies { |
| 122 |
local @INC = (sub { die "module failed\n" }); |
| 123 |
$one->load_command('jgjgjfdfk'); |
| 124 |
}, |
| 125 |
"module failed\n", |
| 126 |
"If a module load throws an exception we pass it along" |
| 127 |
); |
| 128 |
}; |
| 129 |
|
| 130 |
subtest load_options => sub { |
| 131 |
local @INC = (@INC, 't/lib'); |
| 132 |
my $one = $CLASS->new(); |
| 133 |
|
| 134 |
$one->settings->harness->field(no_scan_plugins => 1); |
| 135 |
|
| 136 |
my $options = $one->load_options(); |
| 137 |
is( |
| 138 |
$options->included, |
| 139 |
{ |
| 140 |
'App::Yath::Options::Debug' => 1, |
| 141 |
'App::Yath::Options::PreCommand' => 1, |
| 142 |
}, |
| 143 |
"Included Debug and PreCommand, but not plugins" |
| 144 |
); |
| 145 |
|
| 146 |
my $two = $CLASS->new(); |
| 147 |
|
| 148 |
$two->settings->harness->field(no_scan_plugins => 0); |
| 149 |
|
| 150 |
my @ignore = warns { $options = $two->load_options() }; |
| 151 |
like( |
| 152 |
$options->included, |
| 153 |
{ |
| 154 |
'App::Yath::Options::Debug' => 1, |
| 155 |
'App::Yath::Options::PreCommand' => 1, |
| 156 |
'App::Yath::Plugin::Options' => 1, |
| 157 |
}, |
| 158 |
"Included Debug and PreCommand, as well as the plugin" |
| 159 |
); |
| 160 |
|
| 161 |
ref_is($options, $two->load_options, "Cached options result"); |
| 162 |
}; |
| 163 |
|
| 164 |
subtest load_options_conflict => sub { |
| 165 |
# Two plugins, neither of them ours, that lay claim to the same option name. |
| 166 |
# Nothing can include both, but yath must still run. |
| 167 |
my $plugin = sub { |
| 168 |
my ($name) = @_; |
| 169 |
|
| 170 |
return join "\n" => ( |
| 171 |
"package App::Yath::Plugin::$name;", |
| 172 |
"use strict;", |
| 173 |
"use warnings;", |
| 174 |
"use App::Yath::Options;", |
| 175 |
"option collide => (prefix => 'conflict', type => 'b');", |
| 176 |
"1;", |
| 177 |
"", |
| 178 |
); |
| 179 |
}; |
| 180 |
|
| 181 |
my $tmp = gen_temp( |
| 182 |
App => { |
| 183 |
Yath => { |
| 184 |
Plugin => { |
| 185 |
'ConflictA.pm' => $plugin->('ConflictA'), |
| 186 |
'ConflictB.pm' => $plugin->('ConflictB'), |
| 187 |
}, |
| 188 |
}, |
| 189 |
}, |
| 190 |
); |
| 191 |
|
| 192 |
local @INC = ($tmp, @INC); |
| 193 |
|
| 194 |
my $one = $CLASS->new(); |
| 195 |
$one->settings->harness->field(no_scan_plugins => 0); |
| 196 |
|
| 197 |
my $options; |
| 198 |
my $warnings = warnings { $options = $one->load_options() }; |
| 199 |
|
| 200 |
like( |
| 201 |
$warnings, |
| 202 |
bag { |
| 203 |
item match qr/Failed to include options from module .*ConflictB\.pm.*Option 'conflict-collide' was already defined/s; |
| 204 |
etc; |
| 205 |
}, |
| 206 |
"Warned about the plugin that could not be included" |
| 207 |
); |
| 208 |
|
| 209 |
like( |
| 210 |
$options->included, |
| 211 |
{ |
| 212 |
'App::Yath::Options::Debug' => 1, |
| 213 |
'App::Yath::Options::PreCommand' => 1, |
| 214 |
'App::Yath::Plugin::ConflictA' => 1, |
| 215 |
}, |
| 216 |
"Included everything up to the conflict" |
| 217 |
); |
| 218 |
|
| 219 |
ok(!$options->included->{'App::Yath::Plugin::ConflictB'}, "Did not include the conflicting plugin"); |
| 220 |
}; |
| 221 |
|
| 222 |
subtest process_argv => sub { |
| 223 |
local @INC = (@INC, 't/lib'); |
| 224 |
|
| 225 |
my $one = $CLASS->new( |
| 226 |
argv => [qw/-Dfoo -Dbar fake -x -y blah uhg/], |
| 227 |
config => {fake => [qw/-Dbaz -z/], other => [qw/-noop/]}, |
| 228 |
); |
| 229 |
|
| 230 |
my @ignore = warns { is($one->process_argv(), $one->_argv, "remaining args are returned") }; |
| 231 |
|
| 232 |
is($one->command_class, 'App::Yath::Command::fake', "Set command class"); |
| 233 |
is( |
| 234 |
${$one->settings->fake}, |
| 235 |
{ |
| 236 |
x => 1, |
| 237 |
y => 1, |
| 238 |
z => 1, |
| 239 |
}, |
| 240 |
"Added 'fake' command settings" |
| 241 |
); |
| 242 |
|
| 243 |
like( |
| 244 |
$one->settings->harness->dev_libs, |
| 245 |
bag { |
| 246 |
item qr/foo$/; |
| 247 |
item qr/bar$/; |
| 248 |
item qr/baz$/; |
| 249 |
}, |
| 250 |
"Added the dev libs" |
| 251 |
); |
| 252 |
|
| 253 |
is($one->_argv, [qw/blah uhg/], "Remaining args"); |
| 254 |
|
| 255 |
no warnings 'once'; |
| 256 |
is($main::POST_HOOK, F(), "Did not run hook yet (requires command instance)"); |
| 257 |
}; |
| 258 |
|
| 259 |
subtest command_from_argv => sub { |
| 260 |
my $one = $CLASS->new(); |
| 261 |
$one->settings->harness->vivify_field('persist_file'); |
| 262 |
$one->settings->harness->vivify_field('project'); |
| 263 |
$one->settings->harness->vivify_field('persist_dir'); |
| 264 |
|
| 265 |
like( |
| 266 |
warning { is($one->_command_from_argv, 'test', "Default to test") }, |
| 267 |
qr/Defaulting to the 'test' command/, |
| 268 |
"Warning about default" |
| 269 |
); |
| 270 |
|
| 271 |
my $control = mock $CLASS => ( override => [ find_pfile => sub { 1 } ] ); |
| 272 |
like( |
| 273 |
warning { is($one->_command_from_argv, 'run', "Default to run if we have a persistence file") }, |
| 274 |
qr/Persistent runner detected, defaulting to the 'run' command/, |
| 275 |
"Warning about default" |
| 276 |
); |
| 277 |
$control = undef; |
| 278 |
|
| 279 |
$one = $CLASS->new(argv => ['-f', '--foo', 'test', '-b', '--bar']); |
| 280 |
$one->settings->harness->vivify_field('persist_file'); |
| 281 |
$one->settings->harness->vivify_field('project'); |
| 282 |
$one->settings->harness->vivify_field('persist_dir'); |
| 283 |
is($one->_command_from_argv(), "test", "Found 'test' command"); |
| 284 |
is($one->_argv, ['-f', '--foo', '-b', '--bar'], "Command was removed from argv"); |
| 285 |
|
| 286 |
$one = $CLASS->new(argv => ['-f', '--foo', 'hfajhdajshfj', '-b', '--bar']); |
| 287 |
$one->settings->harness->vivify_field('persist_file'); |
| 288 |
$one->settings->harness->vivify_field('project'); |
| 289 |
$one->settings->harness->vivify_field('persist_dir'); |
| 290 |
is($one->_command_from_argv(), "hfajhdajshfj", "Found 'hfajhdajshfj' command"); |
| 291 |
is($one->_argv, ['-f', '--foo', '-b', '--bar'], "Command was removed from argv"); |
| 292 |
|
| 293 |
$one = $CLASS->new(argv => ['-f', '--foo', '--help', '-b', '--bar']); |
| 294 |
$one->settings->harness->vivify_field('persist_file'); |
| 295 |
$one->settings->harness->vivify_field('project'); |
| 296 |
$one->settings->harness->vivify_field('persist_dir'); |
| 297 |
is($one->_command_from_argv(), "help", "Found 'help' command"); |
| 298 |
is($one->_argv, ['-f', '--foo', '-b', '--bar'], "Command was removed from argv"); |
| 299 |
|
| 300 |
$one = $CLASS->new(argv => ['-f', '--foo', '-h', '-b', '--bar']); |
| 301 |
$one->settings->harness->vivify_field('persist_file'); |
| 302 |
$one->settings->harness->vivify_field('project'); |
| 303 |
$one->settings->harness->vivify_field('persist_dir'); |
| 304 |
is($one->_command_from_argv(), "help", "Found 'help' command"); |
| 305 |
is($one->_argv, ['-f', '--foo', '-b', '--bar'], "Command was removed from argv"); |
| 306 |
|
| 307 |
$one = $CLASS->new(argv => ['-f', '--foo', 'foo.jsonl.bz2', '-b', '--bar']); |
| 308 |
$one->settings->harness->vivify_field('persist_file'); |
| 309 |
$one->settings->harness->vivify_field('project'); |
| 310 |
$one->settings->harness->vivify_field('persist_dir'); |
| 311 |
my @ignore = warns { is($one->_command_from_argv(), "replay", "Found 'replay' command because we got a log") }; |
| 312 |
is($one->_argv, ['-f', '--foo', 'foo.jsonl.bz2', '-b', '--bar'], "log was not removed from argv"); |
| 313 |
|
| 314 |
$one = $CLASS->new(argv => ['-f', '--foo', __FILE__, '-b', '--bar']); |
| 315 |
$one->settings->harness->vivify_field('persist_file'); |
| 316 |
$one->settings->harness->vivify_field('project'); |
| 317 |
$one->settings->harness->vivify_field('persist_dir'); |
| 318 |
@ignore = warns { is($one->_command_from_argv(), "test", "Found 'test' command because we got a path") }; |
| 319 |
is($one->_argv, ['-f', '--foo', __FILE__, '-b', '--bar'], "path was not removed"); |
| 320 |
}; |
| 321 |
|
| 322 |
done_testing; |