10 use Catalyst::Request;
11 use Catalyst::Response;
13 use Gitalist::Model::GitRepos;
14 use File::Temp qw/tempdir/;
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
22 my $stash = $orig->($c);
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};
33 my ($cb, $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?
40 $ctx->response->_context($ctx);
41 $ctx->request->_context($ctx);
47 delete $ENV{GITALIST_REPO_DIR};
49 throws_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), {}) }
50 qr/Cannot find repository dir/, 'Blows up nicely with no repos dir';
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';
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)');
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';
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))';
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';
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';
80 my $td = tempdir( CLEANUP => 1 );
82 $ENV{GITALIST_REPO_DIR} = $td;
83 lives_ok { Gitalist::Model::GitRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
86 sub test_with_config {
87 my ($config, $msg) = @_;
88 my $ctx = $ctx_gen->();
91 lives_ok { $m = Gitalist::Model::GitRepos->COMPONENT($ctx, $config) } $msg;
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';