Jifty/Test.pm

filter: Statementstmt 38.6% 100 / 259 Branchbran 14.2% 12 / 84 Conditioncond 17.0% 8 / 47 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 55.8% 24 / 43 Podpod 100% 17 / 17 totaltotal 33.6% 161 / 478 CC 63 SCAR 38.3
CC
63
Cov
31%
setup_test_database
132.0
_ending
97.7
setup
69.9
CRAP 46.2
« Jifty.pm ↑ Summary Jifty/API.pm »
1 108 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 108 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Test;
5 108 ▶use base qw/Test::More/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 108 ▶use Jifty::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Email::LocalDelivery;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Email::Folder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Cwd qw/abs_path cwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Mechanize uses Test::LongString to report mismatches. Increase the
18 # limit so we can see where errors come from more easily.
19 1 ▶use Test::LongString;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 $Test::LongString::Max = 128;
21  
22 =head1 NAME
23  
24 Jifty::Test - Jifty's test module
25  
26 =head1 SYNOPSIS
27  
28 use Jifty::Test tests => 5;
29 # to load po for test:
30 # use Jifty::Test tests => 5, l10n => 1;
31  
32 # ...all of Test::More's functionality...
33 my $model = MyApp::Model::MyModel->new;
34 $model->create();
35 ok($model->id, 'model works');
36 is($model->foo, 'some default', 'default works');
37  
38 # Startup an external server (see Jifty::TestServer)
39 my $server = Jifty::Test->make_server;
40 my $server_url = $server->started_ok;
41 # You're probably also interested in Jifty::Test::WWW::Mechanize
42  
43  
44 =head1 DESCRIPTION
45  
46 Jifty::Test is a superset of L<Test::More>. It provides all of
47 Test::More's functionality in addition to the class methods defined
48 below.
49  
50 =head1 METHODS
51  
52 =head2 is_passing
53  
54 my $is_passing = Jifty::Test->is_passing;
55  
56 Check if the test is currently in a passing state.
57  
58 =over
59  
60 =item *
61  
62 All tests run so far have passed
63  
64 =item *
65  
66 We have run at least one test
67  
68 =item *
69  
70 We have not run more than we planned (if we planned at all)
71  
72 =back
73  
74 =cut
75  
76 1 ▶sub is_passing {
Subroutine is_passing: called CC=3 SCAR=11.8
Pod documented
77 1 my $tb = Jifty::Test->builder;
78  
79 1 my $is_failing = grep {not $_} $tb->summary;
80 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶ $is_failing ||= $tb->has_plan eq 'no_plan'
Branch: 1 branch
BranchTrueFalseTotal
$tb->has_plan eq 'no_plan' ? : 0 0 0
Condition33%1/3
$is_failing ||= $tb->has_plan eq "no_plan" ? 0 : $tb->expected_tests < $tb->current_test
l!l&&r!l&&!r
100
MC/DC0%0/2
$is_failing ||= $tb->has_plan eq "no_plan" ? 0 : $tb->expected_tests < $tb->current_test
$is_failing $tb->has_plan eq "no_plan" ? 0 : $tb->expected_tests < $tb->current_test
Truth table33%1/3
A ||= B
A= $is_failingB= $tb->has_plan eq "no_plan" ? 0 : $tb->expected_tests < $tb->current_test
ABresult
1X1
011
000
82 ? 0
83 : $tb->expected_tests < $tb->current_test;
84  
85 1 return !$is_failing;
86 }
87  
88  
89 =head2 is_done
90  
91 my $is_done = Jifty::Test->is_done;
92  
93 Check if we have run all the tests we've planned.
94  
95 If the plan is 'no_plan' then is_done() will return true if at least
96 one test has run.
97  
98 =cut
99  
100 0 ▶sub is_done {
Subroutine is_done: not called CC=2 SCAR=11.6
Pod documented
101 0 my $tb = Jifty::Test->builder;
102  
103 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 0 ▶ if( $tb->has_plan eq 'no_plan' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tb->has_plan eq 'no_plan') { } 0 0 0
105 0 return $tb->current_test > 0;
106 }
107 else {
108 0 return $tb->expected_tests == $tb->current_test;
109 }
110 }
111  
112  
113 =begin private
114  
115 =head2 import_extra
116  
117 Called by L<Test::More>'s C<import> code when L<Jifty::Test> is first
118 C<use>'d, it calls L</setup>, and asks Test::More to export its
119 symbols to the namespace that C<use>'d this one.
120  
121 =end private
122  
123 =cut
124  
125 1 our $imported = 0;
126  
127 1 ▶sub import_extra {
Subroutine import_extra: called CC=4 SCAR=21.3
Pod documented
128 1 my $class = shift;
129 1 my $args = shift;
130  
131 1 $imported = 1;
132  
133 1 $class->setup($args);
134 0 Test::More->export_to_level(2);
135  
136 # Now, clobber Test::Builder::plan (if we were given a plan) so
137 # Test::Builder::Module doesn't plan for us
138 0 ▶ if ($class->builder->has_plan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->builder->has_plan) 0 0 0
139 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
140 0 ▶ *Test::Builder::plan = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 }
142  
143 # the modified $args is then passed to Test::Builder's plan. we should
144 # strip our custom items.
145 # XXX: this should probably be done in _strip_imports
146 # we check for multiple args because of 'no_plan'
147 0 ▶ if (@$args > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args > 1) 0 0 0
148 0 my %args = @$args;
149 0 ▶ @$args = map { $args{$_} ? ($_ => $args{$_ }) : () } qw(tests skip_all);
Branch: 1 branch
BranchTrueFalseTotal
$args{$_} ? : 0 0 0
150 }
151 }
152  
153 =head2 setup ARGS
154  
155 This method is passed a single argument. This is a reference to the array of parameters passed in to the import statement.
156  
157 Merges the L</test_config> into the default configuration, resets the
158 database, and resets the fake "outgoing mail" folder.
159  
160 This is the method to override if you wish to do custom setup work, such as
161 insert test data into your database.
162  
163 package MyApp::Test;
164 use base qw/ Jifty::Test /;
165  
166 sub setup {
167 my $self = shift;
168 my $args = shift;
169  
170 # Make sure to call the super-class version
171 $self->SUPER::setup($args);
172  
173 # Now that we have the database and such...
174 my %test_args = @$args;
175  
176 if ($test_arg{something_special}) {
177 # do something special...
178 }
179 }
180  
181 And later in your tests, you may do the following:
182  
183 use MyApp::Test tests => 14, something_special => 1;
184  
185 # 14 tests with some special setup...
186  
187 =cut
188  
189 1 my $WARNINGS_ARE_FATAL;
190  
191 1 ▶sub setup {
Subroutine setup: called CC=17 SCAR=42.5
Pod documented
192 1 my $class = shift;
193 1 my $args = shift;
194  
195 1 ▶ $args ||= [];
Condition50%1/2
$args ||= []
l!l
10
MC/DC0%0/1
$args ||= []
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
196 0 ▶ my %args = @{$args} % 2 ? (@{$args}, 1) : @{$args};
Branch: 1 branch
BranchTrueFalseTotal
@{$args;} % 2 ? : 0 1 1
197  
198 1 my $root = Jifty::Util->app_root;
199  
200 1 require Jifty::YAML;
201 # naive detect of configfileversion before jifty->new, since you
202 # probably don't want to override it in site_config or other places.
203 1 my $config = eval { Jifty::YAML::LoadFile("$root/etc/config.yml") };
204 1 ▶ if ($config && $config->{framework}{ConfigFileVersion} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($config and $config->{"framework"}{"ConfigFileVersion"} and $config->{"framework"}{"ConfigFileVersion"} < 5) 0 1 1
Condition33%2/6
$config and $config->{"framework"}{"ConfigFileVersion"} and $config->{"framework"}{"ConfigFileVersion"} < 5
!ll&&!rl&&r
100
$config and $config->{"framework"}{"ConfigFileVersion"}
!ll&&!rl&&r
010
MC/DC0%0/3
$config and $config->{"framework"}{"ConfigFileVersion"} and $config->{"framework"}{"ConfigFileVersion"} < 5
$config $config->{"framework"}{"ConfigFileVersion"} $config->{"framework"}{"ConfigFileVersion"} < 5
Truth table25%1/4
A and B and C
A= $configB= $config->{"framework"}{"ConfigFileVersion"}C= $config->{"framework"}{"ConfigFileVersion"} < 5
ABCresult
0XX0
10X0
1100
1111
205 $config->{framework}{ConfigFileVersion} < 5) {
206 0 ▶ $ENV{JIFTY_TEST_SERVER} ||= 'Standalone';
Condition0%0/2
$ENV{"JIFTY_TEST_SERVER"} ||= "Standalone"
l!l
00
MC/DC0%0/1
$ENV{"JIFTY_TEST_SERVER"} ||= "Standalone"
$ENV{"JIFTY_TEST_SERVER"}
Truth table0%0/2
A
A= $ENV{"JIFTY_TEST_SERVER"}
Aresult
11
00
207 }
208  
209 my $server = $ENV{JIFTY_TEST_SERVER} ||=
210 1 ▶ $args{actual_server} ? 'Standalone' : 'Inline';
Branch: 1 branch
BranchTrueFalseTotal
$args{'actual_server'} ? : 0 1 1
Condition33%1/3
$ENV{"JIFTY_TEST_SERVER"} ||= $args{"actual_server"} ? "Standalone" : "Inline"
l!l&&r!l&&!r
010
MC/DC0%0/2
$ENV{"JIFTY_TEST_SERVER"} ||= $args{"actual_server"} ? "Standalone" : "Inline"
$ENV{"JIFTY_TEST_SERVER"} $args{"actual_server"} ? "Standalone" : "Inline"
Truth table33%1/3
A ||= B
A= $ENV{"JIFTY_TEST_SERVER"}B= $args{"actual_server"} ? "Standalone" : "Inline"
ABresult
1X1
011
000
211  
212 1 ▶ if ($server eq 'Inline') {
Branch: 1 branch
BranchTrueFalseTotal
if ($server eq "Inline") 1 0 1
213 1 require Jifty::Test::WWW::Mechanize;
214 1 require Test::WWW::Mechanize::PSGI;
215 1 unshift @Jifty::Test::WWW::Mechanize::ISA, 'Test::WWW::Mechanize::PSGI';
216 }
217  
218 1 ▶ if ($args{actual_server}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"actual_server"}) 0 1 1
219 $class->builder->plan(skip_all => "This test requires an actual test server to run. Run with JIFTY_TEST_SERVER=Standalone instead")
220 0 ▶ if $ENV{JIFTY_TEST_SERVER} eq 'Inline';
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"JIFTY_TEST_SERVER"} eq "Inline" 0 0 0
221 }
222  
223 # Spit out a plan (if we got one) *before* we load modules, in
224 # case of compilation errors
225 1 ▶ unless ($class->builder->has_plan) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->builder->has_plan) 0 1 1
226 0 $class->builder->plan(map { $_ => $args{$_ } } qw(tests skip_all))
227 0 ▶ if $args{tests} || $args{skip_all};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"tests"} or $args{"skip_all"} 0 0 0
Condition0%0/3
$args{"tests"} or $args{"skip_all"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"tests"} or $args{"skip_all"}
$args{"tests"} $args{"skip_all"}
Truth table0%0/3
A or B
A= $args{"tests"}B= $args{"skip_all"}
ABresult
1X1
011
000
228 }
229  
230 # Require the things we need
231 1 require Jifty::Script::Schema;
232  
233 0 $class->builder->{no_handle} = $args{no_handle};
234 0 ▶ $WARNINGS_ARE_FATAL = 1 if $args{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"strict"} 0 0 0
235 0 my $test_config = File::Temp->new( UNLINK => 0 );
236 0 Jifty::YAML::DumpFile("$test_config", $class->test_config(Jifty::Config->new, \%args));
237 # Invoking bin/jifty and friends will now have the test config ready.
238 0 ▶ $ENV{'JIFTY_TEST_CONFIG'} ||= "$test_config";
Condition0%0/3
$ENV{"JIFTY_TEST_CONFIG"} ||= "$test_config"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"JIFTY_TEST_CONFIG"} ||= "$test_config"
$ENV{"JIFTY_TEST_CONFIG"} "$test_config"
Truth table0%0/3
A ||= B
A= $ENV{"JIFTY_TEST_CONFIG"}B= "$test_config"
ABresult
1X1
011
000
239 0 $class->builder->{test_config} = $test_config;
240 {
241 # Cache::Memcached stores things. And doesn't let them expire
242 # from the cache easily. This is fine in production, but
243 # during testing each test script needs its own namespace. we
244 # use the pid of the current process, and save it so the keys
245 # stays the same when we fork
246 0 {
247 0 package Jifty::Record;
248 1 ▶ no warnings qw/redefine/;
Subroutine BEGIN: called CC=1 SCAR=0.0
249  
250 1 ▶ use vars qw/$cache_key_prefix/;
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 0 $cache_key_prefix = "jifty-test-" . $$;
253
254 0 ▶ *Jifty::Record::cache_key_prefix = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 0 $Jifty::Record::cache_key_prefix;
256 }
257 0 }
258
259 }
260 # Mason's disk caching sometimes causes false tests
261 0 rmtree([ File::Spec->canonpath("$root/var/mason") ], 0, 1);
262  
263 0 $class->setup_test_database;
264  
265 0 $class->setup_mailbox;
266 }
267  
268 =head2 setup_test_database
269  
270 Create the test database. This can be overloaded if you do your databases in a
271 different way.
272  
273 =cut
274  
275 0 ▶sub setup_test_database {
Subroutine setup_test_database: not called CC=11 SCAR=48.8
Pod documented
276 0 my $class = shift;
277  
278 0 ▶ if ($class->builder->{no_handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->builder->{"no_handle"}) 0 0 0
279 0 Jifty->new( no_handle => 1 );
280 0 return;
281 }
282  
283 0 ▶ if ($ENV{JIFTY_FAST_TEST}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"JIFTY_FAST_TEST"}) 0 0 0
284 0 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
285 0 eval { Jifty->new( no_version_check => 1 ); Jifty->handle->check_schema_version };
286 0 my $booted;
287 0 ▶ if (Jifty->handle && !$@) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty"->handle and not $@) 0 0 0
Condition0%0/3
"Jifty"->handle and not $@
!ll&&!rl&&r
000
MC/DC0%0/2
"Jifty"->handle and not $@
"Jifty"->handle not $@
Truth table0%0/3
A and not(B)
A= "Jifty"->handleB= not $@
ABresult
0X0
100
111
288 0 my $baseclass = Jifty->app_class;
289 0 for my $model_class ( grep {/^\Q$baseclass\E::Model::/} Jifty::Schema->new->models ) {
290 # We don't want to get the Collections, for example.
291 0 ▶ next unless $model_class->isa('Jifty::DBI::Record');
Branch: 1 branch
BranchTrueFalseTotal
unless $model_class->isa("Jifty::DBI::Record") 0 0 0
292 0 Jifty->handle->simple_query('TRUNCATE '.$model_class->table );
293 0 Jifty->handle->simple_query('ALTER SEQUENCE '.$model_class->table.'_id_seq RESTART 1');
294 }
295 # Load initial data
296 0 eval {
297 0 my $bootstrapper = Jifty->app_class("Bootstrap");
298 0 Jifty::Util->require($bootstrapper);
299 0 ▶ $bootstrapper->run() if $bootstrapper->can('run');
Branch: 1 branch
BranchTrueFalseTotal
if $bootstrapper->can("run") 0 0 0
300 };
301 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
302 0 $booted = 1;
303 }
304 0 ▶ if (Jifty->handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty"->handle) 0 0 0
305 0 Jifty->handle->disconnect;
306 0 Jifty->handle(undef);
307 }
308 0 ▶ if ($booted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($booted) 0 0 0
309 0 Jifty->new();
310 0 return;
311 }
312 }
313  
314 0 Jifty->new( no_handle => 1, pre_init => 1 );
315  
316 0 my $schema = Jifty::Script::Schema->new;
317 0 $schema->{drop_database} = 1;
318 0 $schema->{setup_tables} = 1;
319 0 $schema->run;
320  
321 0 Jifty->new();
322 }
323  
324 =head2 load_test_configs FILENAME
325  
326 This will load all the test config files that apply to FILENAME (default:
327 C<$0>, the current test script file). Say you are running the test script
328 C</home/bob/MyApp/t/user/12-delete.t>. The files that will be loaded are:
329  
330 =over 4
331  
332 =item C</home/bob/MyApp/t/user/12-delete.t-config.yml>
333  
334 =item C</home/bob/MyApp/t/user/test_config.yml>
335  
336 =item C</home/bob/MyApp/t/test_config.yml>
337  
338 =back
339  
340 ..followed by the usual Jifty configuration files (such as
341 C<MyApp/etc/config.yml> and C<MyApp/etc/site_config.yml>). The options in a
342 more specific test file override the options in a less specific test file.
343  
344 The options are returned in a single hashref.
345  
346 =cut
347  
348 0 ▶sub load_test_configs {
Subroutine load_test_configs: not called CC=7 SCAR=40.3
Pod documented
349 0 my $class = shift;
350 0 my ($test_config_file) = @_;
351  
352 # Jifty::Test::Dist uses chdir which screws up $0, so to be nice
353 # it also makes available the cwd was before it uses chdir.
354 0 my $cwd = $Jifty::Test::Dist::OrigCwd;
355  
356 # get the initial test config file, which is the input . "-config.yml"
357 0 ▶ $test_config_file = $0 if !defined($test_config_file);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $test_config_file 0 0 0
358 0 $test_config_file .= "-config.yml";
359 0 $test_config_file = File::Spec->rel2abs($test_config_file, $cwd);
360  
361 0 my $test_options = _read_and_merge_config_file($test_config_file, {});
362  
363 # get the directory of the input, so we can recurse upwards
364 0 my ($volume, $directories) = File::Spec->splitpath($test_config_file);
365 0 my $directory = File::Spec->catdir($volume, $directories);
366  
367 0 ▶ my $depth = $ENV{JIFTY_TEST_DEPTH} || 30;
Condition0%0/2
$ENV{'JIFTY_TEST_DEPTH'} || 30
l!l
00
MC/DC0%0/1
$ENV{'JIFTY_TEST_DEPTH'} || 30
$ENV{'JIFTY_TEST_DEPTH'}
Truth table0%0/2
A
A= $ENV{'JIFTY_TEST_DEPTH'}
Aresult
11
00
368  
369 0 for (1 .. $depth)
370 {
371 0 my $file = File::Spec->catfile($directory, "test_config.yml");
372 0 $test_options = _read_and_merge_config_file($file, $test_options);
373  
374 # are we at the app root? if so, then we can stop moving up
375 # did abs_path return undef? if so, there's not much we can do from here
376 0 $directory = abs_path(File::Spec->catdir($directory, File::Spec->updir($directory)));
377 0 ▶ return $test_options
Branch: 1 branch
BranchTrueFalseTotal
if not defined $directory or "Jifty::Util"->is_app_root($directory) 0 0 0
Condition0%0/3
not defined $directory or "Jifty::Util"->is_app_root($directory)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $directory or "Jifty::Util"->is_app_root($directory)
not defined $directory "Jifty::Util"->is_app_root($directory)
Truth table0%0/3
not(A) or B
A= not defined $directoryB= "Jifty::Util"->is_app_root($directory)
ABresult
1X1
011
000
378 if not defined $directory
379 or Jifty::Util->is_app_root($directory);
380 }
381  
382 0 ▶ Jifty->log->fatal("Stopping looking for test config files after recursing upwards $depth times. Either you have a nonstandard layout or an incredibly deep test hierarchy. If you really do have an incredibly deep test hierarchy, you can set the environment variable JIFTY_TEST_DEPTH to a larger value.") if (Jifty->logger);
Branch: 1 branch
BranchTrueFalseTotal
if "Jifty"->logger 0 0 0
383  
384 0 return $test_options;
385 }
386  
387 0 ▶sub _read_and_merge_config_file {
Subroutine _read_and_merge_config_file: not called CC=1 SCAR=6.9
388 0 my $file = shift;
389 0 my $config = shift;
390  
391 0 my $file_options = Jifty::Config->load_file($file);
392  
393 0 Hash::Merge::set_behavior('RIGHT_PRECEDENT');
394  
395 # merge the new options into what we have so far
396 0 return Hash::Merge::merge($file_options, $config);
397 }
398  
399 =head2 test_config
400  
401 Returns a hash which overrides parts of the application's
402 configuration for testing. By default, this changes the database name
403 by appending a 'test', as well as setting the port to a random port
404 between 10000 and 15000. Individual test configurations may override these
405 defaults (see C<load_test_configs>).
406  
407 It is passed the current configuration before any test config is loaded.
408  
409 You can override this to provide application-specific test
410 configuration, e.g:
411  
412 sub test_config {
413 my $class = shift;
414 my ($config) = @_;
415 my $hash = $class->SUPER::test_config($config);
416 $hash->{framework}{LogConfig} = "etc/log-test.conf"
417
418 return $hash;
419 }
420  
421 Note that this is deprecated in favor of having real config files in your
422 test directory.
423  
424 =cut
425  
426 0 ▶sub test_config {
Subroutine test_config: not called CC=4 SCAR=30.0
Pod documented
427 0 my $class = shift;
428 0 my ($config, $args) = @_;
429  
430 my $defaults = {
431 framework => {
432 Database => {
433 Database => $config->framework('Database')->{Database} . $class->_testfile_to_dbname(),
434 },
435 L10N => {
436 Disable => $args->{l10n} ? 0 : 1,
437 },
438 Web => {
439 Port => ($$ % 5000) + 10000,
440 DataDir => File::Temp::tempdir('masonXXXXXXXXXX', CLEANUP => 1)
441 },
442 Plugins => [
443 { TestServerWarnings => {} },
444 ],
445 Mailer => 'Jifty::Test',
446 MailerArgs => [],
447 0 ▶ LogLevel => $ENV{JIFTY_TEST_LOGLEVEL} || 'FATAL',
Branch: 1 branch
BranchTrueFalseTotal
$args->{'l10n'} ? : 0 0 0
Condition0%0/2
$ENV{'JIFTY_TEST_LOGLEVEL'} || 'FATAL'
l!l
00
MC/DC0%0/1
$ENV{'JIFTY_TEST_LOGLEVEL'} || 'FATAL'
$ENV{'JIFTY_TEST_LOGLEVEL'}
Truth table0%0/2
A
A= $ENV{'JIFTY_TEST_LOGLEVEL'}
Aresult
11
00
448 TestMode => 1,
449 }
450 };
451  
452 0 ▶ if ($INC{'Devel/Cover.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Devel/Cover.pm"}) 0 0 0
453 0 $defaults->{framework}{DevelMode} = 0;
454 0 $defaults->{framework}{Web}{MasonConfig}{named_component_subs} = 1;
455 0 $defaults->{framework}{Web}{DataDir} = Jifty::Util->absolute_path( 'var/mason-cover' );
456 }
457  
458 0 Hash::Merge::set_behavior('RIGHT_PRECEDENT');
459 0 return Hash::Merge::merge($defaults, $class->load_test_configs);
460 }
461  
462  
463 0 ▶sub _testfile_to_dbname {
Subroutine _testfile_to_dbname: not called CC=2 SCAR=17.9
464 0 ▶ return 'fasttest' if $ENV{JIFTY_FAST_TEST};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"JIFTY_FAST_TEST"} 0 0 0
465 0 my $dbname = lc($0);
466 0 $dbname =~ s/\.t$//;
467 0 $dbname =~ s/(\W|[_-])+//g;
468 0 $dbname .= substr(md5_hex(cwd()), 0, 8);
469 0 $dbname = substr($dbname,-32,32);
470 0 return $dbname;
471 }
472  
473 =head2 make_server
474  
475 Creates a new L<Jifty::TestServer> depending on the value of
476 C<$ENV{JIFTY_TEST_SERVER}>. If the environment variable is C<Inline>,
477 we run tests using PSGI inline without spawning an actual server.
478 Otherwise, we fork off a L<Plack::Server> to run tests against.
479  
480 =cut
481  
482 0 ▶sub make_server {
Subroutine make_server: not called CC=3 SCAR=18.7
Pod documented
483 0 my $class = shift;
484 1 ▶ use Jifty::TestServer;
Subroutine BEGIN: called CC=1 SCAR=0.0
485  
486 0 ▶ my $server_class = $ENV{JIFTY_TEST_SERVER} eq 'Inline'
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'JIFTY_TEST_SERVER'} eq 'Inline' ? : 0 0 0
487 ? 'Jifty::TestServer::Inline' : 'Jifty::TestServer';
488 0 ▶ Jifty::Util->require($server_class) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
"Jifty::Util"->require($server_class) or die $! 0 0 0
489  
490 0 $Jifty::SERVER = $server_class->new;
491 }
492  
493 =head2 web
494  
495 Like calling C<<Jifty->web>>.
496  
497 C<<Jifty::Test->web>> does the necessary Jifty->web initialization for
498 it to be usable in a test.
499  
500 =cut
501  
502 0 ▶sub web {
Subroutine web: not called CC=3 SCAR=24.8
Pod documented
503 0 my $class = shift;
504  
505 0 ▶ Jifty->web->request(Jifty::Request->new) unless Jifty->web->request;
Branch: 1 branch
BranchTrueFalseTotal
unless "Jifty"->web->request 0 0 0
506 0 ▶ Jifty->web->response(Jifty::Response->new) unless Jifty->web->response;
Branch: 1 branch
BranchTrueFalseTotal
unless "Jifty"->web->response 0 0 0
507  
508 0 return Jifty->web;
509 }
510  
511  
512 =head2 mailbox
513  
514 A mailbox used for testing mail sending.
515  
516 =cut
517  
518 0 ▶sub mailbox {
Subroutine mailbox: not called CC=1 SCAR=6.9
Pod documented
519 0 return Jifty::Util->absolute_path("t/mailbox_" . _testfile_to_dbname());
520 }
521  
522 =head2 setup_mailbox
523  
524 Clears the mailbox.
525  
526 =cut
527  
528 0 ▶sub setup_mailbox {
Subroutine setup_mailbox: not called CC=1 SCAR=6.9
Pod documented
529 0 my $class = shift;
530  
531 0 open my $f, ">:encoding(UTF-8)", $class->mailbox;
532 0 close $f;
533 }
534  
535 =head2 teardown_mailbox
536  
537 Deletes the mailbox.
538  
539 =cut
540  
541 0 ▶sub teardown_mailbox {
Subroutine teardown_mailbox: not called CC=1 SCAR=6.9
Pod documented
542 0 unlink mailbox();
543 }
544  
545 =head2 is_available
546  
547 Informs L<Email::Send> that L<Jifty::Test> is always available as a mailer.
548  
549 =cut
550  
551 0 ▶sub is_available { 1 }
Subroutine is_available: not called CC=1 SCAR=6.9
Pod documented
552  
553 =head2 send
554  
555 Should not be called manually, but is
556 automatically called by L<Email::Send> when using L<Jifty::Test> as a mailer.
557  
558 (Note that it is a class method.)
559  
560 =cut
561  
562 0 ▶sub send {
Subroutine send: not called CC=1 SCAR=6.9
Pod documented
563 0 my $class = shift;
564 0 my $message = shift;
565  
566 0 Email::LocalDelivery->deliver($message->as_string, mailbox());
567 }
568  
569 =head2 messages
570  
571 Returns the messages in the test mailbox, as a list of
572 L<Email::Simple> objects. You may have to use a module like
573 L<Email::MIME> to parse multi-part messages stored in the mailbox.
574  
575 =cut
576  
577 0 ▶sub messages {
Subroutine messages: not called CC=2 SCAR=17.9
Pod documented
578 0 ▶ return () unless -f mailbox();
Branch: 1 branch
BranchTrueFalseTotal
unless -f mailbox() 0 0 0
579 0 return Email::Folder->new(mailbox())->messages;
580 }
581  
582  
583 =head2 test_file
584  
585 my $files = Jifty::Test->test_file($file);
586  
587 Register $file as having been created by the test. It will be
588 cleaned up at the end of the test run I<if and only if> the test
589 passes. Otherwise it will be left alone.
590  
591 It returns $file so you can do this:
592  
593 my $file = Jifty::Test->test_file( Jifty::Util->absolute_path("t/foo") );
594  
595 =cut
596  
597 my @Test_Files_To_Cleanup;
598 0 ▶sub test_file {
Subroutine test_file: not called CC=1 SCAR=6.9
Pod documented
599 0 my $class = shift;
600 0 my $file = shift;
601  
602 0 push @Test_Files_To_Cleanup, $file;
603  
604 0 return $file;
605 }
606  
607  
608 =head2 test_in_isolation
609  
610 my $return = Jifty::Test->test_in_isolation( sub {
611 ...your testing code...
612 });
613  
614 For testing testing modules so you can run testing code (which perhaps
615 fail) without effecting the outer test.
616  
617 Saves the state of Jifty::Test's Test::Builder object and redirects
618 all output to dev null before running your testing code. It then
619 restores the Test::Builder object back to its original state.
620  
621 # Test that fail() returns 0
622 ok !Jifty::Test->test_in_isolation sub {
623 return fail;
624 };
625  
626 =cut
627  
628 0 ▶sub test_in_isolation {
Subroutine test_in_isolation: not called CC=1 SCAR=6.9
Pod documented
629 0 my $class = shift;
630 0 my $code = shift;
631  
632 0 my $tb = Jifty::Test->builder;
633  
634 0 my $output = $tb->output;
635 0 my $failure_output = $tb->failure_output;
636 0 my $todo_output = $tb->todo_output;
637 0 my $current_test = $tb->current_test;
638  
639 0 $tb->output( File::Spec->devnull );
640 0 $tb->failure_output( File::Spec->devnull );
641 0 $tb->todo_output( File::Spec->devnull );
642  
643 0 my $result = $code->();
644  
645 0 $tb->output($output);
646 0 $tb->failure_output($failure_output);
647 0 $tb->todo_output($todo_output);
648 0 $tb->current_test($current_test);
649  
650 0 return $result;
651 }
652  
653 # Stick the END block in a method so we can test it.
654 1 ▶END { Jifty::Test->_ending }
Subroutine END: called CC=1 SCAR=0.0
655  
656 1 ▶sub _ending {
Subroutine _ending: called CC=16 SCAR=45.8
657 # only run the teardown code if we were responsible for setup
658 1 ▶ return unless $imported;
Branch: 1 branch
BranchTrueFalseTotal
unless $Jifty::Test::imported 0 1 1
659  
660 1 my $Test = Jifty::Test->builder;
661  
662 # Such a hack -- try to detect if this is a forked child process and don't
663 # do cleanup in that case.
664 # TODO: note that this check fails if you're forking off multiple
665 # children that all do similar things, say running RT and Jifty tests
666 # in the same process
667 # XXX TODO - This makes assumptions about Test::Builder internals
668 1 ▶ return if $Test->{Original_Pid} != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $Test->{"Original_Pid"} != $$ 0 1 1
669  
670 1 my $should_die = 0;
671 1 ▶ if ($Jifty::SERVER &&
Branch: 1 branch
BranchTrueFalseTotal
if ($Jifty::SERVER and my $plugin = "Jifty"->find_plugin("Jifty::Plugin::TestServerWarnings") and grep {$_ eq "Jifty::View::Declare::Handler";} "Jifty"->handler->view_handlers) 0 1 1
Condition33%2/6
$Jifty::SERVER and my $plugin = "Jifty"->find_plugin("Jifty::Plugin::TestServerWarnings") and grep {$_ eq "Jifty::View::Declare::Handler";} "Jifty"->handler->view_handlers
!ll&&!rl&&r
100
$Jifty::SERVER and my $plugin = "Jifty"->find_plugin("Jifty::Plugin::TestServerWarnings")
!ll&&!rl&&r
100
MC/DC0%0/3
$Jifty::SERVER and my $plugin = "Jifty"->find_plugin("Jifty::Plugin::TestServerWarnings") and grep {$_ eq "Jifty::View::Declare::Handler";} "Jifty"->handler->view_handlers
$Jifty::SERVER my $plugin = "Jifty"->find_plugin("Jifty::Plugin::TestServerWarnings") grep {$_ eq "Jifty::View::Declare::Handler";} "Jifty"->handler->view_handlers
Truth table25%1/4
A and B and C
A= $Jifty::SERVERB= my $plugin = "Jifty"->find_plugin("Jifty::Plugin::TestServerWarnings")C= grep {$_ eq "Jifty::View::Declare::Handler";} "Jifty"->handler->view_handlers
ABCresult
0XX0
10X0
1100
1111
672 (my $plugin = Jifty->find_plugin("Jifty::Plugin::TestServerWarnings")) &&
673 0 grep { $_ eq 'Jifty::View::Declare::Handler' } Jifty->handler->view_handlers) { # testserverwarnings plugin requires TD handler to work properly.
674 0 my @warnings = $plugin->decoded_warnings( 'http://localhost:'.$Jifty::SERVER->port );
675  
676 0 $Test->diag("Uncaught warning: $_") for @warnings;
677 0 ▶ if ($WARNINGS_ARE_FATAL && @warnings) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WARNINGS_ARE_FATAL and @warnings) 0 0 0
Condition0%0/3
$WARNINGS_ARE_FATAL and @warnings
!ll&&!rl&&r
000
MC/DC0%0/2
$WARNINGS_ARE_FATAL and @warnings
$WARNINGS_ARE_FATAL @warnings
Truth table0%0/3
A and B
A= $WARNINGS_ARE_FATALB= @warnings
ABresult
0X0
100
111
678 0 $Test->diag('Warnings not accepted in strict mode.');
679 0 $should_die = 1;
680 }
681 }
682  
683 # Turn off the server
684 1 undef $Jifty::SERVER;
685  
686 # If all tests passed..
687 1 ▶ if (Jifty::Test->is_passing && Jifty::Test->is_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty::Test"->is_passing and "Jifty::Test"->is_done) 0 1 1
Condition33%1/3
"Jifty::Test"->is_passing and "Jifty::Test"->is_done
!ll&&!rl&&r
100
MC/DC0%0/2
"Jifty::Test"->is_passing and "Jifty::Test"->is_done
"Jifty::Test"->is_passing "Jifty::Test"->is_done
Truth table33%1/3
A and B
A= "Jifty::Test"->is_passingB= "Jifty::Test"->is_done
ABresult
0X0
100
111
688 # Clean up mailbox
689 0 Jifty::Test->teardown_mailbox;
690  
691 # Remove testing db
692 0 ▶ if (Jifty->handle && !$ENV{JIFTY_FAST_TEST}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Jifty"->handle and not $ENV{"JIFTY_FAST_TEST"}) 0 0 0
Condition0%0/3
"Jifty"->handle and not $ENV{"JIFTY_FAST_TEST"}
!ll&&!rl&&r
000
MC/DC0%0/2
"Jifty"->handle and not $ENV{"JIFTY_FAST_TEST"}
"Jifty"->handle not $ENV{"JIFTY_FAST_TEST"}
Truth table0%0/3
A and not(B)
A= "Jifty"->handleB= not $ENV{"JIFTY_FAST_TEST"}
ABresult
0X0
100
111
693 0 Jifty->handle->disconnect();
694 0 my $schema = Jifty::Script::Schema->new;
695 0 $schema->{drop_database} = 1;
696  
697 # The schema dropper dies when it can't drop the database
698 # this shouldn't kill tests
699 0 local $@;
700 0 eval { $schema->run };
701 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
702 0 warn $err;
703 }
704 }
705  
706 # Unlink test files
707 0 unlink @Test_Files_To_Cleanup;
708 }
709  
710 # Cleanup the tempdirs
711 1 File::Temp::cleanup();
712  
713 # Unlink test file
714 1 ▶ unlink $Test->{test_config} if $Test->{test_config};
Branch: 1 branch
BranchTrueFalseTotal
if $Test->{"test_config"} 0 1 1
715 1 ▶ exit -1 if $should_die;
Branch: 1 branch
BranchTrueFalseTotal
if $should_die 0 1 1
716 }
717  
718 =head1 SEE ALSO
719  
720 L<Jifty::Test::WWW::Mechanize>, L<Jifty::TestServer>
721  
722 =cut
723  
724 1 1;
« Jifty.pm ↑ Summary Jifty/API.pm »