Add testing for the catalyst model behavior.
[catagits/Gitalist.git] / t / model_gitrepos.t
CommitLineData
329ac3b2 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6
7use Moose ();
8use Moose::Object;
9use Class::MOP::Class;
10use Catalyst::Request;
11use Catalyst::Response;
12use Catalyst::Utils;
13use Gitalist::Model::GitRepos;
14use File::Temp qw/tempdir/;
15
16my $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});
32our $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
46local %ENV = %ENV;
47delete $ENV{GITALIST_REPO_DIR};
48
49throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), {}) }
50 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
51
52throws_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.
65throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
66 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
67
68throws_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)';
70throws_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
73throws_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
76throws_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
86sub 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
101done_testing;