Make the tests pass again
[catagits/Gitalist.git] / t / model_collectionofrepos.t
CommitLineData
5ed74c87 1use FindBin qw/$Bin/;
df629266 2BEGIN {
0556ab26 3 my $env = "$FindBin::Bin/../script/env";
df629266 4 if (-r $env) {
5 do $env or die $@;
6 }
7}
8
329ac3b2 9use strict;
10use warnings;
11
12use Test::More;
13use Test::Exception;
0fd9a84a 14use lib "$Bin/lib"; # Used for testing of --model-class etc
329ac3b2 15
16use Moose ();
17use Moose::Object;
20f9a2d4 18use Moose::Autobox;
329ac3b2 19use Class::MOP::Class;
20use Catalyst::Request;
21use Catalyst::Response;
22use Catalyst::Utils;
7bf1a6f5 23use Gitalist::Model::CollectionOfRepos;
07ee9dc1 24use File::Temp qw/tempdir tempfile/;
7bf1a6f5 25
ea772511 26my $run_options = {};
329ac3b2 27my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
ea772511 28$mock_ctx_meta->add_method('run_options' => sub { $run_options });
329ac3b2 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});
ea772511 44my $mock_log = Moose::Meta::Class->create_anon_class( superclasses => ['Moose::Object'] );
45$mock_log->add_method($_ => sub {}) for qw/ warn info debug /;
46my $logger = $mock_log->name->new;
47$mock_ctx_meta->add_method('log' => sub { $logger });
0fd9a84a 48
329ac3b2 49our $ctx_gen = sub {
0fd9a84a 50 my ($cb, %args) = @_;
329ac3b2 51 my $ctx = $mock_ctx_meta->new_object(
0fd9a84a 52 response => Catalyst::Response->new,
53 request => Catalyst::Request->new,
54 stash => {},
55 %args
329ac3b2 56 );
57 $ctx->response->_context($ctx);
58 $ctx->request->_context($ctx);
59 $cb->($ctx) if $cb;
60 return $ctx;
61};
62
63local %ENV = %ENV;
64delete $ENV{GITALIST_REPO_DIR};
65
ea772511 66throws_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';
329ac3b2 68
ea772511 69throws_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';
329ac3b2 71
72{
73 my $td = tempdir( CLEANUP => 1 );
ea772511 74 test_with_config({ repos_dir => $td }, msg => 'repos_dir is tempdir');
329ac3b2 75 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
76 # if we go much further..
0fd9a84a 77 test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)');
78 test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)');
329ac3b2 79}
80
81# Note - we treat an empty list of repos as if it doesn't exist at all.
7bf1a6f5 82throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
329ac3b2 83 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
84
7bf1a6f5 85throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
ea772511 86 qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (array)';
7bf1a6f5 87throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
ea772511 88 qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
329ac3b2 89
7bf1a6f5 90throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
ea772511 91 qr/No such file or directory/, 'Blows up nicely with repos list - 2 unknown items';
329ac3b2 92
7bf1a6f5 93throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
ea772511 94 qr|No such file or directory|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
329ac3b2 95
96{
97 my $td = tempdir( CLEANUP => 1 );
98 local %ENV = %ENV;
99 $ENV{GITALIST_REPO_DIR} = $td;
7bf1a6f5 100 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
329ac3b2 101}
102
20f9a2d4 103{
ea772511 104 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories"});
039a5ec4 105 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
07ee9dc1 106 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
1891c774 107}
108
109{
ea772511 110 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
039a5ec4 111 is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config';
1891c774 112 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
113}
07ee9dc1 114 {
115 my($tempfh, $wl) = tempfile(UNLINK => 1);
116 print {$tempfh} "repo1";
117 close $tempfh;
ea772511 118 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl });
07ee9dc1 119 is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist';
120 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
121}
1891c774 122
123{
20f9a2d4 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';
07ee9dc1 130 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
20f9a2d4 131}
132
0fd9a84a 133{
134 my $i = test_with_config({
ea772511 135 repos_dir => "$FindBin::Bin/lib/repositories",
0fd9a84a 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({
ea772511 144 repos_dir => "$FindBin::Bin/lib/repositories",
0fd9a84a 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
329ac3b2 153sub test_with_config {
0fd9a84a 154 my ($config, %opts) = @_;
155 my $msg = delete $opts{msg} || 'Built Model without exception';
156 my $ctx = $ctx_gen->(undef, %opts);
329ac3b2 157 my $m;
7bf1a6f5 158 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
329ac3b2 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
168done_testing;