Skip to content

Commit

Permalink
Make sure files don't change in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdbeek committed Dec 8, 2017
1 parent 90a56ae commit eb636c1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
Empty file.
Empty file.
13 changes: 10 additions & 3 deletions test/unit/datatypes/test_bam.py
Expand Up @@ -26,9 +26,16 @@ def test_dataset_content_needs_grooming():

def test_groom_dataset_content():
b = Bam()
with get_input_files('2.shuffled.bam') as input_files:
b.groom_dataset_content(input_files[0])
assert b.dataset_content_needs_grooming(input_files[0]) is False
try:
with get_input_files('2.shuffled.bam') as input_files:
b.groom_dataset_content(input_files[0])
assert b.dataset_content_needs_grooming(input_files[0]) is False
except AssertionError as e:
# Grooming modifies files in-place, so the md5 hash comparison has to fail
assert 'Unexpected change' in e.message
return
# should not reach this part of the test
raise Exception('Bam grooming did not occur in-place')


def test_set_meta_presorted():
Expand Down
23 changes: 15 additions & 8 deletions test/unit/datatypes/util.py
Expand Up @@ -5,6 +5,7 @@

from galaxy.datatypes.sniff import get_test_fname
from galaxy.util.bunch import Bunch
from galaxy.util.hash_util import md5_hash_file


@contextmanager
Expand All @@ -22,9 +23,10 @@ def get_dataset(file, index_attr='bam_index', dataset_id=1, has_data=True):


@contextmanager
def get_tmp_path():
def get_tmp_path(should_exist=False):
_, path = tempfile.mkstemp()
os.remove(path)
if not should_exist:
os.remove(path)
yield path
try:
os.remove(path)
Expand All @@ -34,11 +36,16 @@ def get_tmp_path():

@contextmanager
def get_input_files(*args):
# need to import here, otherwise get_test_fname is treated as a test
temp_dir = tempfile.mkdtemp()
test_files = []
for file in args:
shutil.copy(get_test_fname(file), temp_dir)
test_files.append(os.path.join(temp_dir, file))
yield test_files
shutil.rmtree(temp_dir, ignore_errors=True)
try:
for file in args:
shutil.copy(get_test_fname(file), temp_dir)
test_files.append(os.path.join(temp_dir, file))
md5_sums = [md5_hash_file(f) for f in test_files]
yield test_files
new_md5_sums = [md5_hash_file(f) for f in test_files]
for old_hash, new_hash, f in zip(md5_sums, new_md5_sums, test_files):
assert old_hash == new_hash, 'Unexpected change of content for file %s' % f
finally:
shutil.rmtree(temp_dir, ignore_errors=True)

0 comments on commit eb636c1

Please sign in to comment.