Newer Older
99 lines | 2.259kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojolicious::Command::test;
2
use Mojo::Base 'Mojolicious::Command';
3

            
4
use Cwd 'realpath';
5
use FindBin;
6
use File::Spec::Functions qw(abs2rel catdir splitdir);
7
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
8
use Mojo::Home;
9

            
10
has description => "Run unit tests.\n";
11
has usage       => <<EOF;
12
usage: $0 test [OPTIONS] [TESTS]
13

            
14
These options are available:
15
  -v, --verbose   Print verbose debug information to STDERR.
16
EOF
17

            
18
sub run {
19
  my ($self, @args) = @_;
20

            
21
  GetOptionsFromArray \@args, 'v|verbose' => sub { $ENV{HARNESS_VERBOSE} = 1 };
22

            
23
  unless (@args) {
24
    my @base = splitdir(abs2rel $FindBin::Bin);
25

            
26
    # "./t"
27
    my $path = catdir @base, 't';
28

            
29
    # "../t"
30
    $path = catdir @base, '..', 't' unless -d $path;
31
    die "Can't find test directory.\n" unless -d $path;
32

            
33
    my $home = Mojo::Home->new($path);
34
    /\.t$/ and push @args, $home->rel_file($_) for @{$home->list_files};
35
    say "Running tests from '", realpath($path), "'.";
36
  }
37

            
38
  $ENV{HARNESS_OPTIONS} //= 'c';
39
  require Test::Harness;
40
  Test::Harness::runtests(sort @args);
41
}
42

            
43
1;
44

            
45
=encoding utf8
46

            
47
=head1 NAME
48

            
49
Mojolicious::Command::test - Test command
50

            
51
=head1 SYNOPSIS
52

            
53
  use Mojolicious::Command::test;
54

            
55
  my $test = Mojolicious::Command::test->new;
56
  $test->run(@ARGV);
57

            
58
=head1 DESCRIPTION
59

            
60
L<Mojolicious::Command::test> runs application tests from the C<t> directory.
61

            
62
This is a core command, that means it is always enabled and its code a good
63
example for learning to build new commands, you're welcome to fork it.
64

            
65
=head1 ATTRIBUTES
66

            
67
L<Mojolicious::Command::test> inherits all attributes from
68
L<Mojolicious::Command> and implements the following new ones.
69

            
70
=head2 description
71

            
72
  my $description = $test->description;
73
  $test           = $test->description('Foo!');
74

            
75
Short description of this command, used for the command list.
76

            
77
=head2 usage
78

            
79
  my $usage = $test->usage;
80
  $test     = $test->usage('Foo!');
81

            
82
Usage information for this command, used for the help screen.
83

            
84
=head1 METHODS
85

            
86
L<Mojolicious::Command::test> inherits all methods from
87
L<Mojolicious::Command> and implements the following new ones.
88

            
89
=head2 run
90

            
91
  $test->run(@ARGV);
92

            
93
Run this command.
94

            
95
=head1 SEE ALSO
96

            
97
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
98

            
99
=cut