]> Dogcows Code - chaz/git-codeowners/blob - t/app-codeowners.t
initial commit
[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 plan skip_all => 'Cannot run git' if !$can_git;
17
18 # Set progname so that pod2usage knows how to find the script after we chdir
19 $0 = path($Bin)->parent->child('bin/git-codeowners')->absolute;
20
21 $ENV{NO_COLOR} = 1;
22
23 subtest 'basic options' => sub {
24 my $repodir = _setup_git_repo();
25 my $chdir = pushd($repodir);
26
27 my ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main('--help') } };
28 is($exit, 0, 'exited 0 when --help');
29 like($stdout, qr/Usage:/, 'correct --help output') or diag $stdout;
30
31 ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main('--version') } };
32 is($exit, 0, 'exited 0 when --version');
33 like($stdout, qr/git-codeowners [\d.]+\n/, 'correct --version output') or diag $stdout;
34 };
35
36 subtest 'bad options' => sub {
37 my $repodir = _setup_git_repo();
38 my $chdir = pushd($repodir);
39
40 my ($stdout, $stderr, $exit) = capture { exit_code { App::Codeowners->main(qw{show --not-an-option}) } };
41 is($exit, 2, 'exited with error on bad option');
42 like($stderr, qr/Unknown option: not-an-option/, 'correct error message') or diag $stderr;
43 };
44
45 subtest 'show' => sub {
46 my $repodir = _setup_git_repo();
47 my $chdir = pushd($repodir);
48
49 my ($stdout, $stderr, $exit) = capture { exit_code { 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) = capture { exit_code { 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) = capture { exit_code { 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 ($version) = run_git('--version');
80 return $version;
81 }
82
83 sub _setup_git_repo {
84 my $repodir = tempdir;
85
86 $repodir->child('foo.txt')->touchpath;
87 $repodir->child('a/b/c/bar.txt')->touchpath;
88 $repodir->child('CODEOWNERS')->spew_utf8([<<'END']);
89 # whatever
90 /foo.txt @twix
91 # Project: peanuts
92 a/ @snickers
93 END
94
95 run_git('-C', $repodir, qw{init});
96 run_git('-C', $repodir, qw{add .});
97 run_git('-C', $repodir, qw{commit -m}, 'initial commit');
98
99 return $repodir;
100 }
This page took 0.038875 seconds and 4 git commands to generate.