Skip to content

Commit

Permalink
Add tests to checkout.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronan Dunklau committed Mar 1, 2013
1 parent e257f38 commit e1534ad
Show file tree
Hide file tree
Showing 36 changed files with 600 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/App/Sqitch/Command/checkout.pm
Expand Up @@ -11,10 +11,11 @@ use App::Sqitch::X qw(hurl);
use App::Sqitch::Plan;
use Git::Wrapper;
use FileHandle;
use File::Basename;

extends 'App::Sqitch::Command';

our $VERSION = '0.953';
our $VERSION = '0.954';


has deploy_variables => (
Expand Down Expand Up @@ -62,6 +63,14 @@ has mode => (
default => 'all',
);

has git => (
is => 'ro',
required => 1,
lazy => 1,
default => sub {
Git::Wrapper->new(shift->sqitch->top_dir);
},
);

sub options {
return qw(
Expand Down Expand Up @@ -125,13 +134,13 @@ sub execute {
my $plan = $sqitch->plan;
my $engine = $sqitch->engine;
$engine->with_verify( $self->verify );
my $git = Git::Wrapper->new('.');
my $git = $self->git;
my @current_branch = $git->rev_parse("--abbrev-ref", "HEAD");
hurl plan => __x(
hurl checkout => __x(
'Already on branch {branch}',
branch=>$branch) if $current_branch[0] eq $branch;
my $other_content = join("\n", $git->show($branch . ':' .
$sqitch->plan_file));
basename($sqitch->plan_file)));
my $fh;
open($fh, '<', \$other_content) or die;
my $old_plan = App::Sqitch::Plan->new(
Expand Down
120 changes: 120 additions & 0 deletions t/checkout.t
@@ -0,0 +1,120 @@
#!/usr/bin/perl -w

use strict;
use warnings;
use v5.10;
use Test::More;
use App::Sqitch;
use Path::Class qw(dir file);
use Locale::TextDomain qw(App-Sqitch);
use Test::MockModule;
use Test::Exception;
use Test::File qw(file_not_exists_ok file_exists_ok);

use File::Temp;
use File::Copy::Recursive qw(dircopy);
use lib 't/lib';
use Git::Wrapper;
use MockOutput;

my $CLASS = 'App::Sqitch::Command::checkout';
require_ok $CLASS or die;

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

isa_ok $CLASS, 'App::Sqitch::Command';
can_ok $CLASS, qw(
options
configure
log_only
execute
deploy_variables
revert_variables
);


my $tmp_git_dir = File::Temp->newdir();

ok my $sqitch = App::Sqitch->new(
top_dir => Path::Class::dir($tmp_git_dir),
_engine => 'sqlite',
), 'Load a sqitch object';

my $config = $sqitch->config;

# Test configure().
is_deeply $CLASS->configure($config, {}), {
verify => 0,
mode => 'all',
log_only => 0,
}, 'Check default configuration';

isa_ok my $checkout = App::Sqitch::Command->load({
sqitch => $sqitch,
command => 'checkout',
config => $config,
}), $CLASS, 'checkout command';


# Copy the git repo to a temp directory
my $git = $checkout->git;

$git->clone(Path::Class::Dir->new('t', 'git', 'checkout'), $tmp_git_dir);


file_exists_ok file ($tmp_git_dir, 'sqitch.plan'),
'The plan file exists';

file_exists_ok file ($tmp_git_dir, '.git'),
'The repository exists';


my $plan = $sqitch->plan;
my $changes = $plan->changes;
is $changes, 2, "The plan file from the git repository is ok";
is $git->dir, $tmp_git_dir, 'Git is in the tmp dir';

# Make sure the local branches are actually created.
$git->checkout('another_branch');
$git->pull();
$git->checkout('yet_another_branch');
$git->pull();
$git->checkout('master');
$git->pull();

# Mock the engine
my $mock_engine = Test::MockModule->new('App::Sqitch::Engine::sqlite');
my @dep_args;
$mock_engine->mock(deploy => sub { shift; @dep_args = @_ });
my @rev_args;
$mock_engine->mock(revert => sub { shift; @rev_args = @_ });
my @vars;
$mock_engine->mock(set_variables => sub { shift; push @vars => [@_] });

# Deploy the thing.
$sqitch->engine->deploy;

$checkout->execute('another_branch');

is_deeply +MockOutput->get_info, [
[__x 'Last change before the branches diverged: {last_change}',
last_change => 'users @alpha'],
], 'Should not revert anything, and deploy to another_branch';


throws_ok {$checkout->execute('another_branch')} 'App::Sqitch::X',
'Should throw an error when switching to the same branch';

is $@->ident, 'checkout', 'The error when switching to the same branch should be ident';
is $@->message, __x('Already on branch {branch}',
branch=> 'another_branch'), 'The error message should match';

$checkout->execute('yet_another_branch');
is_deeply +MockOutput->get_info, [
[__x 'Last change before the branches diverged: {last_change}',
last_change => 'users @alpha'],
], 'Should not revert anything, and deploy to another_branch';

done_testing;
1 change: 1 addition & 0 deletions t/git/checkout/HEAD
@@ -0,0 +1 @@
ref: refs/heads/master
6 changes: 6 additions & 0 deletions t/git/checkout/config
@@ -0,0 +1,6 @@
[core]
repositoryformatversion = 0
filemode = true
bare = true
[remote "origin"]
url = /home/ro/projets/sqitch/t/git/checkout_repo
1 change: 1 addition & 0 deletions t/git/checkout/description
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
15 changes: 15 additions & 0 deletions t/git/checkout/hooks/applypatch-msg.sample
@@ -0,0 +1,15 @@
#!/bin/sh
#
# An example hook script to check the commit log message taken by
# applypatch from an e-mail message.
#
# The hook should exit with non-zero status after issuing an
# appropriate message if it wants to stop the commit. The hook is
# allowed to edit the commit message file.
#
# To enable this hook, rename this file to "applypatch-msg".

. git-sh-setup
test -x "$GIT_DIR/hooks/commit-msg" &&
exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
:
24 changes: 24 additions & 0 deletions t/git/checkout/hooks/commit-msg.sample
@@ -0,0 +1,24 @@
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
echo >&2 Duplicate Signed-off-by lines.
exit 1
}
8 changes: 8 additions & 0 deletions t/git/checkout/hooks/post-update.sample
@@ -0,0 +1,8 @@
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".

exec git update-server-info
14 changes: 14 additions & 0 deletions t/git/checkout/hooks/pre-applypatch.sample
@@ -0,0 +1,14 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed
# by applypatch from an e-mail message.
#
# The hook should exit with non-zero status after issuing an
# appropriate message if it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-applypatch".

. git-sh-setup
test -x "$GIT_DIR/hooks/pre-commit" &&
exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"}
:
50 changes: 50 additions & 0 deletions t/git/checkout/hooks/pre-commit.sample
@@ -0,0 +1,50 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# If you want to allow non-ascii filenames set this variable to true.
allownonascii=$(git config hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ascii filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
echo "Error: Attempt to add a non-ascii file name."
echo
echo "This can cause problems if you want to work"
echo "with people on other platforms."
echo
echo "To be portable it is advisable to rename the file ..."
echo
echo "If you know what you are doing you can disable this"
echo "check using:"
echo
echo " git config hooks.allownonascii true"
echo
exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

0 comments on commit e1534ad

Please sign in to comment.