Fix a few test annoyances.
[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;
20f9a2d4 14use FindBin;
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
329ac3b2 26my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
27$mock_ctx_meta->add_attribute($_, accessor => $_, required => 1) for qw/request response/;
28$mock_ctx_meta->add_attribute('stash', accessor => 'stash', required => 1, default => sub { {} });
29$mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight from Catalyst.pm
30 my $orig = shift;
31 my $c = shift;
32 my $stash = $orig->($c);
33 if (@_) {
34 my $new_stash = @_ > 1 ? {@_} : $_[0];
35 croak('stash takes a hash or hashref') unless ref $new_stash;
36 foreach my $key ( keys %$new_stash ) {
37 $stash->{$key} = $new_stash->{$key};
38 }
39 }
40 return $stash;
41});
42our $ctx_gen = sub {
43 my ($cb, $stash) = @_;
44 $stash ||= {};
45 my $ctx = $mock_ctx_meta->new_object(
46 response => Catalyst::Response->new,
47 request => Catalyst::Request->new,
48 stash => { %$stash }, # Shallow copy to try and help the user out. Should we clone?
49 );
50 $ctx->response->_context($ctx);
51 $ctx->request->_context($ctx);
52 $cb->($ctx) if $cb;
53 return $ctx;
54};
55
56local %ENV = %ENV;
57delete $ENV{GITALIST_REPO_DIR};
58
7bf1a6f5 59throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
329ac3b2 60 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
61
7bf1a6f5 62throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
329ac3b2 63 qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist';
64
65{
66 my $td = tempdir( CLEANUP => 1 );
67 test_with_config({ repo_dir => $td }, 'repo_dir is tempdir');
68 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
69 # if we go much further..
70 test_with_config({ repos => $td }, 'repos is tempdir (scalar)');
71 test_with_config({ repos => [$td] }, 'repos is tempdir (array)');
72}
73
74# Note - we treat an empty list of repos as if it doesn't exist at all.
7bf1a6f5 75throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
329ac3b2 76 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
77
7bf1a6f5 78throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
329ac3b2 79 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)';
7bf1a6f5 80throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
329ac3b2 81 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
82
7bf1a6f5 83throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
329ac3b2 84 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items';
85
7bf1a6f5 86throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
329ac3b2 87 qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
88
89{
90 my $td = tempdir( CLEANUP => 1 );
91 local %ENV = %ENV;
92 $ENV{GITALIST_REPO_DIR} = $td;
7bf1a6f5 93 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
329ac3b2 94}
95
20f9a2d4 96{
97 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
b6e0be6c 98 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
07ee9dc1 99 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
1891c774 100}
101
102{
103 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
b6e0be6c 104 is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config';
1891c774 105 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
106}
07ee9dc1 107 {
108 my($tempfh, $wl) = tempfile(UNLINK => 1);
109 print {$tempfh} "repo1";
110 close $tempfh;
111 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl });
112 is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist';
113 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
114}
1891c774 115
116{
20f9a2d4 117 my $i = test_with_config({ repos => [
118 "$FindBin::Bin/lib/repositories/bare.git",
119 "$FindBin::Bin/lib/repositories/repo1",
120 "$FindBin::Bin/lib/repositories/nodescription",
121 ]});
122 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
07ee9dc1 123 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
20f9a2d4 124}
125
329ac3b2 126sub test_with_config {
127 my ($config, $msg) = @_;
128 my $ctx = $ctx_gen->();
129
130 my $m;
7bf1a6f5 131 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
329ac3b2 132 ok $m, 'Has model';
133 my $i = $m->ACCEPT_CONTEXT($ctx);
134 ok $i, 'Has model instance from ACCEPT_CONTEXT';
135 isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
136 is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
137 isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';
138 return $i;
139}
140
141done_testing;