3 my $env = "$FindBin::Bin/../script/env";
14 use lib "$Bin/lib"; # Used for testing of --model-class etc
19 use Class::MOP::Class;
20 use Catalyst::Request;
21 use Catalyst::Response;
23 use Gitalist::Model::CollectionOfRepos;
24 use File::Temp qw/tempdir tempfile/;
27 my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
28 $mock_ctx_meta->add_method('run_options' => sub { $run_options });
29 $mock_ctx_meta->add_attribute($_, accessor => $_, required => 1) for qw/request response/;
30 $mock_ctx_meta->add_method('debug' => sub {});
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
35 my $stash = $orig->($c);
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};
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 });
50 my $host = "example.gitalist.com";
53 my $ctx = $mock_ctx_meta->new_object(
54 response => Catalyst::Response->new,
55 request => Catalyst::Request->new(uri => URI->new("http://$host/")),
59 $ctx->response->_context($ctx);
60 $ctx->request->_context($ctx);
66 delete $ENV{GITALIST_REPO_DIR};
68 throws_ok { my $i = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}); $i->{_application} = $mock_ctx_meta->name; }
69 qr/Don't know where to get repositores from/, 'Blows up nicely with no repos dir';
71 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos_dir => '/does/not/exist' }) }
72 qr|No such file or directory|, 'Blows up nicely with repos dir does not exist';
75 my $td = tempdir( CLEANUP => 1 );
76 test_with_config({ repos_dir => $td }, msg => 'repos_dir is tempdir');
77 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
78 # if we go much further..
79 test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)');
80 test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)');
83 # Note - we treat an empty list of repos as if it doesn't exist at all.
84 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
85 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
87 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
88 qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (array)';
89 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
90 qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
92 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
93 qr/No such file or directory/, 'Blows up nicely with repos list - 2 unknown items';
95 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
96 qr|No such file or directory|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
99 my $td = tempdir( CLEANUP => 1 );
101 $ENV{GITALIST_REPO_DIR} = $td;
102 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
106 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories"});
107 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
108 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
112 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
113 is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config';
114 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
117 my($tempfh, $wl) = tempfile(UNLINK => 1);
118 print {$tempfh} "repo1";
120 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl });
121 is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist';
122 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
126 my $i = test_with_config({ repos => [
127 "$FindBin::Bin/lib/repositories/bare.git",
128 "$FindBin::Bin/lib/repositories/repo1",
129 "$FindBin::Bin/lib/repositories/nodescription",
131 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
132 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
136 my $i = test_with_config({
137 repos_dir => "$FindBin::Bin/lib/repositories",
138 class => 'TestModelSimple'
140 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
141 isa_ok $i, 'TestModelSimple';
145 my $i = test_with_config({
146 repos_dir => "$FindBin::Bin/lib/repositories",
147 class => 'TestModelFancy',
148 args => { fanciness => 1 },
150 is scalar($i->repositories->flatten), 1, 'Found 1 repo';
151 isa_ok $i, 'TestModelFancy';
152 ok $i->fanciness, "The TestModelFancy is fancy (so --model-args worked)";
155 sub test_vhost_instance {
157 class => 'Gitalist::Git::CollectionOfRepositories::Vhost',
160 "git.shadowcat.co.uk" => "default",
161 "git.moose.perl.org" => "moose",
162 "git.catalyst.perl.org" => "catgit",
163 "_default_" => "default",
166 moose => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectory', repo_dir => "$FindBin::Bin/lib/repositories_sets/moose" },
167 catgit => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectory', repo_dir => "$FindBin::Bin/lib/repositories_sets/catgit" },
168 default => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive', repo_dir => "$FindBin::Bin/lib/repositories_sets"},
174 my $c_name = "$FindBin::Bin/lib/repositories_sets/catgit/Catalyst-Runtime/.git";
175 my $m_name = "$FindBin::Bin/lib/repositories_sets/moose/Moose/.git";
177 my $i = test_vhost_instance();
178 is scalar($i->repositories->flatten), 2, 'Found 2 repos on test vhost';
179 my @r = $i->repositories->flatten;
180 my @paths = sort map { $_->path . "" } $i->repositories->flatten;
181 is_deeply \@paths, [sort $c_name, $m_name];
185 $host = "git.moose.perl.org";
186 my $i = test_vhost_instance();
187 is scalar($i->repositories->flatten), 1, 'Found 1 repos on moose vhost';
188 is $i->repositories->[0]->path.'', $m_name;
192 $host = "git.catalyst.perl.org";
193 my $i = test_vhost_instance();
194 is scalar($i->repositories->flatten), 1, 'Found 1 repos on catalyst vhost';
195 is $i->repositories->[0]->path.'', $c_name;
199 $host = "git.shadowcat.co.uk";
200 my $i = test_vhost_instance();
201 is scalar($i->repositories->flatten), 2, 'Found 2 repos on git.shadowcat vhost';
202 my @paths = sort map { $_->path . "" } $i->repositories->flatten;
203 is_deeply \@paths, [sort $c_name, $m_name];
206 sub test_with_config {
207 my ($config, %opts) = @_;
208 my $msg = delete $opts{msg} || 'Built Model without exception';
209 my $ctx = $ctx_gen->(undef, %opts);
211 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
213 my $i = $m->ACCEPT_CONTEXT($ctx);
214 ok $i, 'Has model instance from ACCEPT_CONTEXT';
215 isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
216 is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
217 isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';