Skip to content

Commit 5333634

Browse files
committed
Added an option to expect failures when testing scripts
1 parent 78917fe commit 5333634

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

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

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ sub standaloneJob {
166166
Arg[2] : String $url. The location of the database to be created
167167
Arg[3] : (optional) Arrayref $args. Extra parameters of the pipeline (as on the command-line)
168168
Arg[4] : (optional) Arrayref $tweaks. Tweaks to be applied to the database (as with the -tweak command-line option)
169+
Arg[5] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
170+
key that is understood is "expect_failure" to reverse the expectation of the test.
169171
Example : init_pipeline(
170172
'Bio::EnsEMBL::Hive::Examples::LongMult::PipeConfig::LongMultServer_conf',
171173
$server_url,
@@ -183,7 +185,7 @@ sub standaloneJob {
183185
=cut
184186

185187
sub init_pipeline {
186-
my ($file_or_module, $url, $options, $tweaks) = @_;
188+
my ($file_or_module, $url, $options, $tweaks, $flags) = @_;
187189

188190
$options ||= [];
189191

@@ -203,7 +205,7 @@ sub init_pipeline {
203205
push @args, @$options;
204206
push @args, map {-tweak => $_} @$tweaks if $tweaks;
205207

206-
return _test_ehive_script('init_pipeline', undef, \@args);
208+
return _test_ehive_script('init_pipeline', undef, \@args, undef, $flags);
207209
}
208210

209211

@@ -214,6 +216,8 @@ sub init_pipeline {
214216
Arg[2] : String $url. The location of the database
215217
Arg[3] : Arrayref $args. Extra arguments given to the script
216218
Arg[4] : String $test_name (optional). The name of the test
219+
Arg[5] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
220+
key that is understood is "expect_failure" to reverse the expectation of the test.
217221
Description : Generic method that can run any eHive script and check its return status
218222
Returntype : None
219223
Exceptions : TAP-style
@@ -223,12 +227,19 @@ sub init_pipeline {
223227
=cut
224228

225229
sub _test_ehive_script {
226-
my ($script_name, $url, $args, $test_name) = @_;
230+
my ($script_name, $url, $args, $test_name, $flags) = @_;
227231
$args ||= [];
232+
$flags ||= {};
228233
my @ext_args = ( defined($url) ? (-url => $url) : (), @$args );
229-
$test_name ||= 'Can run '.$script_name.(@ext_args ? ' with the following cmdline options: '.join(' ', @ext_args) : '');
230234

231-
ok(!system($ENV{'EHIVE_ROOT_DIR'}.'/scripts/'.$script_name.'.pl', @ext_args), $test_name);
235+
my $rc = system($ENV{'EHIVE_ROOT_DIR'}.'/scripts/'.$script_name.'.pl', @ext_args);
236+
if ($flags->{expect_failure}) {
237+
$test_name ||= $script_name.' fails'.(@ext_args ? ' with the command-line options: '.join(' ', @ext_args) : '');
238+
ok($rc, $test_name);
239+
} else {
240+
$test_name ||= 'Can run '.$script_name.(@ext_args ? ' with the command-line options: '.join(' ', @ext_args) : '');
241+
is($rc, 0, $test_name);
242+
}
232243
}
233244

234245

@@ -237,6 +248,8 @@ sub _test_ehive_script {
237248
Arg[1] : String $url. The location of the database
238249
Arg[2] : Arrayref $args. Extra arguments given to runWorker
239250
Arg[3] : String $test_name (optional). The name of the test
251+
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
252+
key that is understood is "expect_failure" to reverse the expectation of the test.
240253
Example : runWorker($url, [ -can_respecialize => 1 ]);
241254
Description : Run a worker on the given pipeline in the current process.
242255
The worker options have been divided in three groups: the ones affecting its specialization,
@@ -269,6 +282,8 @@ sub runWorker {
269282
Arg[1] : String $url. The location of the database
270283
Arg[2] : Arrayref $args. Extra arguments given to seed_pipeline
271284
Arg[3] : String $test_name (optional). The name of the test
285+
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
286+
key that is understood is "expect_failure" to reverse the expectation of the test.
272287
Example : $seed_pipeline($url, [$arg1, $arg2], 'Run seed_pipeline with two arguments');
273288
Description : Very generic function to run seed_pipeline on the given database with the given arguments
274289
Returntype : None
@@ -289,6 +304,8 @@ sub seed_pipeline {
289304
Arg[1] : String $url. The location of the database
290305
Arg[2] : Arrayref $args. Extra arguments given to beekeeper.pl
291306
Arg[3] : String $test_name (optional). The name of the test
307+
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
308+
key that is understood is "expect_failure" to reverse the expectation of the test.
292309
Example : beekeeper($url, [$arg1, $arg2], 'Run beekeeper with two arguments');
293310
Description : Very generic function to run beekeeper on the given database with the given arguments
294311
Returntype : None
@@ -326,6 +343,8 @@ sub tweak_pipeline {
326343
Arg[1] : String $url or undef. The location of the database
327344
Arg[2] : Arrayref $args. Extra arguments given to generate_graph.pl
328345
Arg[3] : String $test_name (optional). The name of the test
346+
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
347+
key that is understood is "expect_failure" to reverse the expectation of the test.
329348
Example : generate_graph($url, [-output => 'lm_analyses.png'], 'Generate a PNG A-diagram');
330349
Description : Very generic function to run generate_graph.pl on the given database with the given arguments
331350
Returntype : None
@@ -345,6 +364,8 @@ sub generate_graph {
345364
Arg[1] : String $url. The location of the database
346365
Arg[2] : Arrayref $args. Extra arguments given to visualize_jobs.pl
347366
Arg[3] : String $test_name (optional). The name of the test
367+
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
368+
key that is understood is "expect_failure" to reverse the expectation of the test.
348369
Example : visualize_jobs($url, [-output => 'lm_jobs.png', -accu_values], 'Generate a PNG J-diagram with accu values');
349370
Description : Very generic function to run visualize_jobs.pl on the given database with the given arguments
350371
Returntype : None
@@ -382,6 +403,8 @@ sub peekJob {
382403
Arg[1] : String $url. The location of the database
383404
Arg[2] : Arrayref $args. Extra arguments given to db_cmd.pl
384405
Arg[3] : String $test_name (optional). The name of the test
406+
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
407+
key that is understood is "expect_failure" to reverse the expectation of the test.
385408
Example : db_cmd($url, [-sql => 'DROP DATABASE'], 'Drop the database');
386409
Description : Very generic function to run db_cmd.pl on the given database with the given arguments
387410
Returntype : None
@@ -401,6 +424,8 @@ sub db_cmd {
401424
Arg[1] : String $url. The location of the database
402425
Arg[2] : String $sql. The SQL to run on the database
403426
Arg[3] : String $test_name (optional). The name of the test
427+
Arg[4] : (optional) Hashref $flags. Flags given to the text framework. Currently the only
428+
key that is understood is "expect_failure" to reverse the expectation of the test.
404429
Example : run_sql_on_db($url, 'INSERT INTO sweets (name, quantity) VALUES (3, 'Snickers')');
405430
Description : Execute an SQL command on the given database and test its execution. This expects the
406431
command-line client to return a non-zero code in case of a failure.
@@ -412,8 +437,10 @@ sub db_cmd {
412437
=cut
413438

414439
sub run_sql_on_db {
415-
my ($url, $sql, $test_name) = @_;
416-
return _test_ehive_script('db_cmd', $url, [-sql => $sql], $test_name // 'Can run '.$sql);
440+
my ($url, $sql, $test_name, $flags) = @_;
441+
$flags ||= {};
442+
$test_name //= $flags->{expect_failure} ? $sql.' fails' : 'Can run '.$sql;
443+
return _test_ehive_script('db_cmd', $url, [-sql => $sql], $test_name, $flags);
417444
}
418445

419446

t/02.api/create_drop_database.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ foreach my $test_url (@$ehive_test_pipeline_urls) {
4444
if ($dbc->driver eq 'sqlite') {
4545
run_sql_on_db($test_url, 'DROP DATABASE', "'rm -f' doesn't care about missing files");
4646
} else {
47-
is(system(@{ $dbc->to_cmd(undef, undef, undef, 'DROP DATABASE') }), 256, "Cannot drop a database that doesn't exist");
47+
run_sql_on_db($test_url, 'DROP DATABASE', "Can drop a database that exists", {'expect_failure' => 1});
4848
}
4949
run_sql_on_db($test_url, 'CREATE DATABASE', 'Can create a database');
5050
run_sql_on_db($test_url, 'CREATE DATABASE IF NOT EXISTS', 'Further CREATE DATABASE statements are ignored') unless $dbc->driver eq 'pgsql';

t/03.scripts/beekeeper_opts.t

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ my $pipeline_url = get_test_url_or_die();
6262
is($found_beekeeper_dash_run, 1, 'A beekeeper with option -run was registered in the beekeeper table');
6363

6464
# Check that -run -job_id with a non-existant job id fails with TASK_FAILED
65-
# Not using beekeeper() because we expect the command to *fail*
66-
my @bad_job_cmd = ($ENV{'EHIVE_ROOT_DIR'}.'/scripts/beekeeper.pl', -url => $hive_dba->dbc->url, '-run', -job_id => 98765);
67-
my $rc;
68-
my $bk_stderr = capture_stderr {
69-
$rc = system(@bad_job_cmd);
70-
};
71-
ok($rc, 'beekeeper -run -job_id 98765 exited with a non-zero return code');
72-
like($bk_stderr, qr/Could not fetch Job with dbID=98765/, 'beekeeper complained that the job cannot be found');
65+
beekeeper($hive_dba->dbc->url,
66+
['-run', -job_id => 98765],
67+
'beekeeper complained that the job cannot be found',
68+
{'expect_failure' => 1},
69+
);
7370

7471
$beekeeper_rows = $beekeeper_nta->fetch_all();
7572
is(scalar(@$beekeeper_rows), 3, 'After -sync, -run, and -run -job_id, there are exactly three entries in the beekeeper table');
@@ -83,16 +80,12 @@ my $pipeline_url = get_test_url_or_die();
8380
is($found_beekeeper_bad_job, 1, 'A beekeeper with option -job_id was registered in the beekeeper table');
8481

8582
# Check that -loop -analyses_pattern with a non-matching pattern fails with TASK_FAILED
86-
# Not using beekeeper() because we expect the command to *fail*
87-
my @bad_pattern_cmd = ($ENV{'EHIVE_ROOT_DIR'}.'/scripts/beekeeper.pl',
88-
-url => $hive_dba->dbc->url,
89-
-analyses_pattern => 'this_matches_no_analysis',
90-
'-loop');
91-
$bk_stderr = capture_stderr {
92-
$rc = system(@bad_pattern_cmd);
93-
};
94-
ok($rc, 'beekeeper -loop -analyses_pattern this_matches_no_analysis exited with a non-zero return code');
95-
like($bk_stderr, qr/the -analyses_pattern 'this_matches_no_analysis' did not match any Analyses/, 'beekeeper complained that no analysis could be matched');
83+
beekeeper(
84+
$hive_dba->dbc->url,
85+
['-loop', -analyses_pattern => 'this_matches_no_analysis'],
86+
'beekeeper complained that no analysis could be matched',
87+
{'expect_failure' => 1},
88+
);
9689

9790
$beekeeper_rows = $beekeeper_nta->fetch_all();
9891
is(scalar(@$beekeeper_rows), 4, 'After 4 beekeeper commands, there are exactly 4 entries in the beekeeper table');

0 commit comments

Comments
 (0)