]> Dogcows Code - chaz/git-codeowners/blob - t/app-codeowners-util.t
refactor formatters
[chaz/git-codeowners] / t / app-codeowners-util.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use App::Codeowners::Util qw(run_git);
7 use Path::Tiny qw(path tempdir);
8 use Test::More;
9
10 can_ok('App::Codeowners::Util', qw{
11 colorstrip
12 find_codeowners_in_directory
13 find_nearest_codeowners
14 git_ls_files
15 git_toplevel
16 run_command
17 run_git
18 stringf
19 stringify
20 unbackslash
21 zip
22 });
23
24 my $can_git = _can_git();
25
26 subtest 'git_ls_files' => sub {
27 plan skip_all => 'Cannot run git' if !$can_git;
28 my $repodir =_setup_git_repo();
29
30 my (undef, @r) = App::Codeowners::Util::git_ls_files($repodir);
31 is_deeply(\@r, [], 'git ls-files returns [] when no repo files') or diag explain \@r;
32
33 run_git('-C', $repodir, qw{add .})->wait;
34 run_git('-C', $repodir, qw{commit -m}, 'initial commit')->wait;
35
36 (undef, @r) = App::Codeowners::Util::git_ls_files($repodir);
37 is_deeply(\@r, [
38 qw(a/b/c/bar.txt foo.txt)
39 ], 'git ls-files returns correct repo files') or diag explain \@r;
40 };
41
42 subtest 'git_toplevel' => sub {
43 plan skip_all => 'Cannot run git' if !$can_git;
44 my $repodir =_setup_git_repo();
45
46 my $r = App::Codeowners::Util::git_toplevel($repodir);
47 is($r, $repodir, 'found toplevel directory from toplevel');
48
49 $r = App::Codeowners::Util::git_toplevel($repodir->child('a/b'));
50 is($r, $repodir, 'found toplevel directory');
51 };
52
53 subtest 'find_nearest_codeowners' => sub {
54 my $repodir =_setup_git_repo();
55 $repodir->child('docs')->mkpath;
56 my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS'));
57
58 my $r = App::Codeowners::Util::find_nearest_codeowners($repodir->child('a/b/c'));
59 is($r, $filepath, 'found CODEOWNERS file');
60 };
61
62 subtest 'find_codeowners_in_directory' => sub {
63 my $repodir =_setup_git_repo();
64 $repodir->child('docs')->mkpath;
65
66 my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS'));
67
68 my $r = App::Codeowners::Util::find_codeowners_in_directory($repodir);
69 is($r, $filepath, 'found CODEOWNERS file in docs');
70
71 $filepath = _spew_codeowners($repodir->child('CODEOWNERS'));
72 $r = App::Codeowners::Util::find_codeowners_in_directory($repodir);
73 is($r, $filepath, 'found CODEOWNERS file in toplevel');
74 };
75
76 done_testing;
77 exit;
78
79 sub _can_git {
80 my (undef, $version) = run_git('--version');
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 run_git('-C', $repodir, 'init')->wait;
89
90 $repodir->child('foo.txt')->touchpath;
91 $repodir->child('a/b/c/bar.txt')->touchpath;
92
93 return $repodir;
94 }
95
96 sub _spew_codeowners {
97 my $path = path(shift);
98 $path->spew_utf8(\"foo.txt \@twix\n");
99 return $path;
100 }
This page took 0.041063 seconds and 4 git commands to generate.