Cope with changes in Catalyst 5.90007 in t/model_collectionofrepos.t.
[catagits/Gitalist.git] / t / model_collectionofrepos.t
index bc0c71f..63589a7 100644 (file)
@@ -23,8 +23,11 @@ use Catalyst::Utils;
 use Gitalist::Model::CollectionOfRepos;
 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;
@@ -39,29 +42,35 @@ $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 = "git.shadowcat.co.uk";
+$mock_ctx_meta->add_method('uri' => sub { URI->new("http://$host/") });
 our $ctx_gen = sub {
     my ($cb, %args) = @_;
     my $ctx = $mock_ctx_meta->new_object(
         response    => Catalyst::Response->new,
-        request     => Catalyst::Request->new,
+        # Too lazy to mock up Catalyst::Log
+        request     => Catalyst::Request->new(uri => URI->new("http://$host/"), _log => 1),
         stash       => {},
         %args
     );
-    $ctx->response->_context($ctx);
-    $ctx->request->_context($ctx);
     $cb->($ctx) if $cb;
     return $ctx;
 };
 
 local %ENV = %ENV;
+delete $ENV{GITALIST_CONFIG};
 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 );
@@ -77,15 +86,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 );
@@ -124,13 +133,6 @@ throws_ok { Gitalist::Model::CollectionOfRepos->COMPONENT($ctx_gen->(), { repos
     isa_ok $i, 'Gitalist::Git::CollectionOfRepositories::FromListOfDirectories';
 }
 
-throws_ok {
-    test_with_config({
-        repo_dir  => "$FindBin::Bin/lib/repositories",
-        class     => 'ThisIsMadeOfLies',
-    });
-} qr/Can't locate ThisIsMadeOfLies/, "Died trying to load a non-existent class";
-
 {
     my $i = test_with_config({
         repo_dir => "$FindBin::Bin/lib/repositories",
@@ -151,6 +153,57 @@ throws_ok {
     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";
+my $m_name = "$FindBin::Bin/lib/repositories_sets/moose/Moose";
+{
+    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, %opts) = @_;
     my $msg = delete $opts{msg} || 'Built Model without exception';
@@ -160,9 +213,6 @@ sub test_with_config {
     ok $m, 'Has model';
     my $i = $m->ACCEPT_CONTEXT($ctx);
     ok $i, 'Has model instance from ACCEPT_CONTEXT';
-    isnt $i, $m, 'Model instance returned from ACCEPT_CONTEXT not same as model';
-    is $i, $m->ACCEPT_CONTEXT($ctx), 'Same model instance for same context';
-    isnt $i, $m->ACCEPT_CONTEXT($ctx_gen->()), 'Different model instance for different context';
     return $i;
 }