1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use Test::More tests => 301;
#use Test::More 'no_plan';
use App::Sqitch;
use Path::Class;
use Test::Exception;
use Test::Warn;
use Test::File qw(file_exists_ok file_not_exists_ok);
use Test::File::Contents;
use Locale::TextDomain qw(App-Sqitch);
use File::Path qw(remove_tree);
use Test::NoWarnings;
use lib 't/lib';
use MockOutput;
use TestConfig;
my $CLASS = 'App::Sqitch::Command::bundle';
# Ignore user and system configs.
$ENV{SQITCH_USER_CONFIG} = $ENV{SQITCH_SYSTEM_CONFIG} = File::Spec->devnull;
ok my $sqitch = App::Sqitch->new, 'Load a sqitch object';
my $config = $sqitch->config;
isa_ok my $bundle = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'bundle',
config => $config,
}), $CLASS, 'bundle command';
can_ok $CLASS, qw(
configure
execute
from
to
dest_dir
dest_top_dir
dest_dirs_for
bundle_config
bundle_plan
bundle_scripts
_mkpath
_copy_if_modified
does
);
ok $CLASS->does("App::Sqitch::Role::ContextCommand"),
"$CLASS does ContextCommand";
is_deeply [$CLASS->options], [qw(
dest-dir|dir=s
all|a!
from=s
to=s
plan-file|f=s
top-dir=s
)], 'Should have dest_dir option';
warning_is {
Getopt::Long::Configure(qw(bundling pass_through));
ok Getopt::Long::GetOptionsFromArray(
[], {}, App::Sqitch->_core_opts, $CLASS->options,
), 'Should parse options';
} undef, 'Options should not conflict with core options';
is $bundle->dest_dir, dir('bundle'),
'Default dest_dir should be bundle/';
is $bundle->dest_top_dir($bundle->default_target), dir('bundle'),
'Should have dest top dir';
##############################################################################
# Test configure().
is_deeply $CLASS->configure($config, {}), {_cx => []},
'Default config should be empty';
is_deeply $CLASS->configure($config, {dest_dir => 'whu'}), {
dest_dir => dir('whu'),
_cx => [],
}, '--dest_dir should be converted to a path object by configure()';
is_deeply $CLASS->configure($config, {from => 'HERE', to => 'THERE'}), {
from => 'HERE',
to => 'THERE',
_cx => [],
}, '--from and --to should be passed through configure';
chdir 't';
$config= TestConfig->from(local => 'sqitch.conf');
$config->update('core.top_dir' => dir('sql')->stringify);
END { remove_tree 'bundle' if -d 'bundle' }
ok $sqitch = App::Sqitch->new(config => $config),
'Load a sqitch object with top_dir';
$config = $sqitch->config;
my $dir = dir qw(_build sql);
is_deeply $CLASS->configure($config, {}), {
dest_dir => $dir,
_cx => [],
}, 'bundle.dest_dir config should be converted to a path object by configure()';
##############################################################################
# Load a real project.
isa_ok $bundle = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'bundle',
config => $config,
}), $CLASS, 'another bundle command';
is $bundle->dest_dir, $dir, qq{dest_dir should be "$dir"};
is $bundle->dest_top_dir($bundle->default_target), dir(qw(_build sql sql)),
'Dest top dir should be _build/sql/sql/';
my $target = $bundle->default_target;
my $dir_for = $bundle->dest_dirs_for($target);
for my $sub (qw(deploy revert verify)) {
is $dir_for->{$sub}, $dir->subdir('sql', $sub),
"Dest $sub dir should be _build/sql/sql/$sub";
}
# Try engine project.
$config->update(
'core.top_dir' => dir('engine')->stringify,
'core.reworked_dir' => dir(qw(engine reworked))->stringify,
);
ok $sqitch = App::Sqitch->new(config => $config),
'Load a sqitch object with engine top_dir';
isa_ok $bundle = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'bundle',
config => $config,
}), $CLASS, 'engine bundle command';
$target = $bundle->default_target;
is $bundle->dest_dir, $dir, qq{dest_dir should again be "$dir"};
$dir_for = $bundle->dest_dirs_for($target);
for my $sub (qw(deploy revert verify)) {
is $dir_for->{$sub}, $dir->subdir('engine', $sub),
"Dest $sub dir should be _build/sql/engine/$sub";
}
##############################################################################
# Test _copy().
my $path = dir 'delete.me';
END { remove_tree $path->stringify if -e $path }
my $file = file qw(sql deploy roles.sql);
my $dest = file $path, qw(deploy roles.sql);
file_not_exists_ok $dest, "File $dest should not exist";
ok $bundle->_copy_if_modified($file, $dest), "Copy $file to $dest";
file_exists_ok $dest, "File $dest should now exist";
file_contents_identical $dest, $file;
is_deeply +MockOutput->get_debug, [
[' ', __x 'Created {file}', file => $dest->dir],
[' ', __x(
"Copying {source} -> {dest}",
source => $file,
dest => $dest
)],
], 'The mkdir and copy info should have been output';
# Copy it again.
ok $bundle->_copy_if_modified($file, $dest), "Copy $file to $dest again";
file_exists_ok $dest, "File $dest should still exist";
file_contents_identical $dest, $file;
my $out = MockOutput->get_debug;
is_deeply $out, [], 'Should have no debugging output' or diag explain $out;
# Make it old and copy it again.
utime 0, $file->stat->mtime - 1, $dest;
ok $bundle->_copy_if_modified($file, $dest), "Copy $file to old $dest";
file_exists_ok $dest, "File $dest should still be there";
file_contents_identical $dest, $file;
is_deeply +MockOutput->get_debug, [[' ', __x(
"Copying {source} -> {dest}",
source => $file,
dest => $dest
)]], 'Only copy message should again have been emitted';
# Copy a different file.
my $file2 = file qw(sql deploy users.sql);
$dest->remove;
ok $bundle->_copy_if_modified($file2, $dest), "Copy $file2 to $dest";
file_exists_ok $dest, "File $dest should now exist";
file_contents_identical $dest, $file2;
is_deeply +MockOutput->get_debug, [[' ', __x(
"Copying {source} -> {dest}",
source => $file2,
dest => $dest
)]], 'Again only Copy message should have been emitted';
# Try to copy a nonexistent file.
my $nonfile = file 'nonexistent.txt';
throws_ok { $bundle->_copy_if_modified($nonfile, $dest) } 'App::Sqitch::X',
'Should get exception when source file does not exist';
is $@->ident, 'bundle', 'Nonexistent file error ident should be "bundle"';
is $@->message, __x(
'Cannot copy {file}: does not exist',
file => $nonfile,
), 'Nonexistent file error message should be correct';
COPYDIE: {
# Make copy die.
$dest->remove;
my $mocker = Test::MockModule->new('File::Copy');
$mocker->mock(copy => sub { return 0 });
throws_ok { $bundle->_copy_if_modified($file, $dest) } 'App::Sqitch::X',
'Should get exception when copy returns false';
is $@->ident, 'bundle', 'Copy fail ident should be "bundle"';
is $@->message, __x(
'Cannot copy "{source}" to "{dest}": {error}',
source => $file,
dest => $dest,
error => $!,
), 'Copy fail error message should be correct';
}
##############################################################################
# Test bundle_config().
END {
my $to_remove = $dir->parent->stringify;
remove_tree $to_remove if -e $to_remove;
}
$dest = file $dir, qw(sqitch.conf);
file_not_exists_ok $dest;
ok $bundle->bundle_config, 'Bundle the config file';
file_exists_ok $dest;
file_contents_identical $dest, file('sqitch.conf');
is_deeply +MockOutput->get_info, [[__ 'Writing config']],
'Should have config notice';
##############################################################################
# Test bundle_plan().
$dest = file $bundle->dest_top_dir($bundle->default_target), qw(sqitch.plan);
file_not_exists_ok $dest;
ok $bundle->bundle_plan($bundle->default_target),
'Bundle the default target plan file';
file_exists_ok $dest;
file_contents_identical $dest, file(qw(engine sqitch.plan));
is_deeply +MockOutput->get_info, [[__ 'Writing plan']],
'Should have plan notice';
# Make sure that --from works.
isa_ok $bundle = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'bundle',
config => $config,
args => ['--from', 'widgets'],
}), $CLASS, '--from bundle command';
is $bundle->from, 'widgets', 'From should be "widgets"';
ok $bundle->bundle_plan($bundle->default_target, 'widgets'),
'Bundle the default target plan file with from arg';
my $plan = $bundle->default_target->plan;
is_deeply +MockOutput->get_info, [[__x(
'Writing plan from {from} to {to}',
from => 'widgets',
to => '@HEAD',
)]], 'Statement of the bits written should have been emitted';
file_contents_is $dest,
'%syntax-version=' . App::Sqitch::Plan::SYNTAX_VERSION . "\n"
. '%project=engine' . "\n"
. "\n"
. $plan->find('widgets')->as_string . "\n"
. $plan->find('func/add_user')->as_string . "\n"
. $plan->find('users@HEAD')->as_string . "\n",
'Plan should contain only changes from "widgets" on';
# Make sure that --to works.
isa_ok $bundle = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'bundle',
config => $config,
args => ['--to', 'users'],
}), $CLASS, '--to bundle command';
is $bundle->to, 'users', 'To should be "users"';
ok $bundle->bundle_plan($bundle->default_target, undef, 'users'),
'Bundle the default target plan file with to arg';
is_deeply +MockOutput->get_info, [[__x(
'Writing plan from {from} to {to}',
from => '@ROOT',
to => 'users',
)]], 'Statement of the bits written should have been emitted';
file_contents_is $dest,
'%syntax-version=' . App::Sqitch::Plan::SYNTAX_VERSION . "\n"
. '%project=engine' . "\n"
. "\n"
. $plan->find('users')->as_string . "\n"
. join( "\n", map { $_->as_string } $plan->find('users')->tags ) . "\n",
'Plan should have written only "users" and its tags';
##############################################################################
# Test bundle_scripts().
my @scripts = (
$dir_for->{reworked_deploy}->file('users@alpha.sql'),
$dir_for->{reworked_revert}->file('users@alpha.sql'),
$dir_for->{deploy}->file('widgets.sql'),
$dir_for->{revert}->file('widgets.sql'),
$dir_for->{deploy}->file(qw(func add_user.sql)),
$dir_for->{revert}->file(qw(func add_user.sql)),
$dir_for->{deploy}->file('users.sql'),
$dir_for->{revert}->file('users.sql'),
);
file_not_exists_ok $_ for @scripts;
$config->update( 'core.extension' => 'sql');
isa_ok $bundle = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'bundle',
config => $config,
}), $CLASS, 'another bundle command';
ok $bundle->bundle_scripts($bundle->default_target),
'Bundle default target scripts';
file_exists_ok $_ for @scripts;
is_deeply +MockOutput->get_info, [
[__ 'Writing scripts'],
[' + ', 'users @alpha'],
[' + ', 'widgets'],
[' + ', 'func/add_user'],
[' + ', 'users'],
], 'Should have change notices';
# Make sure that --from works.
remove_tree $dir->parent->stringify;
isa_ok $bundle = App::Sqitch::Command::bundle->new(
sqitch => $sqitch,
dest_dir => $bundle->dest_dir,
from => 'widgets',
), $CLASS, 'bundle from "widgets"';
ok $bundle->bundle_scripts($bundle->default_target, 'widgets'), 'Bundle scripts';
file_not_exists_ok $_ for @scripts[0,1];
file_exists_ok $_ for @scripts[2,3];
is_deeply +MockOutput->get_info, [
[__ 'Writing scripts'],
[' + ', 'widgets'],
[' + ', 'func/add_user'],
[' + ', 'users'],
], 'Should have changes only from "widets" onward in notices';
# Make sure that --to works.
remove_tree $dir->parent->stringify;
isa_ok $bundle = App::Sqitch::Command::bundle->new(
sqitch => $sqitch,
dest_dir => $bundle->dest_dir,
to => 'users@alpha',
), $CLASS, 'bundle to "users"';
ok $bundle->bundle_scripts($bundle->default_target, undef, 'users@alpha'), 'Bundle scripts';
file_exists_ok $_ for @scripts[0,1];
file_not_exists_ok $_ for @scripts[2,3];
is_deeply +MockOutput->get_info, [
[__ 'Writing scripts'],
[' + ', 'users @alpha'],
], 'Should have only "users" in change notices';
# Should throw exceptions on unknonw changes.
for my $key (qw(from to)) {
my $bundle = $CLASS->new( sqitch => $sqitch, $key => 'nonexistent' );
throws_ok {
$bundle->bundle_scripts($bundle->default_target, 'nonexistent')
} 'App::Sqitch::X', "Should die on nonexistent $key change";
is $@->ident, 'bundle', qq{Nonexistent $key change ident should be "bundle"};
is $@->message, __x(
'Cannot find change {change}',
change => 'nonexistent',
), "Nonexistent $key message change should be correct";
}
##############################################################################
# Test execute().
MockOutput->get_debug;
remove_tree $dir->parent->stringify;
@scripts = (
file($dir, 'sqitch.conf'),
file($bundle->dest_top_dir($bundle->default_target), 'sqitch.plan'),
@scripts,
);
file_not_exists_ok $_ for @scripts;
isa_ok $bundle = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'bundle',
config => $config,
}), $CLASS, 'another bundle command';
ok $bundle->execute, 'Execute!';
file_exists_ok $_ for @scripts;
is_deeply +MockOutput->get_info, [
[__x 'Bundling into {dir}', dir => $bundle->dest_dir ],
[__ 'Writing config'],
[__ 'Writing plan'],
[__ 'Writing scripts'],
[' + ', 'users @alpha'],
[' + ', 'widgets'],
[' + ', 'func/add_user'],
[' + ', 'users'],
], 'Should have all notices';
# Try a configuration with multiple plans.
my $multidir = $dir->parent;
END { remove_tree $multidir->stringify }
remove_tree $multidir->stringify;
my @sql = (
$multidir->file(qw(sql sqitch.plan)),
$multidir->file(qw(sql deploy roles.sql)),
$multidir->file(qw(sql deploy users.sql)),
$multidir->file(qw(sql verify users.sql)),
$multidir->file(qw(sql deploy widgets.sql)),
);
my @engine = (
$multidir->file(qw(engine sqitch.plan)),
$multidir->file(qw(engine reworked deploy users@alpha.sql)),
$multidir->file(qw(engine reworked revert users@alpha.sql)),
$multidir->file(qw(engine deploy widgets.sql)),
$multidir->file(qw(engine revert widgets.sql)),
$multidir->file(qw(engine deploy func add_user.sql)),
$multidir->file(qw(engine revert func add_user.sql)),
$multidir->file(qw(engine deploy users.sql)),
$multidir->file(qw(engine revert users.sql)),
);
my $conf_file = $multidir->file('multiplan.conf'),;
file_not_exists_ok $_ for ($conf_file, @sql, @engine);
$config = TestConfig->from(local => 'multiplan.conf');
$sqitch = App::Sqitch->new(config => $config);
isa_ok $bundle = $CLASS->new(
sqitch => $sqitch,
config => $config,
all => 1,
from => '@ROOT',
dest_dir => dir '_build',
), $CLASS, 'all multiplan bundle command';
ok $bundle->execute, 'Execute multi-target bundle!';
file_exists_ok $_ for ($conf_file, @sql, @engine);
is_deeply +MockOutput->get_warn, [[__(
"Use of --to or --from to bundle multiple targets is not recommended.\nPass them as arguments after each target argument, instead."
)]], 'Should have a warning about --from and -too';
# Make sure we get an error with both --all and a specified target.
throws_ok { $bundle->execute('pg' ) } 'App::Sqitch::X',
'Should get an error for --all and a target arg';
is $@->ident, 'bundle', 'Mixed arguments error ident should be "bundle"';
is $@->message, __(
'Cannot specify both --all and engine, target, or plan arugments'
), 'Mixed arguments error message should be correct';
# Try without --all.
isa_ok $bundle = $CLASS->new(
sqitch => $sqitch,
config => $sqitch->config,
dest_dir => dir '_build',
), $CLASS, 'multiplan bundle command';
remove_tree $multidir->stringify;
ok $bundle->execute, qq{Execute with no arg};
file_exists_ok $_ for ($conf_file, @engine);
file_not_exists_ok $_ for @sql;
# Make sure it works with bundle.all set, as well.
$config->update('bundle.all' => 1);
remove_tree $multidir->stringify;
ok $bundle->execute, qq{Execute with bundle.all config};
file_exists_ok $_ for ($conf_file, @engine, @sql);
# Try limiting it in various ways.
for my $spec (
[
target => 'pg',
{ include => \@engine, exclude => \@sql },
],
[
'plan file' => file(qw(engine sqitch.plan))->stringify,
{ include => \@engine, exclude => \@sql },
],
[
target => 'mysql',
{ include => \@sql, exclude => \@engine },
],
[
'plan file' => file(qw(sql sqitch.plan))->stringify,
{ include => \@sql, exclude => \@engine },
],
) {
my ($type, $arg, $files) = @{ $spec };
remove_tree $multidir->stringify;
ok $bundle->execute($arg), qq{Execute with $type arg "$arg"};
file_exists_ok $_ for ($conf_file, @{ $files->{include} });
file_not_exists_ok $_ for @{ $files->{exclude} };
}
# Make sure we handle --to and --from.
isa_ok $bundle = $CLASS->new(
sqitch => $sqitch,
config => $sqitch->config,
from => 'widgets',
to => 'widgets',
dest_dir => dir '_build',
), $CLASS, 'to/from bundle command';
remove_tree $multidir->stringify;
ok $bundle->execute('pg'), 'Execute to/from bundle!';
file_exists_ok $_ for ($conf_file, @engine[0,3,4]);
file_not_exists_ok $_ for (@engine[1,2,5..$#engine]);
file_contents_is $engine[0],
'%syntax-version=' . App::Sqitch::Plan::SYNTAX_VERSION . "\n"
. '%project=engine' . "\n"
. "\n"
. $plan->find('widgets')->as_string . "\n",
'Plan should have written only "widgets"';
# Make sure we handle to and from args.
isa_ok $bundle = $CLASS->new(
sqitch => $sqitch,
config => $sqitch->config,
dest_dir => dir '_build',
), $CLASS, 'another bundle command';
remove_tree $multidir->stringify;
ok $bundle->execute(qw(pg widgets @HEAD)), 'Execute bundle with to/from args!';
file_exists_ok $_ for ($conf_file, @engine[0,3..$#engine]);
file_not_exists_ok $_ for (@engine[1,2]);
file_contents_is $engine[0],
'%syntax-version=' . App::Sqitch::Plan::SYNTAX_VERSION . "\n"
. '%project=engine' . "\n"
. "\n"
. $plan->find('widgets')->as_string . "\n"
. $plan->find('func/add_user')->as_string . "\n"
. $plan->find('users@HEAD')->as_string . "\n",
'Plan should have written "widgets" and "func/add_user"';
# Should die on unknown argument.
throws_ok { $bundle->execute('nonesuch') } 'App::Sqitch::X',
'Should get an exception for unknown argument';
is $@->ident, 'bundle', 'Unknown argument error ident shoud be "bundle"';
is $@->message, __nx(
'Unknown argument "{arg}"',
'Unknown arguments: {arg}',
1,
arg => 'nonesuch',
), 'Unknown argument error message should be correct';
# Should handle multiple arguments, too.
throws_ok { $bundle->execute(qw(ba da dum)) } 'App::Sqitch::X',
'Should get an exception for unknown arguments';
is $@->ident, 'bundle', 'Unknown arguments error ident shoud be "bundle"';
is $@->message, __nx(
'Unknown argument "{arg}"',
'Unknown arguments: {arg}',
3,
arg => join ', ', qw(ba da dum)
), 'Unknown arguments error message should be correct';
# Should die on both changes and --from or -to.
isa_ok $bundle = $CLASS->new(
sqitch => $sqitch,
config => $sqitch->config,
from => '@ROOT',
), $CLASS, 'all multiplan bundle command';
throws_ok { $bundle->execute(qw(widgets)) } 'App::Sqitch::X',
'Should get an exception a change name and --from';
is $@->ident, 'bundle', 'Conflicting arguments error ident shoud be "bundle"';
is $@->message, __('Cannot specify both --from or --to and change arguments'),
'Conflicting arguments error message should be correct';
|