Add testing for the catalyst model behavior.
[catagits/Gitalist.git] / t / model_gitrepos.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6
7 use Moose ();
8 use Moose::Object;
9 use Class::MOP::Class;
10 use Catalyst::Request;
11 use Catalyst::Response;
12 use Catalyst::Utils;
13 use Gitalist::Model::GitRepos;
14 use File::Temp qw/tempdir/;
15  
16 my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
17 $mock_ctx_meta->add_attribute($_, accessor => $_, required => 1) for qw/request response/;
18 $mock_ctx_meta->add_attribute('stash', accessor => 'stash', required => 1, default => sub { {} });
19 $mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight from Catalyst.pm
20     my $orig = shift;
21     my $c = shift;
22     my $stash = $orig->($c);
23     if (@_) {
24         my $new_stash = @_ > 1 ? {@_} : $_[0];
25         croak('stash takes a hash or hashref') unless ref $new_stash;
26         foreach my $key ( keys %$new_stash ) {
27           $stash->{$key} = $new_stash->{$key};
28         }
29     }
30     return $stash;
31 });
32 our $ctx_gen = sub {
33     my ($cb, $stash) = @_;
34     $stash ||= {};
35     my $ctx = $mock_ctx_meta->new_object(
36         response => Catalyst::Response->new,
37         request => Catalyst::Request->new,
38         stash => { %$stash }, # Shallow copy to try and help the user out. Should we clone?
39     );
40     $ctx->response->_context($ctx);
41     $ctx->request->_context($ctx);
42     $cb->($ctx) if $cb;
43     return $ctx;
44 };
45
46 local %ENV = %ENV;
47 delete $ENV{GITALIST_REPO_DIR};
48
49 throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), {}) }
50     qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
51
52 throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) }
53     qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist';
54
55 {
56     my $td = tempdir( CLEANUP => 1 );
57     test_with_config({ repo_dir => $td }, 'repo_dir is tempdir');
58     # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
59     #        if we go much further..
60     test_with_config({ repos => $td }, 'repos is tempdir (scalar)');
61     test_with_config({ repos => [$td] }, 'repos is tempdir (array)');
62 }
63
64 # Note - we treat an empty list of repos as if it doesn't exist at all.
65 throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
66     qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
67
68 throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
69     qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)';
70 throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
71     qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
72
73 throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
74     qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items';
75
76 throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
77     qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
78
79 {
80     my $td = tempdir( CLEANUP => 1 );
81     local %ENV = %ENV;
82     $ENV{GITALIST_REPO_DIR} = $td;
83     lives_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
84 }
85
86 sub test_with_config {
87     my ($config, $msg) = @_;
88     my $ctx = $ctx_gen->();
89         
90     my $m;
91     lives_ok { $m = Gitalist::Model::GitRepos->COMPONENT($ctx, $config) } $msg;
92     ok $m, 'Has model';
93     my $i = $m->ACCEPT_CONTEXT($ctx);
94     ok $i, 'Has model instance from ACCEPT_CONTEXT';
95     isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
96     is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
97     isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';
98     return $i;
99 }
100
101 done_testing;