Skip to content

Commit

Permalink
Add --directory/-C option.
Browse files Browse the repository at this point in the history
Closes #411.
  • Loading branch information
theory committed Dec 21, 2018
1 parent 102f625 commit 55b95db
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Revision history for Perl extension App::Sqitch
tag-qualified to eliminate ambiguity. Now reworked dependencies may be
required without tag-qualification, though tag-qualification should still
be specified if functionality as of a particular tag is required.
- Added the `--directory`/`-C` option to specify a directory to change to
before executing any Sqitch commands. Thansk to Thomas Sibley for the
suggestion (#411).

0.9998 2018-10-03T20:53:58Z
- Fixed an issue where Sqitch would sometimes truncate the registry
Expand Down
10 changes: 10 additions & 0 deletions lib/App/Sqitch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ sub _core_opts {
plan-file|f=s
engine=s
registry=s
directory|C=s
client|db-client=s
db-name|d=s
db-username|db-user|u=s
Expand Down Expand Up @@ -315,6 +316,15 @@ sub _parse_core_opts {
exit;
}

# Handle --directory
if ( my $dir = delete $opts{directory} ) {
chdir $dir or hurl fs => __x(
'Cannot change to directory {directory}: {error}',
directory => $dir,
error => $!,
);
}

# Convert files and dirs to objects.
for my $dir (qw(
top_dir
Expand Down
15 changes: 15 additions & 0 deletions lib/sqitch.pod
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ User who adds a change to the plan.
-u --db-user USER Database user name.
-h --db-host HOST Database server host name.
-p --db-port PORT Database server port number.
-C --directory DIR Change to directory before performing any actions.
--top-dir DIR Path to directory with plan and scripts.
--etc-path Print path to etc directory and exit.
--quiet Quiet mode with non-error output suppressed.
Expand Down Expand Up @@ -325,6 +326,20 @@ Port number to connect to. Does not apply to all engines.In general,
L<targets|sqitch-target> and URIs are preferred, but this option can be used
to override the port in a target.

=item C<-C>

=item C<--directory>

sqitch --directory dbproject
sqitch -C dbcheckout

Change to the specified directory before performing any actions. Effectively the
same as:

(cd somedir && sqitch ...)

But a bit friendlier when managing multiple projects.

=item C<--top-dir>

sqitch --top-dir migrations/
Expand Down
6 changes: 3 additions & 3 deletions lib/sqitchcommands.pod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ sqitchcommands - List of common sqitch commands

=head1 Usage

sqitch [--plan-file <file>] [--engine <engine>] [--top-dir <dir> ]
[--extension <ext>] [--registry <registry>] [--etc-path]
[--quiet] [--verbose] [--version]
sqitch [--plan-file <file>] [--directory <path>] [--engine <engine>]
[--top-dir <dir> ] [--extension <ext>] [--registry <registry>]
[--etc-path] [--quiet] [--verbose] [--version] [--directory <path>]
<command> [<command-options>] [<args>]

=head1 Common Commands
Expand Down
40 changes: 39 additions & 1 deletion t/options.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@
use strict;
use warnings;
use utf8;
use Test::More tests => 26;
use Test::More tests => 35;
#use Test::More 'no_plan';
use Test::MockModule;
use Test::Exception;
use Capture::Tiny 0.12 ':all';
use Locale::TextDomain qw(App-Sqitch);

$ENV{SQITCH_CONFIG} = 'nonexistent.conf';
$ENV{SQITCH_USER_CONFIG} = 'nonexistent.user';
$ENV{SQITCH_SYSTEM_CONFIG} = 'nonexistent.sys';

my ($catch_chdir, $chdir_to, $chdir_fail);
BEGIN {
$catch_chdir = 0;
# Stub out chdir.
*CORE::GLOBAL::chdir = sub {
return CORE::chdir(@_) unless $catch_chdir;
$chdir_to = shift;
return !$chdir_fail;
};
}

my $CLASS;

BEGIN {
Expand Down Expand Up @@ -175,3 +188,28 @@ USAGE: {
'foo' => 'bar',
}, 'Proper args should have been passed to Pod::Usage';
}

# Test --directory.
$catch_chdir = 1;
ok $opts = $CLASS->_parse_core_opts(['--directory', 'foo/bar']),
'Parse --directory';
is $chdir_to, 'foo/bar', 'Should have changed to foo/bar';
is_deeply $opts, {}, 'Should have preserved no opts';

ok $opts = $CLASS->_parse_core_opts(['-C', 'hi crampus']), 'Parse -C';
is $chdir_to, 'hi crampus', 'Should have changed to hi cramus';
is_deeply $opts, {}, 'Should have preserved no opts';

# Make sure it fails properly.
CHDIE: {
local $! = 9;
$chdir_fail = 1;
throws_ok { $CLASS->_parse_core_opts(['-C', 'nonesuch']) }
'App::Sqitch::X', 'Should get error when chdir fails';
is $@->ident, 'fs', 'Error ident should be "fs"';
is $@->message, __x(
'Cannot change to directory {directory}: {error}',
directory => 'nonesuch',
error => 'Bad file descriptor',
), 'Error message should be correct';
}

0 comments on commit 55b95db

Please sign in to comment.