Bump Gitalist version and required Git::PurePerl version.
[catagits/Gitalist.git] / t / model_collectionofrepos.t
CommitLineData
5ed74c87 1use FindBin qw/$Bin/;
2BEGIN { do "$FindBin::Bin/../script/env" or die $@ }
329ac3b2 3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
20f9a2d4 8use FindBin;
329ac3b2 9
10use Moose ();
11use Moose::Object;
20f9a2d4 12use Moose::Autobox;
329ac3b2 13use Class::MOP::Class;
14use Catalyst::Request;
15use Catalyst::Response;
16use Catalyst::Utils;
7bf1a6f5 17use Gitalist::Model::CollectionOfRepos;
329ac3b2 18use File::Temp qw/tempdir/;
7bf1a6f5 19
329ac3b2 20my $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
24 my $orig = shift;
25 my $c = shift;
26 my $stash = $orig->($c);
27 if (@_) {
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};
32 }
33 }
34 return $stash;
35});
36our $ctx_gen = sub {
37 my ($cb, $stash) = @_;
38 $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?
43 );
44 $ctx->response->_context($ctx);
45 $ctx->request->_context($ctx);
46 $cb->($ctx) if $cb;
47 return $ctx;
48};
49
50local %ENV = %ENV;
51delete $ENV{GITALIST_REPO_DIR};
52
7bf1a6f5 53throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
329ac3b2 54 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
55
7bf1a6f5 56throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
329ac3b2 57 qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist';
58
59{
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)');
66}
67
68# Note - we treat an empty list of repos as if it doesn't exist at all.
7bf1a6f5 69throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
329ac3b2 70 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
71
7bf1a6f5 72throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
329ac3b2 73 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)';
7bf1a6f5 74throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
329ac3b2 75 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
76
7bf1a6f5 77throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
329ac3b2 78 qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items';
79
7bf1a6f5 80throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
329ac3b2 81 qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
82
83{
84 my $td = tempdir( CLEANUP => 1 );
85 local %ENV = %ENV;
86 $ENV{GITALIST_REPO_DIR} = $td;
7bf1a6f5 87 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
329ac3b2 88}
89
20f9a2d4 90{
91 my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
92 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
93}
94
95{
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",
100 ]});
101 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
102}
103
329ac3b2 104sub test_with_config {
105 my ($config, $msg) = @_;
106 my $ctx = $ctx_gen->();
107
108 my $m;
7bf1a6f5 109 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
329ac3b2 110 ok $m, 'Has model';
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';
116 return $i;
117}
118
119done_testing;