Throw a bit more Moose around, and copy config to model so that we can test (and...
Tomas Doran [Sun, 27 Sep 2009 12:55:41 +0000 (13:55 +0100)]
Makefile.PL
lib/Gitalist.pm
lib/Gitalist/Model/Git.pm
t/model_Git.t

index 4422b62..ec6233e 100644 (file)
@@ -17,12 +17,14 @@ requires 'Moose';
 requires 'namespace::autoclean';
 requires 'Config::General'; # This should reflect the config file format you've chosen
                  # See Catalyst::Plugin::ConfigLoader for supported formats
+requires 'MooseX::Types::Common';
 catalyst;
 requires 'File::Find::Rule';
 requires 'File::Stat::ModeString';
 requires 'File::Slurp';
 requires 'DateTime::Format::Mail';
 requires 'IO::Capture::Stdout';
+requires 'File::Which';
 
 install_script glob('script/*.pl');
 auto_install;
index 8d3f4f5..0e8c2f3 100644 (file)
@@ -15,13 +15,11 @@ our $VERSION = '0.01';
 # Bring in the libified gitweb.cgi.
 use gitweb;
 
-# Load the testing config if we're invoked under test
-if ($ENV{APP_TEST} ) {
-  __PACKAGE__->config( 'Plugin::ConfigLoader' =>
-                       { file => __PACKAGE__->path_to
-                         ('t/lib/gitalist_testing.conf')}
-                     );
-}
+before 'setup' => sub {
+    my $app = shift;
+    $app->config('Model::Git' => { repo_dir => $app->config('repo_dir') });
+};
+
 __PACKAGE__->config(
        name => 'Gitalist',
        default_view => 'Default',
index 627ce01..260bb26 100644 (file)
@@ -1,6 +1,7 @@
 package Gitalist::Model::Git;
 
 use Moose;
+use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce
 use namespace::autoclean;
 
 BEGIN { extends 'Catalyst::Model' }
@@ -12,34 +13,26 @@ use File::Find::Rule;
 use DateTime::Format::Mail;
 use File::Stat::ModeString;
 use List::MoreUtils qw/any/;
-use Scalar::Util qw/blessed/;
+use File::Which;
 
-# abstraction fail - but currently needed for unit tests to work
-use Gitalist;
+sub BUILD {
+    my ($self) = @_;
+    $self->git; # Cause lazy value build.
+}
 
-{
-       my $git;
-       sub git {
-               return $git
-                       if $git;
-
-               if (my $config_git = Gitalist->config->{git}) {
-                       $git = $config_git if -x $config_git;
-               }
-               else {
-                       require File::Which;
-                       $git = File::Which::which('git');
-               }
-
-               if (!$git) {
-                       die <<EOR
+has git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 );
+
+sub _build_git {
+       my $git = File::Which::which('git');
+
+       if (!$git) {
+               die <<EOR
 Could not find a git executable.
 Please specify the which git executable to use in gitweb.yml
 EOR
-               }
-
-               return $git;
        }
+
+       return $git;
 }
 
 sub is_git_repo {
@@ -88,10 +81,12 @@ sub get_project_properties {
     return %props;
 }
 
+has repo_dir => ( isa => NonEmptySimpleStr, required => 1, is => 'ro' ); # Fixme - path::class
+
 sub list_projects {
     my ($self) = @_;
 
-    my $base = dir(Gitalist->config->{repo_dir});
+    my $base = dir($self->repo_dir);
 
     my @ret;
     my $dh = $base->open;
@@ -119,7 +114,7 @@ sub list_projects {
 sub run_cmd {
     my ($self, @args) = @_;
 
-    open my $fh, '-|', __PACKAGE__->git, @args
+    open my $fh, '-|', $self->git, @args
         or die "failed to run git command";
     binmode $fh, ':encoding(UTF-8)';
 
@@ -145,7 +140,7 @@ sub run_cmd_in {
 sub git_dir_from_project_name {
     my ($self, $project) = @_;
 
-    return dir(Gitalist->config->{repo_dir})->subdir($project);
+    return dir($self->repo_dir)->subdir($project);
 }
 
 sub get_head_hash {
index f629e34..c7c3cb4 100644 (file)
@@ -1,10 +1,12 @@
 use strict;
 use warnings;
+use FindBin qw/$Bin/;
 use Test::More qw/no_plan/;
 
 BEGIN { use_ok 'Gitalist::Model::Git' }
 
-my $Git = Gitalist::Model::Git->new;
+my $c = bless {}, 'Gitalist';
+my $Git = Gitalist::Model::Git->new($c, { repo_dir => "$Bin/lib/repositories" });
 
 # 'bare.git' is a bare git repository in the repository dir
 use Path::Class;
@@ -19,20 +21,11 @@ my $repoWorking = Path::Class::Dir->new('t/lib/repositories/working');
 my $repoEmpty = Path::Class::Dir->new('t/lib/repositories/empty.git');
 ok( ! $Git->is_git_repo( $repoEmpty ), 'is_git_repo is false for empty dir' );
 
-# At present, these tests only work if the APP_TEST env var is set.
-# This is needed to load the test configuration.
-diag("*** SKIPPING app tests.
-*** Set APP_TEST for the tests to run fully") if !$ENV{APP_TEST};
-SKIP: {
-  skip "Set APP_TEST for the tests to run fully",
-    2 if !$ENV{APP_TEST};
-
 my $projectList = $Git->list_projects;
 ok( scalar @{$projectList} == 1, 'list_projects returns an array with the correct number of members' );
 ok( $projectList->[0]->{name} eq 'bare.git', 'list_projects has correct name for "bare.git" repo' );
 #ok( $projectList->[1]->{name} eq 'working/.git', 'list_projects has correct name for "working" repo' );
 
 use Data::Dumper;
-warn( Dumper($projectList) );
+#warn( Dumper($projectList) );
 
-} # Close APP_TEST skip