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/;
26 my $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
32 my $stash = $orig->($c);
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};
45 my $ctx = $mock_ctx_meta->new_object(
46 response => Catalyst::Response->new,
47 request => Catalyst::Request->new,
51 $ctx->response->_context($ctx);
52 $ctx->request->_context($ctx);
58 delete $ENV{GITALIST_REPO_DIR};
60 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
61 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
63 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
64 qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist';
67 my $td = tempdir( CLEANUP => 1 );
68 test_with_config({ repo_dir => $td }, msg => 'repo_dir is tempdir');
69 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
70 # if we go much further..
71 test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)');
72 test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)');
75 # Note - we treat an empty list of repos as if it doesn't exist at all.
76 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
77 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
79 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
80 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)';
81 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
82 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
84 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
85 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items';
87 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
88 qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
91 my $td = tempdir( CLEANUP => 1 );
93 $ENV{GITALIST_REPO_DIR} = $td;
94 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
98 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
99 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
100 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
104 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
105 is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config';
106 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
109 my($tempfh, $wl) = tempfile(UNLINK => 1);
110 print {$tempfh} "repo1";
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';
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",
123 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
124 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
129 repo_dir => "$FindBin::Bin/lib/repositories",
130 class => 'ThisIsMadeOfLies',
132 } qr/Can't locate ThisIsMadeOfLies/, "Died trying to load a non-existent class";
135 my $i = test_with_config({
136 repo_dir => "$FindBin::Bin/lib/repositories",
137 class => 'TestModelSimple'
139 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
140 isa_ok $i, 'TestModelSimple';
144 my $i = test_with_config({
145 repo_dir => "$FindBin::Bin/lib/repositories",
146 class => 'TestModelFancy',
147 args => { fanciness => 1 },
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)";
154 sub test_with_config {
155 my ($config, %opts) = @_;
156 my $msg = delete $opts{msg} || 'Built Model without exception';
157 my $ctx = $ctx_gen->(undef, %opts);
159 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
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';