Commit | Line | Data |
5ed74c87 |
1 | use FindBin qw/$Bin/; |
df629266 |
2 | BEGIN { |
0556ab26 |
3 | my $env = "$FindBin::Bin/../script/env"; |
df629266 |
4 | if (-r $env) { |
5 | do $env or die $@; |
6 | } |
7 | } |
8 | |
329ac3b2 |
9 | use strict; |
10 | use warnings; |
11 | |
12 | use Test::More; |
13 | use Test::Exception; |
0fd9a84a |
14 | use lib "$Bin/lib"; # Used for testing of --model-class etc |
329ac3b2 |
15 | |
16 | use Moose (); |
17 | use Moose::Object; |
20f9a2d4 |
18 | use Moose::Autobox; |
329ac3b2 |
19 | use Class::MOP::Class; |
20 | use Catalyst::Request; |
21 | use Catalyst::Response; |
22 | use Catalyst::Utils; |
7bf1a6f5 |
23 | use Gitalist::Model::CollectionOfRepos; |
07ee9dc1 |
24 | use File::Temp qw/tempdir tempfile/; |
7bf1a6f5 |
25 | |
ea772511 |
26 | my $run_options = {}; |
329ac3b2 |
27 | my $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/; |
31d3c368 |
30 | $mock_ctx_meta->add_method('debug' => sub {}); |
329ac3b2 |
31 | $mock_ctx_meta->add_attribute('stash', accessor => 'stash', required => 1, default => sub { {} }); |
32 | $mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight from Catalyst.pm |
33 | my $orig = shift; |
34 | my $c = shift; |
35 | my $stash = $orig->($c); |
36 | if (@_) { |
37 | my $new_stash = @_ > 1 ? {@_} : $_[0]; |
38 | croak('stash takes a hash or hashref') unless ref $new_stash; |
39 | foreach my $key ( keys %$new_stash ) { |
40 | $stash->{$key} = $new_stash->{$key}; |
41 | } |
42 | } |
43 | return $stash; |
44 | }); |
ea772511 |
45 | my $mock_log = Moose::Meta::Class->create_anon_class( superclasses => ['Moose::Object'] ); |
46 | $mock_log->add_method($_ => sub {}) for qw/ warn info debug /; |
47 | my $logger = $mock_log->name->new; |
48 | $mock_ctx_meta->add_method('log' => sub { $logger }); |
0fd9a84a |
49 | |
4d334b53 |
50 | my $host = "git.shadowcat.co.uk"; |
51 | $mock_ctx_meta->add_method('uri' => sub { URI->new("http://$host/") }); |
329ac3b2 |
52 | our $ctx_gen = sub { |
0fd9a84a |
53 | my ($cb, %args) = @_; |
329ac3b2 |
54 | my $ctx = $mock_ctx_meta->new_object( |
0fd9a84a |
55 | response => Catalyst::Response->new, |
31d3c368 |
56 | request => Catalyst::Request->new(uri => URI->new("http://$host/")), |
0fd9a84a |
57 | stash => {}, |
58 | %args |
329ac3b2 |
59 | ); |
60 | $ctx->response->_context($ctx); |
61 | $ctx->request->_context($ctx); |
62 | $cb->($ctx) if $cb; |
63 | return $ctx; |
64 | }; |
65 | |
66 | local %ENV = %ENV; |
4d334b53 |
67 | delete $ENV{GITALIST_CONFIG}; |
329ac3b2 |
68 | delete $ENV{GITALIST_REPO_DIR}; |
69 | |
ea772511 |
70 | throws_ok { my $i = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}); $i->{_application} = $mock_ctx_meta->name; } |
71 | qr/Don't know where to get repositores from/, 'Blows up nicely with no repos dir'; |
329ac3b2 |
72 | |
f4184925 |
73 | throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) } |
ea772511 |
74 | qr|No such file or directory|, 'Blows up nicely with repos dir does not exist'; |
329ac3b2 |
75 | |
76 | { |
77 | my $td = tempdir( CLEANUP => 1 ); |
f4184925 |
78 | test_with_config({ repo_dir => $td }, msg => 'repo_dir is tempdir'); |
329ac3b2 |
79 | # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully) |
80 | # if we go much further.. |
0fd9a84a |
81 | test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)'); |
82 | test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)'); |
329ac3b2 |
83 | } |
84 | |
85 | # Note - we treat an empty list of repos as if it doesn't exist at all. |
7bf1a6f5 |
86 | throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) } |
329ac3b2 |
87 | qr/Cannot find repository dir/, 'Blows up nicely with no repos list'; |
88 | |
7bf1a6f5 |
89 | throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) } |
ea772511 |
90 | qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (array)'; |
7bf1a6f5 |
91 | throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) } |
ea772511 |
92 | qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (scalar))'; |
329ac3b2 |
93 | |
7bf1a6f5 |
94 | throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) } |
ea772511 |
95 | qr/No such file or directory/, 'Blows up nicely with repos list - 2 unknown items'; |
329ac3b2 |
96 | |
7bf1a6f5 |
97 | throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) } |
ea772511 |
98 | qr|No such file or directory|, 'Blows up nicely with repos list - 1 known, 1 unknown items'; |
329ac3b2 |
99 | |
100 | { |
101 | my $td = tempdir( CLEANUP => 1 ); |
102 | local %ENV = %ENV; |
103 | $ENV{GITALIST_REPO_DIR} = $td; |
7bf1a6f5 |
104 | lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works'; |
329ac3b2 |
105 | } |
106 | |
20f9a2d4 |
107 | { |
f4184925 |
108 | my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"}); |
039a5ec4 |
109 | is scalar($i->repositories->flatten), 3, 'Found 3 repos'; |
07ee9dc1 |
110 | isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory'; |
1891c774 |
111 | } |
112 | |
113 | { |
f4184925 |
114 | my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 }); |
039a5ec4 |
115 | is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config'; |
1891c774 |
116 | isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive'; |
117 | } |
07ee9dc1 |
118 | { |
119 | my($tempfh, $wl) = tempfile(UNLINK => 1); |
120 | print {$tempfh} "repo1"; |
121 | close $tempfh; |
f4184925 |
122 | my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl }); |
07ee9dc1 |
123 | is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist'; |
124 | isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList'; |
125 | } |
1891c774 |
126 | |
127 | { |
20f9a2d4 |
128 | my $i = test_with_config({ repos => [ |
129 | "$FindBin::Bin/lib/repositories/bare.git", |
130 | "$FindBin::Bin/lib/repositories/repo1", |
131 | "$FindBin::Bin/lib/repositories/nodescription", |
132 | ]}); |
133 | is scalar($i->repositories->flatten), 3, 'Found 3 repos'; |
07ee9dc1 |
134 | isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories'; |
20f9a2d4 |
135 | } |
136 | |
0fd9a84a |
137 | { |
138 | my $i = test_with_config({ |
f4184925 |
139 | repo_dir => "$FindBin::Bin/lib/repositories", |
0fd9a84a |
140 | class => 'TestModelSimple' |
141 | }); |
142 | is scalar($i->repositories->flatten), 3, 'Found 3 repos'; |
143 | isa_ok $i, 'TestModelSimple'; |
144 | } |
145 | |
146 | { |
147 | my $i = test_with_config({ |
f4184925 |
148 | repo_dir => "$FindBin::Bin/lib/repositories", |
0fd9a84a |
149 | class => 'TestModelFancy', |
150 | args => { fanciness => 1 }, |
151 | }); |
152 | is scalar($i->repositories->flatten), 1, 'Found 1 repo'; |
153 | isa_ok $i, 'TestModelFancy'; |
154 | ok $i->fanciness, "The TestModelFancy is fancy (so --model-args worked)"; |
155 | } |
156 | |
31d3c368 |
157 | sub test_vhost_instance { |
158 | test_with_config({ |
159 | class => 'Gitalist::Git::CollectionOfRepositories::Vhost', |
160 | args => { |
161 | vhost_dispatch => { |
162 | "git.shadowcat.co.uk" => "default", |
163 | "git.moose.perl.org" => "moose", |
164 | "git.catalyst.perl.org" => "catgit", |
165 | "_default_" => "default", |
166 | }, |
167 | collections => { |
168 | moose => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectory', repo_dir => "$FindBin::Bin/lib/repositories_sets/moose" }, |
169 | catgit => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectory', repo_dir => "$FindBin::Bin/lib/repositories_sets/catgit" }, |
170 | default => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive', repo_dir => "$FindBin::Bin/lib/repositories_sets"}, |
171 | } |
172 | }, |
173 | }); |
174 | } |
175 | |
4d334b53 |
176 | my $c_name = "$FindBin::Bin/lib/repositories_sets/catgit/Catalyst-Runtime"; |
177 | my $m_name = "$FindBin::Bin/lib/repositories_sets/moose/Moose"; |
31d3c368 |
178 | { |
179 | my $i = test_vhost_instance(); |
180 | is scalar($i->repositories->flatten), 2, 'Found 2 repos on test vhost'; |
181 | my @r = $i->repositories->flatten; |
182 | my @paths = sort map { $_->path . "" } $i->repositories->flatten; |
183 | is_deeply \@paths, [sort $c_name, $m_name]; |
184 | } |
185 | |
186 | { |
187 | $host = "git.moose.perl.org"; |
188 | my $i = test_vhost_instance(); |
189 | is scalar($i->repositories->flatten), 1, 'Found 1 repos on moose vhost'; |
190 | is $i->repositories->[0]->path.'', $m_name; |
191 | } |
192 | |
193 | { |
194 | $host = "git.catalyst.perl.org"; |
195 | my $i = test_vhost_instance(); |
196 | is scalar($i->repositories->flatten), 1, 'Found 1 repos on catalyst vhost'; |
197 | is $i->repositories->[0]->path.'', $c_name; |
198 | } |
199 | |
200 | { |
201 | $host = "git.shadowcat.co.uk"; |
202 | my $i = test_vhost_instance(); |
203 | is scalar($i->repositories->flatten), 2, 'Found 2 repos on git.shadowcat vhost'; |
204 | my @paths = sort map { $_->path . "" } $i->repositories->flatten; |
205 | is_deeply \@paths, [sort $c_name, $m_name]; |
206 | } |
207 | |
329ac3b2 |
208 | sub test_with_config { |
0fd9a84a |
209 | my ($config, %opts) = @_; |
210 | my $msg = delete $opts{msg} || 'Built Model without exception'; |
211 | my $ctx = $ctx_gen->(undef, %opts); |
329ac3b2 |
212 | my $m; |
7bf1a6f5 |
213 | lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg; |
329ac3b2 |
214 | ok $m, 'Has model'; |
215 | my $i = $m->ACCEPT_CONTEXT($ctx); |
216 | ok $i, 'Has model instance from ACCEPT_CONTEXT'; |
329ac3b2 |
217 | return $i; |
218 | } |
219 | |
220 | done_testing; |