Skip to content

Commit

Permalink
t/LocalDB/Index/Blast.t: create test tempfiles within a tempdir (issue
Browse files Browse the repository at this point in the history
…#281)

There's other tests that create files with the same name on the current
working directory with the same name.  If running the test suite with
jobs in parallel this causes failing tests, because multiple tests will
be writing and reading to the same file at the same time.

So use File::Temp to avoid clashes.

Specific case here is that Bio::Index::Blast->new will not creat a new
file, it will use an existing one if it's there. In this case, it was
reading an Index file for anoter format which had a different version
number.

Take the chance to rewrite the test to avoid code duplication.
  • Loading branch information
carandraug committed Sep 15, 2018
1 parent c059b46 commit 926dbeb
Showing 1 changed file with 64 additions and 60 deletions.
124 changes: 64 additions & 60 deletions t/LocalDB/Index/Blast.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,72 @@
# $Id$

use strict;
use File::Spec;
use File::Temp;

BEGIN {
use lib '.';
use Bio::Root::Test;

test_begin(-tests => 26,
-requires_module => 'IO::String');

use_ok('Cwd');
use_ok('Bio::SearchIO');
use_ok('Bio::Index::Blast');
}
use lib '.';
use Bio::Root::Test;

test_begin(-tests => 5,
-requires_module => 'IO::String');

# BLASTP

my $index = Bio::Index::Blast->new(-filename => 'Wibbl',
-write_flag => 1);
ok($index);

$index->make_index(test_input_file('multi_blast.bls'));
($index->dbm_package eq 'SDBM_File') ?
(ok(-e "Wibbl.pag" && -e "Wibbl.dir")) :
(ok(-e "Wibbl"));

foreach my $id ( qw(CATH_RAT PAPA_CARPA) ) {
my $fh = $index->get_stream($id);
ok($fh);
ok( ! eof($fh) );
my $report = Bio::SearchIO->new(-noclose => 1,
-format => 'blast',
-fh => $fh);
my $result = $report->next_result;
like($result->query_name, qr/$id/);
ok( $result->next_hit);

like( $index->fetch_report($id)->query_name, qr/$id/);
use_ok('Cwd');
use_ok('Bio::SearchIO');
use_ok('Bio::Index::Blast');
}
# ActivePerl will not allow deletion if the tie-hash is still active
$index->DESTROY;
unlink qw( Wibbl Wibbl.pag Wibbl.dir );

# RPS-BLAST

$index = Bio::Index::Blast->new(-filename => 'Wibbl.index',
-write_flag => 1);
ok($index);

$index->make_index(test_input_file('rpsblast.bls'));

foreach my $id ( qw(orf20 orf40) ) {
my $fh = $index->get_stream($id);
ok($fh);
ok( ! eof($fh) );
my $report = Bio::SearchIO->new(-noclose => 1,
-format => 'blast',
-fh => $fh);
my $result = $report->next_result;
like($result->query_name, qr/$id/);
ok( $result->next_hit);

like( $index->fetch_report($id)->query_name, qr/$id/);

sub test_results {
my $index = shift;
my @ids = @_;

foreach my $id (@ids) {
my $fh = $index->get_stream($id);
ok($fh);
ok( ! eof($fh) );
my $report = Bio::SearchIO->new(-noclose => 1,
-format => 'blast',
-fh => $fh);
my $result = $report->next_result;
like($result->query_name, qr/$id/);
ok( $result->next_hit);

like( $index->fetch_report($id)->query_name, qr/$id/);
}
}
# ActivePerl will not allow deletion if the tie-hash is still active
$index->DESTROY;
unlink qw( Wibbl.index Wibbl.index.pag Wibbl.index.dir );


subtest 'BLASTP' => sub {
plan tests => 2 + 2*5;

my $dir = File::Temp->newdir();
my $basename = 'Wibbl';
my $filepath = File::Spec->catfile($dir, $basename);

my $index = Bio::Index::Blast->new(-filename => $filepath,
-write_flag => 1);
ok($index);

$index->make_index(test_input_file('multi_blast.bls'));
($index->dbm_package eq 'SDBM_File') ?
(ok(-e "$filepath.pag" && -e "$filepath.dir")) :
(ok(-e "$filepath"));

test_results($index, qw(CATH_RAT PAPA_CARPA));
};

subtest 'RPS-BLAST' => sub {
plan tests => 1 + 2*5;

my $dir = File::Temp->newdir();
my $basename = 'Wibbl.index';
my $filepath = File::Spec->catfile($dir, $basename);

my $index = Bio::Index::Blast->new(-filename => $filepath,
-write_flag => 1);
ok($index);

$index->make_index(test_input_file('rpsblast.bls'));

test_results($index, qw(orf20 orf40));
};

0 comments on commit 926dbeb

Please sign in to comment.