2 BEGIN { do "$FindBin::Bin/../script/env" or die $@ }
13 use Class::MOP::Class;
14 use Catalyst::Request;
15 use Catalyst::Response;
17 use Gitalist::Model::CollectionOfRepos;
18 use File::Temp qw/tempdir/;
20 my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
21 $mock_ctx_meta->add_attribute($_, accessor => $_, required => 1) for qw/request response/;
22 $mock_ctx_meta->add_attribute('stash', accessor => 'stash', required => 1, default => sub { {} });
23 $mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight from Catalyst.pm
26 my $stash = $orig->($c);
28 my $new_stash = @_ > 1 ? {@_} : $_[0];
29 croak('stash takes a hash or hashref') unless ref $new_stash;
30 foreach my $key ( keys %$new_stash ) {
31 $stash->{$key} = $new_stash->{$key};
37 my ($cb, $stash) = @_;
39 my $ctx = $mock_ctx_meta->new_object(
40 response => Catalyst::Response->new,
41 request => Catalyst::Request->new,
42 stash => { %$stash }, # Shallow copy to try and help the user out. Should we clone?
44 $ctx->response->_context($ctx);
45 $ctx->request->_context($ctx);
51 delete $ENV{GITALIST_REPO_DIR};
53 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
54 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
56 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
57 qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist';
60 my $td = tempdir( CLEANUP => 1 );
61 test_with_config({ repo_dir => $td }, 'repo_dir is tempdir');
62 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
63 # if we go much further..
64 test_with_config({ repos => $td }, 'repos is tempdir (scalar)');
65 test_with_config({ repos => [$td] }, 'repos is tempdir (array)');
68 # Note - we treat an empty list of repos as if it doesn't exist at all.
69 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
70 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
72 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
73 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)';
74 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
75 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
77 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
78 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items';
80 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
81 qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
84 my $td = tempdir( CLEANUP => 1 );
86 $ENV{GITALIST_REPO_DIR} = $td;
87 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
91 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
92 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
96 my $i = test_with_config({ repos => [
97 "$FindBin::Bin/lib/repositories/bare.git",
98 "$FindBin::Bin/lib/repositories/repo1",
99 "$FindBin::Bin/lib/repositories/nodescription",
101 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
104 sub test_with_config {
105 my ($config, $msg) = @_;
106 my $ctx = $ctx_gen->();
109 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
111 my $i = $m->ACCEPT_CONTEXT($ctx);
112 ok $i, 'Has model instance from ACCEPT_CONTEXT';
113 isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
114 is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
115 isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';