More tests changed when a new repo was added
[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;
329ac3b2 24use File::Temp qw/tempdir/;
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"});
cc6059c0 98 is scalar($i->repositories->flatten), 6, 'Found 6 repos';
20f9a2d4 99}
100
101{
1891c774 102 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib"});
cc6059c0 103 is scalar($i->repositories->flatten), 6, 'Found 6 repos recursively';
1891c774 104 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
105}
106
107{
108 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
cc6059c0 109 is scalar($i->repositories->flatten), 6, 'Found 6 repos recursively using config';
1891c774 110 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
111}
112
113{
20f9a2d4 114 my $i = test_with_config({ repos => [
115 "$FindBin::Bin/lib/repositories/bare.git",
116 "$FindBin::Bin/lib/repositories/repo1",
117 "$FindBin::Bin/lib/repositories/nodescription",
118 ]});
119 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
120}
121
329ac3b2 122sub test_with_config {
123 my ($config, $msg) = @_;
124 my $ctx = $ctx_gen->();
125
126 my $m;
7bf1a6f5 127 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
329ac3b2 128 ok $m, 'Has model';
129 my $i = $m->ACCEPT_CONTEXT($ctx);
130 ok $i, 'Has model instance from ACCEPT_CONTEXT';
131 isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
132 is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
133 isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';
134 return $i;
135}
136
137done_testing;