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 = "git.shadowcat.co.uk";
51 $mock_ctx_meta->add_method('uri' => sub { URI->new("http://$host/") });
54 my $ctx = $mock_ctx_meta->new_object(
55 response => Catalyst::Response->new,
56 # Too lazy to mock up Catalyst::Log
57 request => Catalyst::Request->new(uri => URI->new("http://$host/"), _log => 1),
66 delete $ENV{GITALIST_CONFIG};
67 delete $ENV{GITALIST_REPO_DIR};
69 throws_ok { my $i = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}); $i->{_application} = $mock_ctx_meta->name; }
70 qr/Don't know where to get repositores from/, 'Blows up nicely with no repos dir';
72 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
73 qr|No such file or directory|, 'Blows up nicely with repos dir does not exist';
76 my $td = tempdir( CLEANUP => 1 );
77 test_with_config({ repo_dir => $td }, msg => 'repo_dir is tempdir');
78 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
79 # if we go much further..
80 test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)');
81 test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)');
84 # Note - we treat an empty list of repos as if it doesn't exist at all.
85 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
86 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
88 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
89 qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (array)';
90 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
91 qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
93 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
94 qr/No such file or directory/, 'Blows up nicely with repos list - 2 unknown items';
96 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
97 qr|No such file or directory|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
100 my $td = tempdir( CLEANUP => 1 );
102 $ENV{GITALIST_REPO_DIR} = $td;
103 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
107 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
108 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
109 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
113 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
114 is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config';
115 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
118 my($tempfh, $wl) = tempfile(UNLINK => 1);
119 print {$tempfh} "repo1";
121 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl });
122 is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist';
123 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
127 my $i = test_with_config({ repos => [
128 "$FindBin::Bin/lib/repositories/bare.git",
129 "$FindBin::Bin/lib/repositories/repo1",
130 "$FindBin::Bin/lib/repositories/nodescription",
132 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
133 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
137 my $i = test_with_config({
138 repo_dir => "$FindBin::Bin/lib/repositories",
139 class => 'TestModelSimple'
141 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
142 isa_ok $i, 'TestModelSimple';
146 my $i = test_with_config({
147 repo_dir => "$FindBin::Bin/lib/repositories",
148 class => 'TestModelFancy',
149 args => { fanciness => 1 },
151 is scalar($i->repositories->flatten), 1, 'Found 1 repo';
152 isa_ok $i, 'TestModelFancy';
153 ok $i->fanciness, "The TestModelFancy is fancy (so --model-args worked)";
156 sub test_vhost_instance {
158 class => 'Gitalist::Git::CollectionOfRepositories::Vhost',
161 "git.shadowcat.co.uk" => "default",
162 "git.moose.perl.org" => "moose",
163 "git.catalyst.perl.org" => "catgit",
164 "_default_" => "default",
167 moose => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectory', repo_dir => "$FindBin::Bin/lib/repositories_sets/moose" },
168 catgit => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectory', repo_dir => "$FindBin::Bin/lib/repositories_sets/catgit" },
169 default => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive', repo_dir => "$FindBin::Bin/lib/repositories_sets"},
175 my $c_name = "$FindBin::Bin/lib/repositories_sets/catgit/Catalyst-Runtime";
176 my $m_name = "$FindBin::Bin/lib/repositories_sets/moose/Moose";
178 my $i = test_vhost_instance();
179 is scalar($i->repositories->flatten), 2, 'Found 2 repos on test vhost';
180 my @r = $i->repositories->flatten;
181 my @paths = sort map { $_->path . "" } $i->repositories->flatten;
182 is_deeply \@paths, [sort $c_name, $m_name];
186 $host = "git.moose.perl.org";
187 my $i = test_vhost_instance();
188 is scalar($i->repositories->flatten), 1, 'Found 1 repos on moose vhost';
189 is $i->repositories->[0]->path.'', $m_name;
193 $host = "git.catalyst.perl.org";
194 my $i = test_vhost_instance();
195 is scalar($i->repositories->flatten), 1, 'Found 1 repos on catalyst vhost';
196 is $i->repositories->[0]->path.'', $c_name;
200 $host = "git.shadowcat.co.uk";
201 my $i = test_vhost_instance();
202 is scalar($i->repositories->flatten), 2, 'Found 2 repos on git.shadowcat vhost';
203 my @paths = sort map { $_->path . "" } $i->repositories->flatten;
204 is_deeply \@paths, [sort $c_name, $m_name];
207 sub test_with_config {
208 my ($config, %opts) = @_;
209 my $msg = delete $opts{msg} || 'Built Model without exception';
210 my $ctx = $ctx_gen->(undef, %opts);
212 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
214 my $i = $m->ACCEPT_CONTEXT($ctx);
215 ok $i, 'Has model instance from ACCEPT_CONTEXT';