]>
Dogcows Code - chaz/git-codeowners/blob - t/app-codeowners.t
7 use Test
::Exit
; # must be first
8 use App
::Codeowners
::Util
qw(run_git);
10 use Capture
::Tiny
qw(capture);
12 use Path
::Tiny
qw(path tempdir);
15 my $can_git = _can_git
();
17 # Set progname so that pod2usage knows how to find the script after we chdir.
18 $0 = path
($Bin)->parent->child('bin/git-codeowners')->absolute->stringify;
22 sub run
(&) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
24 capture
{ exit_code
{ $code->() } };
27 subtest
'basic options' => sub {
28 my ($stdout, $stderr, $exit) = run
{ App
::Codeowners-
>main('--help') };
29 is($exit, 0, 'exited 0 when --help');
30 like
($stdout, qr/Usage:/, 'correct --help output') or diag
$stdout;
32 ($stdout, $stderr, $exit) = run
{ App
::Codeowners-
>main('--version') };
33 is($exit, 0, 'exited 0 when --version');
34 like
($stdout, qr/git-codeowners [\d.]+\n/, 'correct --version output') or diag
$stdout;
37 subtest
'bad options' => sub {
38 my ($stdout, $stderr, $exit) = run
{ App
::Codeowners-
>main(qw{show --not-an-option}) };
39 is($exit, 2, 'exited with error on bad option');
40 like
($stderr, qr/Unknown option: not-an-option/, 'correct error message') or diag
$stderr;
43 subtest
'show' => sub {
44 plan skip_all
=> 'Cannot run git' if !$can_git;
46 my $repodir = _setup_git_repo
();
47 my $chdir = pushd
($repodir);
49 my ($stdout, $stderr, $exit) = run
{ App
::Codeowners-
>main(qw{-f %F;%O show}) };
50 is($exit, 0, 'exited without error');
51 is($stdout, <<'END', 'correct output');
53 a/b/c/bar.txt;@snickers
57 ($stdout, $stderr, $exit) = run
{ App
::Codeowners-
>main(qw{-f %F;%O;%P show}) };
58 is($exit, 0, 'exited without error');
59 is($stdout, <<'END', 'correct output');
61 a/b/c/bar.txt;@snickers;peanuts
65 subtest
'format json' => sub {
66 plan skip_all
=> 'No JSON::MaybeXS' if !eval { require JSON
::MaybeXS
};
68 ($stdout, $stderr, $exit) = run
{ App
::Codeowners-
>main(qw{-f json show --no-projects}) };
69 is($exit, 0, 'exited without error');
70 my $expect = '[{"File":"CODEOWNERS","Owner":null},{"File":"a/b/c/bar.txt","Owner":["@snickers"]},{"File":"foo.txt","Owner":["@twix"]}]';
71 is($stdout, $expect, 'correct output with json format');
75 subtest
'create' => sub {
76 plan skip_all
=> 'Cannot run git' if !$can_git;
78 my $repodir = _setup_git_repo
();
79 my $chdir = pushd
($repodir);
81 my $codeowners_filepath = path
('CODEOWNERS');
82 $codeowners_filepath->remove;
84 my ($stdout, $stderr, $exit) = run
{ App
::Codeowners-
>main(qw{create}) };
85 is($exit, 0, 'exited without error');
86 is($stderr, "Wrote CODEOWNERS\n", 'reportedly wrote a CODEOWNERS file');
88 ok
($codeowners_filepath->is_file, 'did write CODEOWNERS file');
90 my $contents = $codeowners_filepath->slurp_utf8;
91 like
($contents, qr/^# This file shows mappings/, 'correct contents of file') or diag
$contents;
98 my (undef, $version) = eval { run_git
('--version') };
100 note
"Found: $version" if $version;
101 return $version && $version ge 'git version 1.8.5'; # for -C flag
104 sub _setup_git_repo
{
105 my $repodir = tempdir
;
107 $repodir->child('foo.txt')->touchpath;
108 $repodir->child('a/b/c/bar.txt')->touchpath;
109 $repodir->child('CODEOWNERS')->spew_utf8([<<'END']);
116 run_git
('-C', $repodir, qw{init})->wait;
117 run_git
('-C', $repodir, qw{add .})->wait;
118 run_git
('-C', $repodir, qw{commit -m}, 'initial commit')->wait;
This page took 0.042142 seconds and 4 git commands to generate.