Skip to content

Commit

Permalink
Have init die on invalid project file.
Browse files Browse the repository at this point in the history
If the project file exists already, and is either not a file, not a Sqitch
plan file, or defined for a different project, then throw an error so the user
has some idea what to do next. Closes #214.
  • Loading branch information
theory committed Mar 24, 2015
1 parent ca2aee4 commit 3de259e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -38,6 +38,9 @@ Revision history for Perl extension App::Sqitch
- Fixed the formatting of change notes so that a space preceeds the `#`
character whether the note was added by the `--note` option or via an
editor.
- The `init` command now throws an error when the plan file already
exists and is invalid or defined for a different project. Thanks to
Gabriel Potkány for the suggestion (Issue #214).

0.999 2015-02-12T19:43:45Z
- Improved MySQL missing table error detection by relying on error codes
Expand Down
28 changes: 27 additions & 1 deletion lib/App/Sqitch/Command/init.pm
Expand Up @@ -93,7 +93,33 @@ sub write_plan {
my ( $self, $project ) = @_;
my $target = $self->default_target;
my $file = $target->plan_file;
return $self if -f $file;

if (-e $file) {
hurl init => __x(
'Cannot initialize because {file} already exists and is not a file',
file => $file,
) unless -f $file;

# Try to load the plan file.
my $plan = App::Sqitch::Plan->new(
sqitch => $self->sqitch,
file => $file,
target => $self->default_target,
);
my $file_proj = try { $plan->project } or hurl init => __x(
'Cannot initialize because {file} already exists and is not a valid plan file',
file => $file,
);

# Bail if this plan file looks like it's for a different project.
hurl init => __x(
'Cannot initialize because project "{project}" already initialized in {file}',
project => $plan->project,
file => $file,
) if $plan->project ne $project;
return $self;
}

$self->_mkdir( $file->dir ) unless -d $file->dir;

my $fh = $file->open('>:utf8_strict') or hurl init => __x(
Expand Down
30 changes: 27 additions & 3 deletions t/init.t
Expand Up @@ -4,7 +4,7 @@ use strict;
use warnings;
use 5.010;
use utf8;
use Test::More tests => 159;
use Test::More tests => 167;
#use Test::More 'no_plan';
use App::Sqitch;
use Locale::TextDomain qw(App-Sqitch);
Expand Down Expand Up @@ -434,13 +434,37 @@ file_contents_is $plan_file,
'%project=nada' . "\n\n",
'The contents should be correct';

# Write more to the plan.
# Make sure we don't overwrite the file when initializing again.
ok $init->write_plan( 'nada' ), 'Write the plan file again';
file_exists_ok $plan_file, 'Plan file should still exist';
file_contents_is $plan_file,
'%syntax-version=' . App::Sqitch::Plan::SYNTAX_VERSION() . "\n" .
'%project=nada' . "\n\n",
'The contents should be identical';

# Make sure we get an error trying to initalize a different plan.
throws_ok { $init->write_plan( 'oopsie' ) } 'App::Sqitch::X',
'Should get an error initialing a different project';
is $@->ident, 'init', 'Initialization error ident should be "init"';
is $@->message, __x(
'Cannot initialize because project "{project}" already initialized in {file}',
project => 'nada',
file => $plan_file,
), 'Initialzation error message should be correct';

# Write a different file.
my $fh = $plan_file->open('>:utf8_strict') or die "Cannot open $plan_file: $!\n";
$fh->say('# testing 1, 2, 3');
$fh->close;

# Try writing again.
ok $init->write_plan( 'foofoo' ), 'Write the plan file again';
throws_ok { $init->write_plan( 'foofoo' ) } 'App::Sqitch::X',
'Should get an error initialzing a non-plan file';
is $@->ident, 'init', 'Non-plan file error ident should be "init"';
is $@->message, __x(
'Cannot initialize because {file} already exists and is not a valid plan file',
file => $plan_file,
), 'Non-plan file error message should be correct';
file_contents_like $plan_file, qr/testing 1, 2, 3/,
'The file should not be overwritten';

Expand Down

0 comments on commit 3de259e

Please sign in to comment.