]> Dogcows Code - chaz/git-codeowners/blob - t/app-codeowners.t
64415394b69acb1e0b01d6ac004d90d789123fb4
[chaz/git-codeowners] / t / app-codeowners.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use FindBin '$Bin';
7 use Test::Exit; # must be first
8 use App::Codeowners::Util qw(run_git);
9 use App::Codeowners;
10 use Capture::Tiny qw(capture);
11 use File::pushd;
12 use Path::Tiny qw(path tempdir);
13 use Test::More;
14
15 my $can_git = _can_git();
16
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;
19
20 $ENV{NO_COLOR} = 1;
21
22 sub run(&) { ## no critic (Subroutines::ProhibitSubroutinePrototypes)
23 my $code = shift;
24 capture { exit_code { $code->() } };
25 }
26
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;
31
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;
35 };
36
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;
41 };
42
43 subtest 'show' => sub {
44 plan skip_all => 'Cannot run git' if !$can_git;
45
46 my $repodir = _setup_git_repo();
47 my $chdir = pushd($repodir);
48
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');
52 CODEOWNERS;
53 a/b/c/bar.txt;@snickers
54 foo.txt;@twix
55 END
56
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');
60 CODEOWNERS;;
61 a/b/c/bar.txt;@snickers;peanuts
62 foo.txt;@twix;
63 END
64
65 subtest 'format json' => sub {
66 plan skip_all => 'No JSON::MaybeXS' if !eval { require JSON::MaybeXS };
67
68 ($stdout, $stderr, $exit) = run { App::Codeowners->main(qw{-f json show --no-project}) };
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');
72 };
73 };
74
75 done_testing;
76 exit;
77
78 sub _can_git {
79 my (undef, $version) = eval { run_git('--version') };
80 note $@ if $@;
81 note "Found: $version" if $version;
82 return $version && $version ge 'git version 1.8.5'; # for -C flag
83 }
84
85 sub _setup_git_repo {
86 my $repodir = tempdir;
87
88 $repodir->child('foo.txt')->touchpath;
89 $repodir->child('a/b/c/bar.txt')->touchpath;
90 $repodir->child('CODEOWNERS')->spew_utf8([<<'END']);
91 # whatever
92 /foo.txt @twix
93 # Project: peanuts
94 a/ @snickers
95 END
96
97 run_git('-C', $repodir, qw{init})->wait;
98 run_git('-C', $repodir, qw{add .})->wait;
99 run_git('-C', $repodir, qw{commit -m}, 'initial commit')->wait;
100
101 return $repodir;
102 }
This page took 0.038714 seconds and 3 git commands to generate.