]> Dogcows Code - chaz/git-codeowners/blob - t/app-codeowners.t
require Path::Tiny 0.089 (for spewing arrayrefs)
[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 0.089 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->stringify;
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-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');
72 };
73 };
74
75 subtest 'create' => sub {
76 plan skip_all => 'Cannot run git' if !$can_git;
77
78 my $repodir = _setup_git_repo();
79 my $chdir = pushd($repodir);
80
81 my $codeowners_filepath = path('CODEOWNERS');
82 $codeowners_filepath->remove;
83
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');
87
88 ok($codeowners_filepath->is_file, 'did write CODEOWNERS file');
89
90 my $contents = $codeowners_filepath->slurp_utf8;
91 like($contents, qr/^# This file shows mappings/, 'correct contents of file') or diag $contents;
92 };
93
94 done_testing;
95 exit;
96
97 sub _can_git {
98 my (undef, $version) = eval { run_git('--version') };
99 note $@ if $@;
100 note "Found: $version" if $version;
101 return $version && $version ge 'git version 1.8.5'; # for -C flag
102 }
103
104 sub _setup_git_repo {
105 my $repodir = tempdir;
106
107 $repodir->child('foo.txt')->touchpath;
108 $repodir->child('a/b/c/bar.txt')->touchpath;
109 $repodir->child('CODEOWNERS')->spew_utf8([<<'END']);
110 # whatever
111 /foo.txt @twix
112 # Project: peanuts
113 a/ @snickers
114 END
115
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;
119
120 return $repodir;
121 }
This page took 0.040163 seconds and 4 git commands to generate.