3 my $env = "$FindBin::Bin/../script/env";
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};
43 my ($cb, $stash) = @_;
45 my $ctx = $mock_ctx_meta->new_object(
46 response => Catalyst::Response->new,
47 request => Catalyst::Request->new,
48 stash => { %$stash }, # Shallow copy to try and help the user out. Should we clone?
50 $ctx->response->_context($ctx);
51 $ctx->request->_context($ctx);
57 delete $ENV{GITALIST_REPO_DIR};
59 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
60 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
62 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
63 qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist';
66 my $td = tempdir( CLEANUP => 1 );
67 test_with_config({ repo_dir => $td }, 'repo_dir is tempdir');
68 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
69 # if we go much further..
70 test_with_config({ repos => $td }, 'repos is tempdir (scalar)');
71 test_with_config({ repos => [$td] }, 'repos is tempdir (array)');
74 # Note - we treat an empty list of repos as if it doesn't exist at all.
75 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
76 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
78 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
79 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)';
80 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
81 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
83 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
84 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items';
86 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
87 qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
90 my $td = tempdir( CLEANUP => 1 );
92 $ENV{GITALIST_REPO_DIR} = $td;
93 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
97 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
98 is scalar($i->repositories->flatten), 3, 'Found 6 repos';
99 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
103 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
104 is scalar($i->repositories->flatten), 6, 'Found 6 repos recursively using config';
105 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
108 my($tempfh, $wl) = tempfile(UNLINK => 1);
109 print {$tempfh} "repo1";
111 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl });
112 is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist';
113 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
117 my $i = test_with_config({ repos => [
118 "$FindBin::Bin/lib/repositories/bare.git",
119 "$FindBin::Bin/lib/repositories/repo1",
120 "$FindBin::Bin/lib/repositories/nodescription",
122 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
123 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
126 sub test_with_config {
127 my ($config, $msg) = @_;
128 my $ctx = $ctx_gen->();
131 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
133 my $i = $m->ACCEPT_CONTEXT($ctx);
134 ok $i, 'Has model instance from ACCEPT_CONTEXT';
135 isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
136 is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
137 isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';