Make the tests pass again
[catagits/Gitalist.git] / t / model_collectionofrepos.t
1 use FindBin qw/$Bin/;
2 BEGIN {
3     my $env = "$FindBin::Bin/../script/env";
4     if (-r $env) {
5         do $env or die $@;
6     }
7 }
8
9 use strict;
10 use warnings;
11
12 use Test::More;
13 use Test::Exception;
14 use lib "$Bin/lib"; # Used for testing of --model-class etc
15
16 use Moose ();
17 use Moose::Object;
18 use Moose::Autobox;
19 use Class::MOP::Class;
20 use Catalyst::Request;
21 use Catalyst::Response;
22 use Catalyst::Utils;
23 use Gitalist::Model::CollectionOfRepos;
24 use File::Temp qw/tempdir tempfile/;
25
26 my $run_options = {};
27 my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
28 $mock_ctx_meta->add_method('run_options' => sub { $run_options });
29 $mock_ctx_meta->add_attribute($_, accessor => $_, required => 1) for qw/request response/;
30 $mock_ctx_meta->add_attribute('stash', accessor => 'stash', required => 1, default => sub { {} });
31 $mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight from Catalyst.pm
32     my $orig = shift;
33     my $c = shift;
34     my $stash = $orig->($c);
35     if (@_) {
36         my $new_stash = @_ > 1 ? {@_} : $_[0];
37         croak('stash takes a hash or hashref') unless ref $new_stash;
38         foreach my $key ( keys %$new_stash ) {
39           $stash->{$key} = $new_stash->{$key};
40         }
41     }
42     return $stash;
43 });
44 my $mock_log = Moose::Meta::Class->create_anon_class( superclasses => ['Moose::Object'] );
45 $mock_log->add_method($_ => sub {}) for qw/ warn info debug /;
46 my $logger = $mock_log->name->new;
47 $mock_ctx_meta->add_method('log' => sub { $logger });
48
49 our $ctx_gen = sub {
50     my ($cb, %args) = @_;
51     my $ctx = $mock_ctx_meta->new_object(
52         response    => Catalyst::Response->new,
53         request     => Catalyst::Request->new,
54         stash       => {},
55         %args
56     );
57     $ctx->response->_context($ctx);
58     $ctx->request->_context($ctx);
59     $cb->($ctx) if $cb;
60     return $ctx;
61 };
62
63 local %ENV = %ENV;
64 delete $ENV{GITALIST_REPO_DIR};
65
66 throws_ok { my $i = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}); $i->{_application} = $mock_ctx_meta->name; }
67     qr/Don't know where to get repositores from/, 'Blows up nicely with no repos dir';
68
69 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos_dir => '/does/not/exist' }) }
70     qr|No such file or directory|, 'Blows up nicely with repos dir does not exist';
71
72 {
73     my $td = tempdir( CLEANUP => 1 );
74     test_with_config({ repos_dir => $td }, msg => 'repos_dir is tempdir');
75     # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
76     #        if we go much further..
77     test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)');
78     test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)');
79 }
80
81 # Note - we treat an empty list of repos as if it doesn't exist at all.
82 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
83     qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
84
85 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
86     qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (array)';
87 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
88     qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
89
90 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
91     qr/No such file or directory/, 'Blows up nicely with repos list - 2 unknown items';
92
93 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
94     qr|No such file or directory|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
95
96 {
97     my $td = tempdir( CLEANUP => 1 );
98     local %ENV = %ENV;
99     $ENV{GITALIST_REPO_DIR} = $td;
100     lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
101 }
102
103 {
104     my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories"});
105     is scalar($i->repositories->flatten), 3, 'Found 3 repos';
106     isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
107 }
108
109 {
110     my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
111     is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config';
112     isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
113 }
114  {
115     my($tempfh, $wl) = tempfile(UNLINK => 1);
116     print {$tempfh} "repo1";
117     close $tempfh;
118     my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl });
119     is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist';
120     isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
121 }
122
123 {
124     my $i = test_with_config({ repos => [
125         "$FindBin::Bin/lib/repositories/bare.git",
126         "$FindBin::Bin/lib/repositories/repo1",
127         "$FindBin::Bin/lib/repositories/nodescription",
128     ]});
129     is scalar($i->repositories->flatten), 3, 'Found 3 repos';
130     isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
131 }
132
133 {
134     my $i = test_with_config({
135         repos_dir => "$FindBin::Bin/lib/repositories",
136         class    => 'TestModelSimple'
137     });
138     is scalar($i->repositories->flatten), 3, 'Found 3 repos';
139     isa_ok $i, 'TestModelSimple';
140 }
141
142 {
143     my $i = test_with_config({
144         repos_dir => "$FindBin::Bin/lib/repositories",
145         class    => 'TestModelFancy',
146         args     => { fanciness => 1 },
147     });
148     is scalar($i->repositories->flatten), 1, 'Found 1 repo';
149     isa_ok $i, 'TestModelFancy';
150     ok $i->fanciness, "The TestModelFancy is fancy (so --model-args worked)";
151 }
152
153 sub test_with_config {
154     my ($config, %opts) = @_;
155     my $msg = delete $opts{msg} || 'Built Model without exception';
156     my $ctx = $ctx_gen->(undef, %opts);
157     my $m;
158     lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
159     ok $m, 'Has model';
160     my $i = $m->ACCEPT_CONTEXT($ctx);
161     ok $i, 'Has model instance from ACCEPT_CONTEXT';
162     isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
163     is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
164     isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';
165     return $i;
166 }
167
168 done_testing;