Skip to content

Commit 298e451

Browse files
committed
expect_failure can now be a regexp to test the error message
1 parent 5333634 commit 298e451

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

modules/Bio/EnsEMBL/Hive/Utils/Test.pm

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use warnings;
2525
no warnings qw( redefine );
2626

2727
use Exporter;
28+
use Capture::Tiny 'tee_stderr';
2829
use Carp qw{croak};
2930
use File::Spec;
3031
use File::Temp qw{tempfile};
@@ -168,6 +169,8 @@ sub standaloneJob {
168169
Arg[4] : (optional) Arrayref $tweaks. Tweaks to be applied to the database (as with the -tweak command-line option)
169170
Arg[5] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
170171
key that is understood is "expect_failure" to reverse the expectation of the test.
172+
It is primarily used as a boolean, but can be a regular expression to test the standard
173+
error of the script.
171174
Example : init_pipeline(
172175
'Bio::EnsEMBL::Hive::Examples::LongMult::PipeConfig::LongMultServer_conf',
173176
$server_url,
@@ -218,6 +221,8 @@ sub init_pipeline {
218221
Arg[4] : String $test_name (optional). The name of the test
219222
Arg[5] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
220223
key that is understood is "expect_failure" to reverse the expectation of the test.
224+
It is primarily used as a boolean, but can be a regular expression to test the standard
225+
error of the script.
221226
Description : Generic method that can run any eHive script and check its return status
222227
Returntype : None
223228
Exceptions : TAP-style
@@ -232,10 +237,16 @@ sub _test_ehive_script {
232237
$flags ||= {};
233238
my @ext_args = ( defined($url) ? (-url => $url) : (), @$args );
234239

235-
my $rc = system($ENV{'EHIVE_ROOT_DIR'}.'/scripts/'.$script_name.'.pl', @ext_args);
240+
my $rc;
241+
my $stderr = tee_stderr {
242+
$rc = system($ENV{'EHIVE_ROOT_DIR'}.'/scripts/'.$script_name.'.pl', @ext_args);
243+
};
236244
if ($flags->{expect_failure}) {
237245
$test_name ||= $script_name.' fails'.(@ext_args ? ' with the command-line options: '.join(' ', @ext_args) : '');
238246
ok($rc, $test_name);
247+
if (re::is_regexp($flags->{expect_failure})) {
248+
like($stderr, $flags->{expect_failure}, 'error message as expected');
249+
}
239250
} else {
240251
$test_name ||= 'Can run '.$script_name.(@ext_args ? ' with the command-line options: '.join(' ', @ext_args) : '');
241252
is($rc, 0, $test_name);
@@ -250,6 +261,8 @@ sub _test_ehive_script {
250261
Arg[3] : String $test_name (optional). The name of the test
251262
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
252263
key that is understood is "expect_failure" to reverse the expectation of the test.
264+
It is primarily used as a boolean, but can be a regular expression to test the standard
265+
error of the script.
253266
Example : runWorker($url, [ -can_respecialize => 1 ]);
254267
Description : Run a worker on the given pipeline in the current process.
255268
The worker options have been divided in three groups: the ones affecting its specialization,
@@ -284,6 +297,8 @@ sub runWorker {
284297
Arg[3] : String $test_name (optional). The name of the test
285298
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
286299
key that is understood is "expect_failure" to reverse the expectation of the test.
300+
It is primarily used as a boolean, but can be a regular expression to test the standard
301+
error of the script.
287302
Example : $seed_pipeline($url, [$arg1, $arg2], 'Run seed_pipeline with two arguments');
288303
Description : Very generic function to run seed_pipeline on the given database with the given arguments
289304
Returntype : None
@@ -306,6 +321,8 @@ sub seed_pipeline {
306321
Arg[3] : String $test_name (optional). The name of the test
307322
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
308323
key that is understood is "expect_failure" to reverse the expectation of the test.
324+
It is primarily used as a boolean, but can be a regular expression to test the standard
325+
error of the script.
309326
Example : beekeeper($url, [$arg1, $arg2], 'Run beekeeper with two arguments');
310327
Description : Very generic function to run beekeeper on the given database with the given arguments
311328
Returntype : None
@@ -345,6 +362,8 @@ sub tweak_pipeline {
345362
Arg[3] : String $test_name (optional). The name of the test
346363
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
347364
key that is understood is "expect_failure" to reverse the expectation of the test.
365+
It is primarily used as a boolean, but can be a regular expression to test the standard
366+
error of the script.
348367
Example : generate_graph($url, [-output => 'lm_analyses.png'], 'Generate a PNG A-diagram');
349368
Description : Very generic function to run generate_graph.pl on the given database with the given arguments
350369
Returntype : None
@@ -366,6 +385,8 @@ sub generate_graph {
366385
Arg[3] : String $test_name (optional). The name of the test
367386
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
368387
key that is understood is "expect_failure" to reverse the expectation of the test.
388+
It is primarily used as a boolean, but can be a regular expression to test the standard
389+
error of the script.
369390
Example : visualize_jobs($url, [-output => 'lm_jobs.png', -accu_values], 'Generate a PNG J-diagram with accu values');
370391
Description : Very generic function to run visualize_jobs.pl on the given database with the given arguments
371392
Returntype : None
@@ -405,6 +426,8 @@ sub peekJob {
405426
Arg[3] : String $test_name (optional). The name of the test
406427
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
407428
key that is understood is "expect_failure" to reverse the expectation of the test.
429+
It is primarily used as a boolean, but can be a regular expression to test the standard
430+
error of the script.
408431
Example : db_cmd($url, [-sql => 'DROP DATABASE'], 'Drop the database');
409432
Description : Very generic function to run db_cmd.pl on the given database with the given arguments
410433
Returntype : None
@@ -426,6 +449,8 @@ sub db_cmd {
426449
Arg[3] : String $test_name (optional). The name of the test
427450
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
428451
key that is understood is "expect_failure" to reverse the expectation of the test.
452+
It is primarily used as a boolean, but can be a regular expression to test the standard
453+
error of the script.
429454
Example : run_sql_on_db($url, 'INSERT INTO sweets (name, quantity) VALUES (3, 'Snickers')');
430455
Description : Execute an SQL command on the given database and test its execution. This expects the
431456
command-line client to return a non-zero code in case of a failure.

t/03.scripts/beekeeper_opts.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ my $pipeline_url = get_test_url_or_die();
6565
beekeeper($hive_dba->dbc->url,
6666
['-run', -job_id => 98765],
6767
'beekeeper complained that the job cannot be found',
68-
{'expect_failure' => 1},
68+
{'expect_failure' => qr/Could not fetch Job with dbID=98765/},
6969
);
7070

7171
$beekeeper_rows = $beekeeper_nta->fetch_all();
@@ -84,7 +84,7 @@ my $pipeline_url = get_test_url_or_die();
8484
$hive_dba->dbc->url,
8585
['-loop', -analyses_pattern => 'this_matches_no_analysis'],
8686
'beekeeper complained that no analysis could be matched',
87-
{'expect_failure' => 1},
87+
{'expect_failure' => qr/the -analyses_pattern 'this_matches_no_analysis' did not match any Analyses/},
8888
);
8989

9090
$beekeeper_rows = $beekeeper_nta->fetch_all();

0 commit comments

Comments
 (0)