11 use Class::MOP::Class;
12 use Catalyst::Request;
13 use Catalyst::Response;
15 use Gitalist::Model::CollectionOfRepos;
16 use File::Temp qw/tempdir/;
18 my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
19 $mock_ctx_meta->add_attribute($_, accessor => $_, required => 1) for qw/request response/;
20 $mock_ctx_meta->add_attribute('stash', accessor => 'stash', required => 1, default => sub { {} });
21 $mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight from Catalyst.pm
24 my $stash = $orig->($c);
26 my $new_stash = @_ > 1 ? {@_} : $_[0];
27 croak('stash takes a hash or hashref') unless ref $new_stash;
28 foreach my $key ( keys %$new_stash ) {
29 $stash->{$key} = $new_stash->{$key};
35 my ($cb, $stash) = @_;
37 my $ctx = $mock_ctx_meta->new_object(
38 response => Catalyst::Response->new,
39 request => Catalyst::Request->new,
40 stash => { %$stash }, # Shallow copy to try and help the user out. Should we clone?
42 $ctx->response->_context($ctx);
43 $ctx->request->_context($ctx);
49 delete $ENV{GITALIST_REPO_DIR};
51 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
52 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
54 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
55 qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist';
58 my $td = tempdir( CLEANUP => 1 );
59 test_with_config({ repo_dir => $td }, 'repo_dir is tempdir');
60 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
61 # if we go much further..
62 test_with_config({ repos => $td }, 'repos is tempdir (scalar)');
63 test_with_config({ repos => [$td] }, 'repos is tempdir (array)');
66 # Note - we treat an empty list of repos as if it doesn't exist at all.
67 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
68 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
70 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
71 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)';
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 (scalar))';
75 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
76 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items';
78 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
79 qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
82 my $td = tempdir( CLEANUP => 1 );
84 $ENV{GITALIST_REPO_DIR} = $td;
85 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
89 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
90 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
94 my $i = test_with_config({ repos => [
95 "$FindBin::Bin/lib/repositories/bare.git",
96 "$FindBin::Bin/lib/repositories/repo1",
97 "$FindBin::Bin/lib/repositories/nodescription",
99 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
102 sub test_with_config {
103 my ($config, $msg) = @_;
104 my $ctx = $ctx_gen->();
107 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
109 my $i = $m->ACCEPT_CONTEXT($ctx);
110 ok $i, 'Has model instance from ACCEPT_CONTEXT';
111 isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
112 is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
113 isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';