ae1a6a9124b20c37f02de07b321fd90ea56b34c5
[catagits/Gitalist.git] / t / model_collectionofrepos.t
1 use FindBin qw/$Bin/;
2 BEGIN { do "$FindBin::Bin/../script/env" or die $@ }
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use FindBin;
9
10 use Moose ();
11 use Moose::Object;
12 use Moose::Autobox;
13 use Class::MOP::Class;
14 use Catalyst::Request;
15 use Catalyst::Response;
16 use Catalyst::Utils;
17 use Gitalist::Model::CollectionOfRepos;
18 use File::Temp qw/tempdir/;
19
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
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 });
36 our $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
50 local %ENV = %ENV;
51 delete $ENV{GITALIST_REPO_DIR};
52
53 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
54     qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
55
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';
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.
69 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
70     qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
71
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))';
76
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';
79
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';
82
83 {
84     my $td = tempdir( CLEANUP => 1 );
85     local %ENV = %ENV;
86     $ENV{GITALIST_REPO_DIR} = $td;
87     lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
88 }
89
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
104 sub test_with_config {
105     my ($config, $msg) = @_;
106     my $ctx = $ctx_gen->();
107         
108     my $m;
109     lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
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
119 done_testing;