3 my $env = "$FindBin::Bin/../script/env";
14 use lib "$Bin/lib"; # Used for testing of --model-class etc
19 use Class::MOP::Class;
20 use Catalyst::Request;
21 use Catalyst::Response;
23 use Gitalist::Model::CollectionOfRepos;
24 use File::Temp qw/tempdir tempfile/;
27 my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] );
28 $mock_ctx_meta->add_method('run_options' => sub { $run_options });
29 $mock_ctx_meta->add_attribute($_, accessor => $_, required => 1) for qw/request response/;
30 $mock_ctx_meta->add_attribute('stash', accessor => 'stash', required => 1, default => sub { {} });
31 $mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight from Catalyst.pm
34 my $stash = $orig->($c);
36 my $new_stash = @_ > 1 ? {@_} : $_[0];
37 croak('stash takes a hash or hashref') unless ref $new_stash;
38 foreach my $key ( keys %$new_stash ) {
39 $stash->{$key} = $new_stash->{$key};
44 my $mock_log = Moose::Meta::Class->create_anon_class( superclasses => ['Moose::Object'] );
45 $mock_log->add_method($_ => sub {}) for qw/ warn info debug /;
46 my $logger = $mock_log->name->new;
47 $mock_ctx_meta->add_method('log' => sub { $logger });
51 my $ctx = $mock_ctx_meta->new_object(
52 response => Catalyst::Response->new,
53 request => Catalyst::Request->new,
57 $ctx->response->_context($ctx);
58 $ctx->request->_context($ctx);
64 delete $ENV{GITALIST_REPO_DIR};
66 throws_ok { my $i = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}); $i->{_application} = $mock_ctx_meta->name; }
67 qr/Don't know where to get repositores from/, 'Blows up nicely with no repos dir';
69 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos_dir => '/does/not/exist' }) }
70 qr|No such file or directory|, 'Blows up nicely with repos dir does not exist';
73 my $td = tempdir( CLEANUP => 1 );
74 test_with_config({ repos_dir => $td }, msg => 'repos_dir is tempdir');
75 # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully)
76 # if we go much further..
77 test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)');
78 test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)');
81 # Note - we treat an empty list of repos as if it doesn't exist at all.
82 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [] } ) }
83 qr/Cannot find repository dir/, 'Blows up nicely with no repos list';
85 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) }
86 qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (array)';
87 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) }
88 qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (scalar))';
90 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) }
91 qr/No such file or directory/, 'Blows up nicely with repos list - 2 unknown items';
93 throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) }
94 qr|No such file or directory|, 'Blows up nicely with repos list - 1 known, 1 unknown items';
97 my $td = tempdir( CLEANUP => 1 );
99 $ENV{GITALIST_REPO_DIR} = $td;
100 lives_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } 'GITALIST_REPO_DIR env variable works';
104 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories"});
105 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
106 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory';
110 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 });
111 is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config';
112 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive';
115 my($tempfh, $wl) = tempfile(UNLINK => 1);
116 print {$tempfh} "repo1";
118 my $i = test_with_config({ repos_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl });
119 is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist';
120 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList';
124 my $i = test_with_config({ repos => [
125 "$FindBin::Bin/lib/repositories/bare.git",
126 "$FindBin::Bin/lib/repositories/repo1",
127 "$FindBin::Bin/lib/repositories/nodescription",
129 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
130 isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
134 my $i = test_with_config({
135 repos_dir => "$FindBin::Bin/lib/repositories",
136 class => 'TestModelSimple'
138 is scalar($i->repositories->flatten), 3, 'Found 3 repos';
139 isa_ok $i, 'TestModelSimple';
143 my $i = test_with_config({
144 repos_dir => "$FindBin::Bin/lib/repositories",
145 class => 'TestModelFancy',
146 args => { fanciness => 1 },
148 is scalar($i->repositories->flatten), 1, 'Found 1 repo';
149 isa_ok $i, 'TestModelFancy';
150 ok $i->fanciness, "The TestModelFancy is fancy (so --model-args worked)";
153 sub test_with_config {
154 my ($config, %opts) = @_;
155 my $msg = delete $opts{msg} || 'Built Model without exception';
156 my $ctx = $ctx_gen->(undef, %opts);
158 lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg;
160 my $i = $m->ACCEPT_CONTEXT($ctx);
161 ok $i, 'Has model instance from ACCEPT_CONTEXT';
162 isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
163 is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
164 isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';