]> Dogcows Code - chaz/git-codeowners/blob - t/app-codeowners-util.t
compare long paths on win32
[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 my $repodir_fixed = _fix_win32_path($repodir);
46
47 my $r = App::Codeowners::Util::git_toplevel($repodir);
48 is($r->canonpath, $repodir_fixed->canonpath, 'found toplevel directory from toplevel');
49
50 $r = App::Codeowners::Util::git_toplevel($repodir->child('a/b'));
51 is($r->canonpath, $repodir_fixed->canonpath, 'found toplevel directory');
52 };
53
54 subtest 'find_nearest_codeowners' => sub {
55 plan skip_all => 'Cannot run git' if !$can_git;
56 my $repodir =_setup_git_repo();
57
58 $repodir->child('docs')->mkpath;
59 my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS'));
60
61 my $r = App::Codeowners::Util::find_nearest_codeowners($repodir->child('a/b/c'));
62 is($r, $filepath, 'found CODEOWNERS file');
63 };
64
65 subtest 'find_codeowners_in_directory' => sub {
66 plan skip_all => 'Cannot run git' if !$can_git;
67 my $repodir =_setup_git_repo();
68
69 $repodir->child('docs')->mkpath;
70 my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS'));
71
72 my $r = App::Codeowners::Util::find_codeowners_in_directory($repodir);
73 is($r, $filepath, 'found CODEOWNERS file in docs');
74
75 $filepath = _spew_codeowners($repodir->child('CODEOWNERS'));
76 $r = App::Codeowners::Util::find_codeowners_in_directory($repodir);
77 is($r, $filepath, 'found CODEOWNERS file in toplevel');
78 };
79
80 done_testing;
81 exit;
82
83 sub _can_git {
84 my (undef, $version) = eval { run_git('--version') };
85 note $@ if $@;
86 note "Found: $version" if $version;
87 return $version && $version ge 'git version 1.8.5'; # for -C flag
88 }
89
90 sub _setup_git_repo {
91 my $repodir = tempdir;
92
93 run_git('-C', $repodir, 'init')->wait;
94 run_git('-C', $repodir, qw{config --local user.email app-codeowners@example.com})->wait;
95 run_git('-C', $repodir, qw{config --local user.name App-Codeowners})->wait;
96
97 $repodir->child('foo.txt')->touchpath;
98 $repodir->child('a/b/c/bar.txt')->touchpath;
99
100 return $repodir;
101 }
102
103 sub _fix_win32_path {
104 my $path = shift;
105 # Git for Windows shows full paths
106 if (eval { require Win32 }) {
107 $path = path(Win32::GetLongPathName($path));
108 }
109 return $path;
110 }
111
112 sub _spew_codeowners {
113 my $path = path(shift);
114 $path->spew_utf8(\"foo.txt \@twix\n");
115 return $path;
116 }
This page took 0.049591 seconds and 4 git commands to generate.