Rename the Catalyst model to be in line with everything else. BREAKS CURRENT INSTALLS...
[catagits/Gitalist.git] / t / model_collectionofrepos.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use FindBin;
7
8 use Moose ();
9 use Moose::Object;
10 use Moose::Autobox;
11 use Class::MOP::Class;
12 use Catalyst::Request;
13 use Catalyst::Response;
14 use Catalyst::Utils;
15 use Gitalist::Model::CollectionOfRepos;
16 use File::Temp qw/tempdir/;
17
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
22     my $orig = shift;
23     my $c = shift;
24     my $stash = $orig->($c);
25     if (@_) {
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};
30         }
31     }
32     return $stash;
33 });
34 our $ctx_gen = sub {
35     my ($cb, $stash) = @_;
36     $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?
41     );
42     $ctx->response->_context($ctx);
43     $ctx->request->_context($ctx);
44     $cb->($ctx) if $cb;
45     return $ctx;
46 };
47
48 local %ENV = %ENV;
49 delete $ENV{GITALIST_REPO_DIR};
50
51 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) }
52     qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
53
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';
56
57 {
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)');
64 }
65
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';
69
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))';
74
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';
77
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';
80
81 {
82     my $td = tempdir( CLEANUP => 1 );
83     local %ENV = %ENV;
84     $ENV{GITALIST_REPO_DIR} = $td;
85     lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
86 }
87
88 {
89     my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"});
90     is scalar($i->repositories->flatten), 3, 'Found 3 repos';
91 }
92
93 {
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",
98     ]});
99     is scalar($i->repositories->flatten), 3, 'Found 3 repos';
100 }
101
102 sub test_with_config {
103     my ($config, $msg) = @_;
104     my $ctx = $ctx_gen->();
105         
106     my $m;
107     lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
108     ok $m, 'Has model';
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';
114     return $i;
115 }
116
117 done_testing;