X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fmodel_collectionofrepos.t;h=5b0c601d8fde1b5d73398d38baf7eec247b3c31f;hb=f4184925554569404d131ff1b7a45794e51f505a;hp=e847c33af7574f53bd6446ae7f58e2223fd95077;hpb=7bf1a6f5b5a0f73770455e4acbd88982451fde5c;p=catagits%2FGitalist.git diff --git a/t/model_collectionofrepos.t b/t/model_collectionofrepos.t index e847c33..5b0c601 100644 --- a/t/model_collectionofrepos.t +++ b/t/model_collectionofrepos.t @@ -1,9 +1,17 @@ +use FindBin qw/$Bin/; +BEGIN { + my $env = "$FindBin::Bin/../script/env"; + if (-r $env) { + do $env or die $@; + } +} + use strict; use warnings; use Test::More; use Test::Exception; -use FindBin; +use lib "$Bin/lib"; # Used for testing of --model-class etc use Moose (); use Moose::Object; @@ -13,10 +21,13 @@ use Catalyst::Request; use Catalyst::Response; use Catalyst::Utils; use Gitalist::Model::CollectionOfRepos; -use File::Temp qw/tempdir/; +use File::Temp qw/tempdir tempfile/; +my $run_options = {}; my $mock_ctx_meta = Class::MOP::Class->create_anon_class( superclasses => ['Moose::Object'] ); +$mock_ctx_meta->add_method('run_options' => sub { $run_options }); $mock_ctx_meta->add_attribute($_, accessor => $_, required => 1) for qw/request response/; +$mock_ctx_meta->add_method('debug' => sub {}); $mock_ctx_meta->add_attribute('stash', accessor => 'stash', required => 1, default => sub { {} }); $mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight from Catalyst.pm my $orig = shift; @@ -31,13 +42,19 @@ $mock_ctx_meta->add_around_method_modifier( stash => sub { # Nicked straight fro } return $stash; }); +my $mock_log = Moose::Meta::Class->create_anon_class( superclasses => ['Moose::Object'] ); +$mock_log->add_method($_ => sub {}) for qw/ warn info debug /; +my $logger = $mock_log->name->new; +$mock_ctx_meta->add_method('log' => sub { $logger }); + +my $host = "example.gitalist.com"; our $ctx_gen = sub { - my ($cb, $stash) = @_; - $stash ||= {}; + my ($cb, %args) = @_; my $ctx = $mock_ctx_meta->new_object( - response => Catalyst::Response->new, - request => Catalyst::Request->new, - stash => { %$stash }, # Shallow copy to try and help the user out. Should we clone? + response => Catalyst::Response->new, + request => Catalyst::Request->new(uri => URI->new("http://$host/")), + stash => {}, + %args ); $ctx->response->_context($ctx); $ctx->request->_context($ctx); @@ -48,19 +65,19 @@ our $ctx_gen = sub { local %ENV = %ENV; delete $ENV{GITALIST_REPO_DIR}; -throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}) } - qr/Cannot find repository dir/, 'Blows up nicely with no repos dir'; +throws_ok { my $i = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), {}); $i->{_application} = $mock_ctx_meta->name; } + qr/Don't know where to get repositores from/, 'Blows up nicely with no repos dir'; throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repo_dir => '/does/not/exist' }) } - qr|Cannot find repository dir: "/does/not/exist"|, 'Blows up nicely with repos dir does not exist'; + qr|No such file or directory|, 'Blows up nicely with repos dir does not exist'; { my $td = tempdir( CLEANUP => 1 ); - test_with_config({ repo_dir => $td }, 'repo_dir is tempdir'); + test_with_config({ repo_dir => $td }, msg => 'repo_dir is tempdir'); # NOTE - This is cheating, there isn't a real git repository here, so things will explode (hopefully) # if we go much further.. - test_with_config({ repos => $td }, 'repos is tempdir (scalar)'); - test_with_config({ repos => [$td] }, 'repos is tempdir (array)'); + test_with_config({ repos => $td }, msg => 'repos is tempdir (scalar)'); + test_with_config({ repos => [$td] }, msg => 'repos is tempdir (array)'); } # Note - we treat an empty list of repos as if it doesn't exist at all. @@ -68,15 +85,15 @@ throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos qr/Cannot find repository dir/, 'Blows up nicely with no repos list'; throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist' ] } ) } - qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (array)'; + qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (array)'; throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => '/does/not/exist' } ) } - qr/Cannot find repository directories/, 'Blows up nicely with repos list - 1 unknown item (scalar))'; + qr/No such file or directory/, 'Blows up nicely with repos list - 1 unknown item (scalar))'; throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ '/does/not/exist', '/also/does/not/exist' ] } ) } - qr/Cannot find repository directories/, 'Blows up nicely with repos list - 2 unknown items'; + qr/No such file or directory/, 'Blows up nicely with repos list - 2 unknown items'; throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos => [ tempdir( CLEANUP => 1), '/also/does/not/exist' ] } ) } - qr|Cannot find repository directories.*/also/does/not/exist|, 'Blows up nicely with repos list - 1 known, 1 unknown items'; + qr|No such file or directory|, 'Blows up nicely with repos list - 1 known, 1 unknown items'; { my $td = tempdir( CLEANUP => 1 ); @@ -88,6 +105,21 @@ throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos { my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories"}); is scalar($i->repositories->flatten), 3, 'Found 3 repos'; + isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory'; +} + +{ + my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", search_recursively => 1 }); + is scalar($i->repositories->flatten), 7, 'Found 7 repos recursively using config'; + isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive'; +} + { + my($tempfh, $wl) = tempfile(UNLINK => 1); + print {$tempfh} "repo1"; + close $tempfh; + my $i = test_with_config({ repo_dir => "$FindBin::Bin/lib/repositories", whitelist => $wl }); + is scalar($i->repositories->flatten), 1, 'Found 1 repos using whitelist'; + isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromDirectory::WhiteList'; } { @@ -97,12 +129,84 @@ throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos "$FindBin::Bin/lib/repositories/nodescription", ]}); is scalar($i->repositories->flatten), 3, 'Found 3 repos'; + isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories'; +} + +{ + my $i = test_with_config({ + repo_dir => "$FindBin::Bin/lib/repositories", + class => 'TestModelSimple' + }); + is scalar($i->repositories->flatten), 3, 'Found 3 repos'; + isa_ok $i, 'TestModelSimple'; +} + +{ + my $i = test_with_config({ + repo_dir => "$FindBin::Bin/lib/repositories", + class => 'TestModelFancy', + args => { fanciness => 1 }, + }); + is scalar($i->repositories->flatten), 1, 'Found 1 repo'; + isa_ok $i, 'TestModelFancy'; + ok $i->fanciness, "The TestModelFancy is fancy (so --model-args worked)"; +} + +sub test_vhost_instance { + test_with_config({ + class => 'Gitalist::Git::CollectionOfRepositories::Vhost', + args => { + vhost_dispatch => { + "git.shadowcat.co.uk" => "default", + "git.moose.perl.org" => "moose", + "git.catalyst.perl.org" => "catgit", + "_default_" => "default", + }, + collections => { + moose => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectory', repo_dir => "$FindBin::Bin/lib/repositories_sets/moose" }, + catgit => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectory', repo_dir => "$FindBin::Bin/lib/repositories_sets/catgit" }, + default => { class => 'Gitalist::Git::CollectionOfRepositories::FromDirectoryRecursive', repo_dir => "$FindBin::Bin/lib/repositories_sets"}, + } + }, + }); +} + +my $c_name = "$FindBin::Bin/lib/repositories_sets/catgit/Catalyst-Runtime/.git"; +my $m_name = "$FindBin::Bin/lib/repositories_sets/moose/Moose/.git"; +{ + my $i = test_vhost_instance(); + is scalar($i->repositories->flatten), 2, 'Found 2 repos on test vhost'; + my @r = $i->repositories->flatten; + my @paths = sort map { $_->path . "" } $i->repositories->flatten; + is_deeply \@paths, [sort $c_name, $m_name]; +} + +{ + $host = "git.moose.perl.org"; + my $i = test_vhost_instance(); + is scalar($i->repositories->flatten), 1, 'Found 1 repos on moose vhost'; + is $i->repositories->[0]->path.'', $m_name; +} + +{ + $host = "git.catalyst.perl.org"; + my $i = test_vhost_instance(); + is scalar($i->repositories->flatten), 1, 'Found 1 repos on catalyst vhost'; + is $i->repositories->[0]->path.'', $c_name; +} + +{ + $host = "git.shadowcat.co.uk"; + my $i = test_vhost_instance(); + is scalar($i->repositories->flatten), 2, 'Found 2 repos on git.shadowcat vhost'; + my @paths = sort map { $_->path . "" } $i->repositories->flatten; + is_deeply \@paths, [sort $c_name, $m_name]; } sub test_with_config { - my ($config, $msg) = @_; - my $ctx = $ctx_gen->(); - + my ($config, %opts) = @_; + my $msg = delete $opts{msg} || 'Built Model without exception'; + my $ctx = $ctx_gen->(undef, %opts); my $m; lives_ok { $m = Gitalist::Model::CollectionOfRepos->COMPONENT($ctx, $config) } $msg; ok $m, 'Has model';