App-Actions

 view release on metacpan or  search on metacpan

t/hooks.t  view on Meta::CPAN

        },
        $source,
    );

    return;
}

sub slurp {
    my ($file) = @_;

    open my $handle, '<', $file or die "cannot read '$file': $!\n";
    my $text = do { local $/; <$handle> };
    close $handle;

    return $text;
}

sub spew {
    my ($file, $text) = @_;

    open my $handle, '>', $file or die "cannot write '$file': $!\n";
    print {$handle} $text or die "cannot write '$file': $!\n";
    close $handle or die "cannot write '$file': $!\n";

    return;
}

my $repository = tempdir('app-actions hook XXXX', TMPDIR => 1, CLEANUP => 1);
copy_tree($root, $repository, $_) for qw(lib bin t .githooks .github);

# The live-knob check reads the documentation as well as the tests, so the
# copy has the README it looks in rather than skipping that half.
copy(
    File::Spec->catfile($root, 'README.md'),
    File::Spec->catfile($repository, 'README.md'),
) or die "cannot copy README.md into the repository copy: $!\n";

# The end-to-end target test generates a Makefile from this copy, so the
# repository carries the same Makefile.PL the checkout does.
copy(
    File::Spec->catfile($root, 'Makefile.PL'),
    File::Spec->catfile($repository, 'Makefile.PL'),
) or die "cannot copy Makefile.PL into the repository copy: $!\n";

# Everything the test runs happens in the repository, and the caller's working
# directory is put back afterwards.
sub in_repository {
    my ($command) = @_;

    my $cwd = File::Spec->rel2abs(File::Spec->curdir);
    chdir $repository or die "cannot change to '$repository': $!\n";

    my ($output, $status) = App::Actions::Process::capture(
        [
            'git',
            '-c', 'user.name=App::Actions test',
            '-c', 'user.email=test@example.com',
            '-c', 'commit.gpgsign=false',
            @$command,
        ],
        merge_stderr => 1,
    );

    chdir $cwd or die "cannot change to '$cwd': $!\n";

    return ($output, $status);
}

sub run_in_repository {
    my (@command) = @_;

    my $cwd = File::Spec->rel2abs(File::Spec->curdir);
    chdir $repository or die "cannot change to '$repository': $!\n";

    my ($output, $exit) = App::Actions::Process::capture(
        \@command, merge_stderr => 1,
    );

    chdir $cwd or die "cannot change to '$cwd': $!\n";

    return ($output, $exit);
}

sub run_hook {
    my (@args) = @_;

    return run_in_repository($^X, $hook, @args);
}

my (undef, $init) = in_repository([ 'init', '-q' ]);
is($init, 0, 'the copy is a git repository');

in_repository([ 'add', '-A' ]);
my (undef, $commit) = in_repository([ 'commit', '-qm', 'initial' ]);
is($commit, 0, 'the copy holds one commit');

my ($clean_output, $clean_status) = run_hook();
is($clean_status, 0, 'the hook passes when no schema can have moved');
is($clean_output, '', 'a commit that passes the hook prints nothing');

# A make target, by contrast, says what it did, even when that was nothing.
my ($idle_output, $idle_status) = run_hook('--staged');
is($idle_status, 0, 'check-staged passes when nothing is staged');
like($idle_output, qr/^check-staged: nothing staged could change what the checks read, so none ran\.$/m,
    'and says that no check ran');
like($idle_output, qr/^  live knobs \(t\/live-knobs\.t\): skipped/m,
    'naming each check it skipped');

# A bump applied to the working tree but not to the index is the one that
# would be committed in half: the staged document still declares the old
# version while the file beside it declares the new one.
my $fixture = File::Spec->catfile($repository, 't', 'fixtures', 'preflight-unreachable.json');
my $shipped = slurp($fixture);

like($shipped, qr/"schema_version"\s*:\s*3\b/,
    'the fixture starts at the version the module ships');

(my $stale = $shipped) =~ s/"schema_version"\s*:\s*\d+/"schema_version": 2/;

spew($fixture, $stale);
in_repository([ 'add', 't/fixtures/preflight-unreachable.json' ]);
spew($fixture, $shipped);

# The working tree agrees with itself now; only the index carries the stale
# document.  If the hook read the working tree it would let this through.
my ($refused, $refused_status) = run_hook();

isnt($refused_status, 0, 'the hook refuses a bump that is staged but not followed through');
like($refused, qr/preflight-unreachable\.json/, 'the refusal names the document');
like($refused, qr/declares schema version 2/, 'the refusal names the version it declares');
like($refused, qr/no-verify/, 'the refusal says how to skip the check for one commit');

# The same snapshot can be checked without attempting a commit, which is what
# make check-staged runs.  It has to reach the same verdict and say which mode
# it is in, so a failure does not claim a commit was refused.
my ($staged_refused, $staged_refused_status) = run_hook('--staged');



( run in 1.459 second using v1.01-cache-2.11-cpan-60884a8df76 )