|
| 1 | +"""Test bids_validator.bidsignore.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from bids_validator.bidsignore import Ignore, compile_pat, filter_file_tree |
| 6 | +from bids_validator.types.files import FileTree |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize( |
| 10 | + ('pattern', 'hits', 'misses'), |
| 11 | + [ |
| 12 | + ('/', ['/'], ['dir/', 'file']), |
| 13 | + # Match file or directory named foo |
| 14 | + ('foo', ['foo', 'foo/', 'bar/foo', 'bar/foo/'], ['bar', 'foobar', 'barfoo', 'barfoo/']), |
| 15 | + # Directories named foo only |
| 16 | + ('foo/', ['foo/', 'bar/foo/'], ['foo', 'bar/foo', 'bar', 'foobar', 'barfoo', 'barfoo/']), |
| 17 | + # Files or directories at the root |
| 18 | + ('/foo', ['foo', 'foo/'], ['bar/foo', 'bar/foo/', 'bar', 'foobar', 'barfoo', 'barfoo/']), |
| 19 | + # doc/frotz/ examples from GITIGNORE(5) |
| 20 | + ('doc/frotz/', ['doc/frotz/'], ['a/doc/frotz/']), |
| 21 | + ('frotz/', ['frotz/', 'doc/frotz/', 'a/doc/frotz/'], []), |
| 22 | + # * matches everything because everything has a basename |
| 23 | + ('*', ['foo', 'foo/', 'foo/bar', 'foo/bar/'], []), |
| 24 | + # *o matches things with basename ending in o |
| 25 | + ('*o', ['foo', 'foo/', 'bar/foo', 'bar/foo/'], ['foo/bar', 'foo/bar/']), |
| 26 | + # Leading **/ matches in all directories |
| 27 | + ('**/foo', ['foo', 'foo/', 'bar/foo', 'bar/foo/'], ['foo/bar', 'foo/bar/', 'baz/foobar']), |
| 28 | + ('**/foo/bar', ['foo/bar', 'foo/bar/', 'a/foo/bar'], ['foo/', 'bar/foo', 'bar']), |
| 29 | + # Trailing /** matches everything inside a root-relative directory |
| 30 | + ('foo/**', ['foo/', 'foo/x', 'foo/x/y/z'], ['foo', 'bar/foo/x/y/z']), |
| 31 | + # /**/ matches zero or more directories |
| 32 | + ('a/**/b', ['a/b', 'a/x/b', 'a/x/y/b'], ['x/a/b', 'x/a/y/b']), |
| 33 | + # ** surrounded by something other than slashes acts like a regular * |
| 34 | + ('a/x**/b', ['a/x/b', 'a/xy/b'], ['x/a/b', 'x/a/y/b', 'a/x/y/b']), |
| 35 | + # Escaped special prefixes |
| 36 | + (r'\#*', ['#', '#foo'], ['foo', 'bar#']), |
| 37 | + (r'\!*', ['!', '!foo'], ['foo', 'bar!']), |
| 38 | + ], |
| 39 | +) |
| 40 | +def test_patterns(pattern, hits, misses): |
| 41 | + """Test expected hits and misses of ignore patterns.""" |
| 42 | + regex = compile_pat(pattern) |
| 43 | + for fname in hits: |
| 44 | + assert regex.match(fname) |
| 45 | + for fname in misses: |
| 46 | + assert not regex.match(fname) |
| 47 | + |
| 48 | + |
| 49 | +def test_skipped_patterns(): |
| 50 | + """Test ignore patterns that should match nothing.""" |
| 51 | + assert compile_pat('') is None |
| 52 | + assert compile_pat('# commented line') is None |
| 53 | + assert compile_pat(' ') is None |
| 54 | + with pytest.raises(ValueError, match='Inverted patterns not supported'): |
| 55 | + compile_pat('!inverted pattern') |
| 56 | + |
| 57 | + |
| 58 | +def test_Ignore_ds000117(examples): |
| 59 | + """Test that we can load a .bidsignore file and match a file.""" |
| 60 | + ds000117 = FileTree.read_from_filesystem(examples / 'ds000117') |
| 61 | + ignore = Ignore.from_file(ds000117.children['.bidsignore']) |
| 62 | + assert 'run-*_echo-*_FLASH.json' in ignore.patterns |
| 63 | + assert 'sub-01/ses-mri/anat/sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz' in ds000117 |
| 64 | + assert ignore.match('sub-01/ses-mri/anat/sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz') |
| 65 | + assert not ignore.match('acq-mprage_T1w.json') |
| 66 | + flash_file = ( |
| 67 | + ds000117.children['sub-01'] |
| 68 | + .children['ses-mri'] |
| 69 | + .children['anat'] |
| 70 | + .children['sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz'] |
| 71 | + ) |
| 72 | + assert ignore.match(flash_file.relative_path) |
| 73 | + |
| 74 | + |
| 75 | +def test_filter_file_tree(examples): |
| 76 | + """Test file tree filtering with .bidsignore.""" |
| 77 | + ds000117 = FileTree.read_from_filesystem(examples / 'ds000117') |
| 78 | + assert '.bidsignore' in ds000117 |
| 79 | + assert 'sub-01/ses-mri/anat/sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz' in ds000117 |
| 80 | + |
| 81 | + filtered = filter_file_tree(ds000117) |
| 82 | + assert '.bidsignore' not in filtered |
| 83 | + assert 'sub-01/ses-mri/anat/sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz' not in filtered |
| 84 | + |
| 85 | + ds000247 = FileTree.read_from_filesystem(examples / 'ds000247') |
| 86 | + assert '.bidsignore' not in ds000247 |
| 87 | + |
| 88 | + filtered = filter_file_tree(ds000247) |
| 89 | + assert filtered is ds000247 |
0 commit comments