Allow the model to be user defined.
[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;
e33993c9 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
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});
e33993c9 42
329ac3b2 43our $ctx_gen = sub {
e33993c9 44 my ($cb, %args) = @_;
329ac3b2 45 my $ctx = $mock_ctx_meta->new_object(
e33993c9 46 response => Catalyst::Response->new,
47 request => Catalyst::Request->new,
48 stash => {},
49 %args
329ac3b2 50 );
51 $ctx->response->_context($ctx);
52 $ctx->request->_context($ctx);
53 $cb->($ctx) if $cb;
54 return $ctx;
55};
56
57local %ENV = %ENV;
58delete $ENV{GITALIST_REPO_DIR};
59
7bf1a6f5 60throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
329ac3b2 61 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
62
7bf1a6f5 63throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
329ac3b2 64 qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist';
65
66{
67 my $td = tempdir( CLEANUP => 1 );
e33993c9 68 test_with_config({ repo_dir => $td }, msg => 'repo_dir is tempdir');
329ac3b2 69 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
70 # if we go much further..
e33993c9 71 test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)');
72 test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)');
329ac3b2 73}
74
75# Note - we treat an empty list of repos as if it doesn't exist at all.
7bf1a6f5 76throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
329ac3b2 77 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
78
7bf1a6f5 79throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
329ac3b2 80 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)';
7bf1a6f5 81throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
329ac3b2 82 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
83
7bf1a6f5 84throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
329ac3b2 85 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items';
86
7bf1a6f5 87throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
329ac3b2 88 qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
89
90{
91 my $td = tempdir( CLEANUP => 1 );
92 local %ENV = %ENV;
93 $ENV{GITALIST_REPO_DIR} = $td;
7bf1a6f5 94 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
329ac3b2 95}
96
20f9a2d4 97{
98 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
b6e0be6c 99 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
07ee9dc1 100 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
1891c774 101}
102
103{
104 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
b6e0be6c 105 is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config';
1891c774 106 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
107}
07ee9dc1 108 {
109 my($tempfh, $wl) = tempfile(UNLINK => 1);
110 print {$tempfh} "repo1";
111 close $tempfh;
112 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl });
113 is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist';
114 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
115}
1891c774 116
117{
20f9a2d4 118 my $i = test_with_config({ repos => [
119 "$FindBin::Bin/lib/repositories/bare.git",
120 "$FindBin::Bin/lib/repositories/repo1",
121 "$FindBin::Bin/lib/repositories/nodescription",
122 ]});
123 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
07ee9dc1 124 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
20f9a2d4 125}
126
e33993c9 127throws_ok {
128 test_with_config({
129 repo_dir => "$FindBin::Bin/lib/repositories",
130 class => 'ThisIsMadeOfLies',
131 });
132} qr/Can't locate ThisIsMadeOfLies/, "Died trying to load a non-existent class";
133
134{
135 my $i = test_with_config({
136 repo_dir => "$FindBin::Bin/lib/repositories",
137 class => 'TestModelSimple'
138 });
139 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
140 isa_ok $i, 'TestModelSimple';
141}
142
143{
144 my $i = test_with_config({
145 repo_dir => "$FindBin::Bin/lib/repositories",
146 class => 'TestModelFancy',
147 args => { fanciness => 1 },
148 });
149 is scalar($i->repositories->flatten), 1, 'Found 1 repo';
150 isa_ok $i, 'TestModelFancy';
151 ok $i->fanciness, "The TestModelFancy is fancy (so --model-args worked)";
152}
153
329ac3b2 154sub test_with_config {
e33993c9 155 my ($config, %opts) = @_;
156 my $msg = delete $opts{msg} || 'Built Model without exception';
157 my $ctx = $ctx_gen->(undef, %opts);
329ac3b2 158 my $m;
7bf1a6f5 159 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
329ac3b2 160 ok $m, 'Has model';
161 my $i = $m->ACCEPT_CONTEXT($ctx);
162 ok $i, 'Has model instance from ACCEPT_CONTEXT';
163 isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
164 is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
165 isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';
166 return $i;
167}
168
169done_testing;