diff --git a/tests/test_fsl_utils_path.py b/tests/test_fsl_utils_path.py index 29ed7e3704fb97b50fc1de64a31e8f2243de2c63..d577739df5d44218190bae9788a5019600e330e3 100644 --- a/tests/test_fsl_utils_path.py +++ b/tests/test_fsl_utils_path.py @@ -629,7 +629,7 @@ def test_getFileGroup_imageFiles_shouldPass(): allowedExts = fslimage.ALLOWED_EXTENSIONS groups = fslimage.FILE_GROUPS - # [(files_to_create, path, files_to_expect), + # [(files_to_create, path, files_to_expect [, unambiguous]), # ... # ] # @@ -666,13 +666,30 @@ def test_getFileGroup_imageFiles_shouldPass(): # file groups should still work. ('file.hdr', 'file', 'file.hdr'), ('file.hdr', 'file.hdr', 'file.hdr'), + + # Unambigiuous paths, when + # unambiguous = True, + # should be ok. + ('file.hdr file.img file.nii', 'file.nii', 'file.nii', True), + ('file.hdr file.img file.nii', 'file.hdr', 'file.hdr file.img', True), + ('file.hdr file.img file.nii', 'file.img', 'file.hdr file.img', True), + ] + # TODO You need to add passing tests for unambiguous=True + workdir = tempfile.mkdtemp() try: - for files_to_create, path, files_to_expect in tests: + for test in tests: + + files_to_create = test[0] + path = test[1] + files_to_expect = test[2] + + if len(test) == 4: unambiguous = test[3] + else: unambiguous = False files_to_create = files_to_create.split() files_to_expect = files_to_expect.split() @@ -690,12 +707,14 @@ def test_getFileGroup_imageFiles_shouldPass(): op.join(workdir, path), allowedExts=allowedExts, fileGroups=groups, - fullPaths=True) + fullPaths=True, + unambiguous=unambiguous) exts = fslpath.getFileGroup( op.join(workdir, path), allowedExts=allowedExts, fileGroups=groups, - fullPaths=False) + fullPaths=False, + unambiguous=unambiguous) assert sorted(fullPaths) == sorted([op.join(workdir, e) for e in files_to_expect]) assert sorted(exts) == sorted([fslpath.getExt(e, allowedExts) for e in files_to_expect]) @@ -708,7 +727,7 @@ def test_getFileGroup_imageFiles_shouldPass(): def test_getFileGroup_otherFiles_shouldPass(): - # (files_to_create, allowedExts, fileGroups, path, files_to_expect) + # (files_to_create, allowedExts, fileGroups, path, files_to_expect [, unambiguous]) tests = [ # allowedExts is None - incomplete paths are not allowed @@ -758,16 +777,34 @@ def test_getFileGroup_otherFiles_shouldPass(): ('file1.a file1.b file2.c file2.d', '.a .b .c .d', ['.a .b', '.c .d'], 'file2.c', 'file2.c file2.d'), ('file1.a file1.b file2.c file2.d', '.a .b .c .d', ['.a .b', '.c .d'], 'file2.d', 'file2.c file2.d'), - # incomplete group + # incomplete group should be ok when + # unambiguous = False (the default) ('file.a', '.a .b', ['.a .b'], 'file', 'file.a'), ('file.a', '.a .b', ['.a .b'], 'file.a', 'file.a'), + + + # Unambiguous/complete group should + # be ok when unambiguous = True + ('file.a file.b file.c', '.a .b .c', ['.a .b'], 'file.a', 'file.a file.b', True), + ('file.a file.b file.c', '.a .b .c', ['.a .b'], 'file.b', 'file.a file.b', True), + ('file.a file.b file.c', '.a .b .c', ['.a .b'], 'file.c', 'file.c', True), ] workdir = tempfile.mkdtemp() try: - for files_to_create, allowedExts, fileGroups, path, files_to_expect in tests: + for test in tests: + + files_to_create = test[0] + allowedExts = test[1] + fileGroups = test[2] + path = test[3] + files_to_expect = test[4] + + if len(test) == 6: unambiguous = test[5] + else: unambiguous = False + files_to_create = files_to_create.split() allowedExts = allowedExts.split() @@ -792,12 +829,14 @@ def test_getFileGroup_otherFiles_shouldPass(): op.join(workdir, path), allowedExts=allowedExts, fileGroups=fileGroups, - fullPaths=True) + fullPaths=True, + unambiguous=unambiguous) exts = fslpath.getFileGroup( op.join(workdir, path), allowedExts=allowedExts, fileGroups=fileGroups, - fullPaths=False) + fullPaths=False, + unambiguous=unambiguous) assert sorted(fullPaths) == sorted([op.join(workdir, e) for e in files_to_expect]) assert sorted(exts) == sorted([fslpath.getExt(e, allowedExts) for e in files_to_expect]) @@ -837,7 +876,10 @@ def test_getFileGroup_shouldFail(): ('file.hdr', 'file', allowedExts, fileGroups, True), ('file.hdr', 'file.hdr', allowedExts, fileGroups, True), ('file.img', 'file', allowedExts, fileGroups, True), - ('file.img', 'file.img', allowedExts, fileGroups, True), + ('file.img', 'file.img', allowedExts, fileGroups, True), + + # Part of more than one group, when unambiguous is True + ('file.a file.b file.c', 'file.a', '.a .b', ['.a .b', '.a .c'], True), ] workdir = tempfile.mkdtemp() diff --git a/tests/test_immv_imcp.py b/tests/test_immv_imcp.py index eb01fa7596de1bd052cd8f30171f034183c55d45..73e6fb29fef2e76d1d085dfe8097d1ea7ff64d92 100644 --- a/tests/test_immv_imcp.py +++ b/tests/test_immv_imcp.py @@ -5,14 +5,19 @@ # Author: Paul McCarthy <pauldmccarthy@gmail.com> # -import os -import os.path as op -import shutil -import tempfile +from __future__ import print_function + +import os +import os.path as op +import shutil +import subprocess as sp +import tempfile import numpy as np import nibabel as nib +import pytest + import fsl.utils.path as fslpath import fsl.utils.imcp as imcp import fsl.scripts.imcp as imcp_script @@ -23,11 +28,22 @@ import fsl.data.image as fslimage def createImageFile(filename): data = np.random.random((10, 10, 10)) - img = nib.Nifti1Image(data, np.eye(4)) - nib.save(img, filename) + # Image file + try: + img = nib.Nifti1Image(data, np.eye(4)) + + nib.save(img, filename) + + return hash(data.tobytes()) - return hash(data.tobytes()) + # Non-image file + except: + contents = '{}\n'.format(op.basename(filename)) + with open(filename, 'wt') as f: + f.write(contents) + + return hash(contents) def checkImageFile(filename, datahash): @@ -42,19 +58,35 @@ def checkFilesToExpect(files, outdir, outputType, datahashes): 'NIFTI' : ['.nii'], 'NIFTI_PAIR' : ['.hdr', '.img'], 'NIFTI_GZ' : ['.nii.gz'], - }.get(outputType, ['.nii.gz']) - + '' : ['.nii.gz'], + }.get(outputType, None) allFiles = [] - for f, h in zip(files.split(), datahashes): + if isinstance(files, str): + files = files.split() + + for f in files: + + f, fe = fslimage.splitExt(f) + fexts = exts - for e in exts: + if fexts is None: + fexts = { + '.img' : ['.hdr', '.img'], + '.hdr' : ['.hdr', '.img'], + '.nii' : ['.nii'], + '.nii.gz' : ['.nii.gz'] + }.get(fe, []) + + for e in fexts: expected = op.join(outdir, f + e) allFiles.append(expected) + print(' ', expected) + assert op.exists(expected) allThatExist = os.listdir(outdir) @@ -62,19 +94,37 @@ def checkFilesToExpect(files, outdir, outputType, datahashes): assert len(allThatExist) == len(allFiles) - for f, h in zip(files.split(), datahashes): + for i, f in enumerate(files): f = fslimage.addExt(op.join(outdir, f), mustExist=True) + if isinstance(datahashes, list): + if len(datahashes) > len(files): + diff = len(datahashes) - len(files) + h = datahashes[i + diff] + + else: + h = datahashes[i] + else: + h = datahashes[op.basename(f)] + checkImageFile(f, h) +def cleardir(dir): + + for f in os.listdir(dir): + f = op.join(dir, f) + if op.isfile(f): os.remove(f) + elif op.isdir(f): shutil.rmtree(f) + def test_imcp_script_shouldPass(move=False): # The imcp/immv scripts should honour the # FSLOUTPUTTYPE env var. If it is unset - # or invalid), they should produce .nii.gz - outputTypes = ['NIFTI', 'NIFTI_PAIR', 'NIFTI_GZ', 'BLAH_DI_BLAH'] + # or invalid - '' in this case), they + # should produce .nii.gz + outputTypes = ['NIFTI', 'NIFTI_PAIR', 'NIFTI_GZ', ''] # Test tuples have the following structure (each @@ -242,7 +292,24 @@ def test_imcp_script_shouldPass(move=False): ('a.img b.img', 'a.hdr a.hdr b b.hdr .', 'a b'), ('a.img b.img', 'a.hdr a.hdr b.img b .', 'a b'), ('a.img b.img', 'a.hdr a.hdr b.img b.img .', 'a b'), - ('a.img b.img', 'a.hdr a.hdr b.img b.hdr .', 'a b'), + ('a.img b.img', 'a.hdr a.hdr b.img b.hdr .', 'a b'), + + # Inputs which cause the destination + # to be overwritten - this should be + # ok, the destination should be the + # last specified input. The order of + # files_to_create has to match the + # imcp_args order, otherwise my bodgy + # checkFilesToExpect function will + # break. + ('a.nii a.img', 'a.nii a.img .', 'a'), + ('a.img a.nii', 'a.img a.nii .', 'a'), + ('a.nii a.img a.nii.gz', 'a.nii a.img a.nii.gz .', 'a'), + ('a.nii a.nii.gz a.img ', 'a.nii a.nii.gz a.img .', 'a'), + ('a.img a.nii.gz a.nii ', 'a.img a.nii.gz a.nii .', 'a'), + ('a.img a.nii a.nii.gz', 'a.img a.nii a.nii.gz .', 'a'), + ('a.nii.gz a.img a.nii ', 'a.nii.gz a.img a.nii .', 'a'), + ('a.nii.gz a.nii a.img ', 'a.nii.gz a.nii a.img .', 'a'), ] indir = tempfile.mkdtemp() @@ -258,12 +325,12 @@ def test_imcp_script_shouldPass(move=False): imageHashes = [] - print - print 'files_to_create: ', files_to_create - print 'imcp_args: ', imcp_args - print 'files_to_expect: ', files_to_expect + print() + print('files_to_create: ', files_to_create) + print('imcp_args: ', imcp_args) + print('files_to_expect: ', files_to_expect) - for fname in files_to_create.split(): + for i, fname in enumerate(files_to_create.split()): imageHashes.append(createImageFile(op.join(indir, fname))) imcp_args = imcp_args.split() @@ -271,26 +338,26 @@ def test_imcp_script_shouldPass(move=False): imcp_args[:-1] = [op.join(indir, a) for a in imcp_args[:-1]] imcp_args[ -1] = op.join(outdir, imcp_args[-1]) + print('indir before: ', os.listdir(indir)) + print('outdir before: ', os.listdir(outdir)) if move: immv_script.main(imcp_args) else: imcp_script.main(imcp_args) - checkFilesToExpect(files_to_expect, outdir, outputType, imageHashes) + print('indir after: ', os.listdir(indir)) + print('outdir after: ', os.listdir(outdir)) + + checkFilesToExpect( + files_to_expect, outdir, outputType, imageHashes) if move: infiles = os.listdir(indir) infiles = [f for f in infiles if op.isfile(f)] assert len(infiles) == 0 - for f in os.listdir(outdir): - f = op.join(outdir, f) - if op.isfile(f): - os.remove(f) + cleardir(indir) + cleardir(outdir) - for f in os.listdir(indir): - f = op.join(indir, f) - if op.isfile(f): - os.remove(f) finally: shutil.rmtree(indir) @@ -300,149 +367,248 @@ def test_imcp_script_shouldPass(move=False): def test_imcp_script_shouldFail(move=False): - # - non-existent input - # - destination exists + # - len(srcs) > 1 and dest is not dir # - input not readable # - move=True and input not deleteable - # - destination not writeable - pass + # - ambiguous inputs + # - input is incomplete pair (e.g. .hdr + # without corresponding .img) + + # a.img + # a.hdr + # a.nii + # + # FAIL: imcp a dest + + # (files_to_create, imcp_args[, preproc]) + tests = [ + + # non-existent input + ('', 'a b'), + ('a.img', 'b.img a'), + + # dest is non-existent dir + ('a.img', 'a.img non_existent_dir/'), + + # len(srcs) > 1, but dest is not dir + ('a.img b.img', 'a b c.img'), + + # destination not writeable + ('a.img b.img', 'a b ./readonly_dir', 'mkdir outdir/readonly_dir; chmod a-wx outdir/readonly_dir'), + ('a.img', 'a b', 'mkdir outdir/b.nii.gz; chmod a-wx outdir/b.nii.gz'), + + # input not readable + ('a.img', 'a b', 'chmod a-rwx indir/a.img'), + + # ambiguous input + ('a.img a.nii', 'a b'), + + # input is part of incomplete pair + ('a.img', 'a b', 'rm indir/a.hdr'), + ('a.img', 'a.img b', 'rm indir/a.hdr'), + ('a.img', 'a.hdr b', 'rm indir/a.hdr'), + ('a.img', 'a b', 'rm indir/a.img'), + ('a.img', 'a.img b', 'rm indir/a.img'), + ('a.img', 'a.hdr b', 'rm indir/a.img'), + ] + + if move: + tests = tests + [ + # Input not deletable + ('a.img', 'a b', 'chmod a-wx indir'), + ('a.img', 'a b', 'chmod a-wx indir'), + ('a.nii', 'a b', 'chmod a-wx indir') + ] + + indir = tempfile.mkdtemp() + outdir = tempfile.mkdtemp() + + try: + for test in tests: + + files_to_create = test[0] + imcp_args = test[1] + + if len(test) == 3: preproc = test[2] + else: preproc = None + + files_to_create = files_to_create.split() + imcp_args = imcp_args .split() + + for fname in files_to_create: + createImageFile(op.join(indir, fname)) + + imcp_args[:-1] = [op.join(indir, a) for a in imcp_args[:-1]] + imcp_args[ -1] = op.join(outdir, imcp_args[-1]) + + if preproc is not None: + for cmd in preproc.split(';'): + cmd = cmd.replace('indir', indir).replace('outdir', outdir) + sp.call(cmd.split()) + + try: + if move: immv_script.main(imcp_args) + else: imcp_script.main(imcp_args) + assert False + except (RuntimeError, IOError, fslpath.PathError): + pass + + sp.call('chmod u+rwx {}'.format(indir) .split()) + sp.call('chmod u+rwx {}'.format(outdir).split()) + + cleardir(indir) + cleardir(outdir) + + finally: + shutil.rmtree(indir) + shutil.rmtree(outdir) + def test_immv_script_shouldPass(): test_imcp_script_shouldPass(move=True) + +def test_immv_script_shouldFail(): + test_imcp_script_shouldFail(move=True) + def test_imcp_shouldPass(move=False): + + # + # if not useDefaultExt: + # + # imcp src.img dest -> dest.img + # imcp src.img dest.nii -> dest.nii + # + # if defaultExt: + # imgp src.img dest -> dest.nii.gz + # imgp src.img dest.nii -> dest.nii.gz + # # (files_to_create, - # [( imcp_src, imcp_dest, files_which_should_exist), - # ( imcp_src, imcp_dest, [files_which_should_exist]), - # ([imcp_srcs], imcp_dest, files_which_should_exist), - # ([imcp_srcs], imcp_dest, [files_which_should_exist]), + # [( imcp_args, files_which_should_exist), # ... # ] # ) - # - # if icmp_dest == '', it means to copy to the directory - # files_which_should_exist == 'all' is equivalent to files_which_should_exist == files_to_create shouldPass = [ - (['file.hdr', 'file.img'], [ - ('file', 'file', 'all'), - ('file', 'file.img', 'all'), - ('file', 'file.hdr', 'all'), - ('file', '', 'all'), - ('file.img', 'file', 'all'), - ('file.img', 'file.img', 'all'), - ('file.img', 'file.hdr', 'all'), - ('file.img', '', 'all'), - ('file.hdr', 'file', 'all'), - ('file.hdr', 'file.img', 'all'), - ('file.hdr', 'file.hdr', 'all'), - ('file.hdr', '', 'all'), + ('file.img', [ + ('file file', 'file.img'), + ('file file.img', 'file.img'), + ('file file.hdr', 'file.img'), + ('file .', 'file.img'), + ('file.img file', 'file.img'), + ('file.img file.img', 'file.img'), + ('file.img file.hdr', 'file.img'), + ('file.img .', 'file.img'), + ('file.hdr file', 'file.img'), + ('file.hdr file.img', 'file.img'), + ('file.hdr file.hdr', 'file.img'), + ('file.hdr .', 'file.img'), ]), - (['file.hdr', 'file.img', 'file.blob'], [ - ('file', 'file', ['file.hdr', 'file.img']), - ('file', 'file.img', ['file.hdr', 'file.img']), - ('file', 'file.hdr', ['file.hdr', 'file.img']), - ('file', '', ['file.hdr', 'file.img']), - ('file.img', 'file', ['file.hdr', 'file.img']), - ('file.img', 'file.img', ['file.hdr', 'file.img']), - ('file.img', 'file.hdr', ['file.hdr', 'file.img']), - ('file.img', '', ['file.hdr', 'file.img']), - ('file.hdr', 'file', ['file.hdr', 'file.img']), - ('file.hdr', 'file.img', ['file.hdr', 'file.img']), - ('file.hdr', 'file.hdr', ['file.hdr', 'file.img']), - ('file.hdr', '', ['file.hdr', 'file.img']), + ('file.img file.blob', [ + ('file file', 'file.img'), + ('file file.img', 'file.img'), + ('file file.hdr', 'file.img'), + ('file .', 'file.img'), + ('file.img file', 'file.img'), + ('file.img file.img', 'file.img'), + ('file.img file.hdr', 'file.img'), + ('file.img .', 'file.img'), + ('file.hdr file', 'file.img'), + ('file.hdr file.img', 'file.img'), + ('file.hdr file.hdr', 'file.img'), + ('file.hdr .', 'file.img'), ]), - (['file.hdr', 'file.img', 'file.nii'], [ - ('file.img', 'file', ['file.hdr', 'file.img']), - ('file.img', 'file.img', ['file.hdr', 'file.img']), - ('file.img', 'file.hdr', ['file.hdr', 'file.img']), - ('file.img', '', ['file.hdr', 'file.img']), - ('file.hdr', 'file', ['file.hdr', 'file.img']), - ('file.hdr', 'file.img', ['file.hdr', 'file.img']), - ('file.hdr', 'file.hdr', ['file.hdr', 'file.img']), - ('file.hdr', '', ['file.hdr', 'file.img']), - ('file.nii', 'file', 'file.nii'), - ('file.nii', 'file.nii', 'file.nii'), - ('file.nii', '', 'file.nii'), + ('file.img file.nii', [ + ('file.img file', 'file.img'), + ('file.img file.img', 'file.img'), + ('file.img file.hdr', 'file.img'), + ('file.img .', 'file.img'), + ('file.hdr file', 'file.img'), + ('file.hdr file.img', 'file.img'), + ('file.hdr file.hdr', 'file.img'), + ('file.hdr .', 'file.img'), + ('file.nii file', 'file.nii'), + ('file.nii file.nii', 'file.nii'), + + # TODO both img/nii files ]), - (['file.nii'], [ - ('file', 'file', 'all'), - ('file', 'file.nii', 'all'), - ('file', '', 'all'), - ('file.nii', 'file', 'all'), - ('file.nii', 'file.nii', 'all'), - ('file.nii', '', 'all'), + ('file.nii', [ + ('file file', 'file.nii'), + ('file file.nii', 'file.nii'), + ('file .', 'file.nii'), + ('file.nii file', 'file.nii'), + ('file.nii file.nii', 'file.nii'), + ('file.nii .', 'file.nii'), ]), - (['file.nii.gz'], [ - ('file', 'file', 'all'), - ('file', 'file.nii.gz', 'all'), - ('file', '', 'all'), - ('file.nii.gz', 'file', 'all'), - ('file.nii.gz', 'file.nii.gz', 'all'), - ('file.nii.gz', '', 'all'), + ('file.nii.gz', [ + ('file file', 'file.nii.gz'), + ('file file.nii.gz', 'file.nii.gz'), + ('file .', 'file.nii.gz'), + ('file.nii.gz file', 'file.nii.gz'), + ('file.nii.gz file.nii.gz', 'file.nii.gz'), + ('file.nii.gz .', 'file.nii.gz'), ]), - (['file.nii', 'file.blob'], [ - ('file', 'file', 'file.nii'), - ('file', 'file.nii', 'file.nii'), - ('file', '', 'file.nii'), - ('file.nii', 'file', 'file.nii'), - ('file.nii', 'file.nii', 'file.nii'), - ('file.nii', '', 'file.nii'), + ('file.nii file.blob', [ + ('file file', 'file.nii'), + ('file file.nii', 'file.nii'), + ('file .', 'file.nii'), + ('file.nii file', 'file.nii'), + ('file.nii file.nii', 'file.nii'), + ('file.nii .', 'file.nii'), ]), - (['file.nii', 'file.nii.gz'], [ - ('file.nii', 'file', 'file.nii'), - ('file.nii', 'file.nii', 'file.nii'), - ('file.nii', '', 'file.nii'), - ('file.nii.gz', 'file', 'file.nii.gz'), - ('file.nii.gz', 'file.nii.gz', 'file.nii.gz'), - ('file.nii.gz', '', 'file.nii.gz'), - ]), + ('file.nii file.nii.gz', [ + ('file.nii file', 'file.nii'), + ('file.nii file.nii', 'file.nii'), + ('file.nii .', 'file.nii'), + ('file.nii.gz file', 'file.nii.gz'), + ('file.nii.gz file.nii.gz', 'file.nii.gz'), + ('file.nii.gz .', 'file.nii.gz'), - (['file.hdr', 'file.img', 'file.nii', 'file.nii.gz'], [ - (['file.img', 'file.nii', 'file.nii.gz'], '', 'all'), - ('file.img', '', ['file.hdr', 'file.img']), - (['file.hdr', 'file.img'], '', ['file.hdr', 'file.img']), + # TODO both + ]), - ('file.nii', '', 'file.nii'), - (['file.nii', 'file.nii.gz'], '', ['file.nii', 'file.nii.gz']), + ('file.img file.nii file.nii.gz', [ + ('file.img file.nii file.nii.gz .', 'file.img file.nii file.nii.gz'), + ('file.img .', 'file.img'), + ('file.img .', 'file.img'), + ('file.nii .', 'file.nii'), + ('file.nii file.nii.gz .', 'file.nii file.nii.gz'), ]), - (['001.hdr', '001.img', '002.hdr', '002.img', '003.hdr', '003.img'], [ + ('001.img 002.img 003.img', [ - (['001', '002', '003'], '', 'all'), - (['001.img', '002.img', '003.img'], '', 'all'), - (['001.hdr', '002.hdr', '003.hdr'], '', 'all'), - - (['001.img', '002', '003'], '', 'all'), - (['001.hdr', '002', '003'], '', 'all'), - - (['001.img', '002.hdr', '003.img'], '', 'all'), - (['001.hdr', '002.img', '003.hdr'], '', 'all'), - - (['001', '003'], '', ['001.hdr', '001.img', '003.hdr', '003.img']), - (['001.img', '003.img'], '', ['001.hdr', '001.img', '003.hdr', '003.img']), - (['001.hdr', '003.hdr'], '', ['001.hdr', '001.img', '003.hdr', '003.img']), - - (['001.img', '003'], '', ['001.hdr', '001.img', '003.hdr', '003.img']), - (['001.hdr', '003'], '', ['001.hdr', '001.img', '003.hdr', '003.img']), - - (['001.img', '003.img'], '', ['001.hdr', '001.img', '003.hdr', '003.img']), - (['001.hdr', '003.hdr'], '', ['001.hdr', '001.img', '003.hdr', '003.img']), - - (['001.img', '001.hdr', '002.img', '002.hdr', '003.img', '003.hdr'], '', 'all'), + ('001 002 003 .', '001.img 002.img 003.img'), + ('001.img 002.img 003.img .', '001.img 002.img 003.img'), + ('001.hdr 002.hdr 003.hdr .', '001.img 002.img 003.img'), + + ('001.img 002 003 .', '001.img 002.img 003.img'), + ('001.hdr 002 003 .', '001.img 002.img 003.img'), + + ('001.img 002.hdr 003.img .', '001.img 002.img 003.img'), + ('001.hdr 002.img 003.hdr .', '001.img 002.img 003.img'), + + ('001 003 .', '001.img 003.img'), + ('001.img 003.img .', '001.img 003.img'), + ('001.hdr 003.hdr .', '001.img 003.img'), + + ('001.img 003 .', '001.img 003.img'), + ('001.hdr 003 .', '001.img 003.img'), + + ('001.img 003.img .', '001.img 003.img'), + ('001.hdr 003.hdr .', '001.img 003.img'), ]), ] @@ -453,51 +619,45 @@ def test_imcp_shouldPass(move=False): try: for files_to_create, tests in shouldPass: - - if not isinstance(files_to_create, list): - files_to_create = [files_to_create] - - for imcp_src, imcp_dest, should_exist in tests: - if not isinstance(imcp_src, list): imcp_src = [imcp_src] - if should_exist == 'all': should_exist = list(files_to_create) - elif not isinstance(should_exist, list): should_exist = [should_exist] + files_to_create = files_to_create.split() + + for imcp_args, should_exist in tests: - imcp_dest = op.join(outdir, imcp_dest) + should_exist = should_exist.split() + imcp_args = imcp_args.split() + imcp_srcs = imcp_args[:-1] + imcp_dest = imcp_args[ -1] - # Each input file contains - # its name in plain text, - # so we can verify that the - # files were correctly copied - hashes = [] + hashes = {} for fn in files_to_create: - hashes.append(createImageFile(op.join(indir, fn))) + hashes[fn] = createImageFile(op.join(indir, fn)) print() print('files_to_create: ', files_to_create) - print('imcp_src: ', imcp_src) + print('imcp_srcs: ', imcp_srcs) print('imcp_dest: ', imcp_dest) print('should_exist: ', should_exist) + print('indir: ', os.listdir(indir)) - for src in imcp_src: + for src in imcp_srcs: print(' src: {}'.format(src)) src = op.join(indir, src) - if move: imcp.immv(src, imcp_dest, overwrite=True) - else: imcp.imcp(src, imcp_dest, overwrite=True) - - copied = os.listdir(outdir) - copied = [f for f in copied if op.isfile(op.join(outdir, f))] + if move: imcp.immv(src, op.join(outdir, imcp_dest), overwrite=True) + else: imcp.imcp(src, op.join(outdir, imcp_dest), overwrite=True) - assert sorted(copied) == sorted(should_exist) + print('indir after: ', os.listdir(indir)) + print('outdir after: ', os.listdir(outdir)) - # check file contents - for fn in should_exist: - with open(op.join(outdir, fn), 'rt') as f: - assert f.read() == '{}\n'.format(fn) + # check file contents + checkFilesToExpect(should_exist, + outdir, + None, + hashes) # If move, check that # input files are gone @@ -505,11 +665,11 @@ def test_imcp_shouldPass(move=False): for f in should_exist: assert not op.exists(op.join(indir, f)) - for f in files_to_create: + for f in os.listdir(indir): try: os.remove(op.join(indir, f)) except: pass - for f in should_exist: + for f in os.listdir(outdir): os.remove(op.join(outdir, f))