Fix tests dying in absence of bootstrap scripts.
[catagits/Gitalist.git] / t / model_collectionofrepos.t
1 use FindBin qw/$Bin/;
2 BEGIN {
3     my $env = "$FindBin::Bin/script/env";
4     if (-r $env) {
5         do $env or die $@;
6     }
7 }
8
9 use strict;
10 use warnings;
11
12 use Test::More;
13 use Test::Exception;
14 use FindBin;
15
16 use Moose ();
17 use Moose::Object;
18 use Moose::Autobox;
19 use Class::MOP::Class;
20 use Catalyst::Request;
21 use Catalyst::Response;
22 use Catalyst::Utils;
23 use Gitalist::Model::CollectionOfRepos;
24 use File::Temp qw/tempdir/;
25
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
30     my $orig = shift;
31     my $c = shift;
32     my $stash = $orig->($c);
33     if (@_) {
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};
38         }
39     }
40     return $stash;
41 });
42 our $ctx_gen = sub {
43     my ($cb, $stash) = @_;
44     $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?
49     );
50     $ctx->response->_context($ctx);
51     $ctx->request->_context($ctx);
52     $cb->($ctx) if $cb;
53     return $ctx;
54 };
55
56 local %ENV = %ENV;
57 delete $ENV{GITALIST_REPO_DIR};
58
59 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
60     qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
61
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';
64
65 {
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)');
72 }
73
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';
77
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))';
82
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';
85
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';
88
89 {
90     my $td = tempdir( CLEANUP => 1 );
91     local %ENV = %ENV;
92     $ENV{GITALIST_REPO_DIR} = $td;
93     lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
94 }
95
96 {
97     my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
98     is scalar($i->repositories->flatten), 3, 'Found 3 repos';
99 }
100
101 {
102     my $i = test_with_config({ repos => [
103         "$FindBin::Bin/lib/repositories/bare.git",
104         "$FindBin::Bin/lib/repositories/repo1",
105         "$FindBin::Bin/lib/repositories/nodescription",
106     ]});
107     is scalar($i->repositories->flatten), 3, 'Found 3 repos';
108 }
109
110 sub test_with_config {
111     my ($config, $msg) = @_;
112     my $ctx = $ctx_gen->();
113         
114     my $m;
115     lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
116     ok $m, 'Has model';
117     my $i = $m->ACCEPT_CONTEXT($ctx);
118     ok $i, 'Has model instance from ACCEPT_CONTEXT';
119     isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
120     is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
121     isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';
122     return $i;
123 }
124
125 done_testing;