1 |
package Frey::Test::Runner; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
with 'Frey::Storage'; |
7 |
|
8 |
use TAP::Harness; |
9 |
use TAP::Formatter::HTML; |
10 |
use Data::Dump qw/dump/; |
11 |
|
12 |
use Frey::SVK; |
13 |
use Frey::PPI; |
14 |
|
15 |
has tests => ( |
16 |
is => 'rw', |
17 |
isa => 'ArrayRef[Str]', |
18 |
required => 1, |
19 |
lazy => 1, # FIXME ask users which tests to run |
20 |
default => sub { |
21 |
# [ glob('t/*.t') ] # all tests |
22 |
[ Frey::SVK->modified ] |
23 |
}, |
24 |
documentation => 'run tests which are result of modifications or whole full tests', |
25 |
); |
26 |
|
27 |
has test => ( |
28 |
is => 'rw', |
29 |
isa => 'Str', |
30 |
documentation => 'run only this single test', |
31 |
); |
32 |
|
33 |
has depends => ( |
34 |
is => 'rw', |
35 |
# isa => 'HashRef[Hashref[Int]', |
36 |
required => 1, |
37 |
lazy => 1, |
38 |
default => sub { |
39 |
my $self = shift; |
40 |
my $depends; |
41 |
|
42 |
# collect real tests |
43 |
map { |
44 |
$depends->{$_}->{'test modified'}++ if m{\.t$}; |
45 |
} @{ $self->tests }; |
46 |
|
47 |
# and tests which depend on modified classes supplied |
48 |
map { |
49 |
if ( m{(.+)\.pm$} ) { |
50 |
my $class = $1; |
51 |
$class =~ s{^lib/}{}; |
52 |
$class =~ s{/}{::}g; |
53 |
warn "extract tests from $_ class $class"; |
54 |
$depends->{$_}->{$class}++ foreach Frey::PPI->new( class => $class )->has_tests; |
55 |
} |
56 |
} @{ $self->tests }; |
57 |
|
58 |
return $depends; |
59 |
}, |
60 |
); |
61 |
|
62 |
our $running; |
63 |
|
64 |
sub as_markup { |
65 |
my ($self) = @_; |
66 |
|
67 |
return 'allready running' if $running; |
68 |
$running = 1; |
69 |
|
70 |
my $f = TAP::Formatter::HTML->new({ |
71 |
# silent => 1, |
72 |
|
73 |
inline_css => 1, |
74 |
inline_js => 0, |
75 |
}); |
76 |
my $h = TAP::Harness->new({ |
77 |
merge => 1, |
78 |
formatter => $f, |
79 |
}); |
80 |
|
81 |
my @tests; |
82 |
|
83 |
@tests = ( $self->test ) if $self->test; |
84 |
|
85 |
if ( my $depends = $self->depends ) { |
86 |
@tests = grep { |
87 |
$_ ne '' && |
88 |
! m{$0} # break recursion |
89 |
} sort keys %{ $depends } unless @tests; |
90 |
} |
91 |
|
92 |
$self->add_status( { test => { depends => $self->depends } } ); |
93 |
|
94 |
if ( ! @tests ) { |
95 |
warn "can't find any tests ", dump( $self->tests ), " within depends ", dump( $self->depends ); |
96 |
warn "running all tests instead"; |
97 |
@tests = glob('t/*.t'); |
98 |
} |
99 |
|
100 |
$self->title( join(' ', @tests ) ); |
101 |
|
102 |
warn "testing ",dump( @tests ); |
103 |
$h->runtests( @tests ); |
104 |
|
105 |
$self->store( 'var/test/' . time() . '.yaml', $h ); |
106 |
|
107 |
my $html = ${ $f->html }; |
108 |
# warn $html; |
109 |
warn "got ",length($html), " bytes"; |
110 |
|
111 |
while ( $html =~ s{(<style.+?/style>)}{}gs ) { |
112 |
my $style = $1; |
113 |
$style =~ s[((?:body|html)\s+{[^}]+})][/\* $1 \*/]sg; # remove some styles |
114 |
$self->add_head( $style ); |
115 |
} |
116 |
|
117 |
$self->add_head(qq| |
118 |
<style type="text/css"> |
119 |
/* CSS to show-hide full text results */ |
120 |
ul.test-out { display: none; } |
121 |
td.results:hover ul.test-out { display: block; } |
122 |
</style> |
123 |
|); |
124 |
$html =~ s{<div id="menu">.+?</div>}{}sg; # remove menu which doesn't work without JavaScript |
125 |
|
126 |
$html =~ s{^.*<body>}{}s; |
127 |
$html =~ s{</body>.*$}{}s; |
128 |
|
129 |
$html =~ s{(<a class="file") href="#"(.+?)>t/(.+?)</a>}{<a target="editor" href="/editor+t/$3.t+1" name="t/$3.t" $2>$3</a>}sg; |
130 |
|
131 |
$html = $self->editor_links( $html ); |
132 |
|
133 |
if ( my $depends = $self->depends ) { |
134 |
$html .= qq|Test dependencies:| |
135 |
. qq|<ul><li>| |
136 |
. join("</li>\n<li>", |
137 |
map { |
138 |
qq|<a href="?test=$_"><tt>$_</tt></a> ← | |
139 |
. |
140 |
join(' ', |
141 |
map { |
142 |
if ( m{\s} ) { |
143 |
$_ # human comment with space |
144 |
} else { |
145 |
qq|<a target="introspect" href="/$_" title="introspect">$_</a>| |
146 |
# qq|<a target="editor" href="/editor+$_+1" title="edit">$_</a>| |
147 |
} |
148 |
} keys %{ $self->depends->{$_} } |
149 |
) |
150 |
} @tests ) |
151 |
. qq|</li></ul>| |
152 |
; |
153 |
} |
154 |
|
155 |
$self->add_icon( $1 ) if $html =~ m{class="(passed|failed)"}; |
156 |
|
157 |
$running = 0; |
158 |
return $html; |
159 |
} |
160 |
|
161 |
1; |