|
| 1 | +"""Test bids_validator.bidsignore.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from bids_validator.bidsignore import Ignore, compile_pat, filter_file_tree |
| 8 | +from bids_validator.types.files import FileTree |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + ('pattern', 'hits', 'misses'), |
| 13 | + [ |
| 14 | + ('/', ['/'], ['dir/', 'file']), |
| 15 | + # Match file or directory named foo |
| 16 | + ('foo', ['foo', 'foo/', 'bar/foo', 'bar/foo/'], ['bar', 'foobar', 'barfoo', 'barfoo/']), |
| 17 | + # Directories named foo only |
| 18 | + ('foo/', ['foo/', 'bar/foo/'], ['foo', 'bar/foo', 'bar', 'foobar', 'barfoo', 'barfoo/']), |
| 19 | + # Files or directories at the root |
| 20 | + ('/foo', ['foo', 'foo/'], ['bar/foo', 'bar/foo/', 'bar', 'foobar', 'barfoo', 'barfoo/']), |
| 21 | + # doc/frotz/ examples from GITIGNORE(5) |
| 22 | + ('doc/frotz/', ['doc/frotz/'], ['a/doc/frotz/']), |
| 23 | + ('frotz/', ['frotz/', 'doc/frotz/', 'a/doc/frotz/'], []), |
| 24 | + # * matches everything because everything has a basename |
| 25 | + ('*', ['foo', 'foo/', 'foo/bar', 'foo/bar/'], []), |
| 26 | + # *o matches things with basename ending in o, including parent directories |
| 27 | + ('*o', ['foo', 'foo/', 'bar/foo', 'bar/foo/', 'foo/bar'], ['bar', 'bar/baz', 'bar/bar/']), |
| 28 | + # Leading **/ matches in all directories |
| 29 | + ( |
| 30 | + '**/foo', |
| 31 | + ['foo', 'foo/', 'bar/foo', 'bar/foo/', 'foo/bar'], |
| 32 | + ['foobar/baz', 'foobar/baz/', 'baz/foobar', 'baz/barfoo'], |
| 33 | + ), |
| 34 | + ('**/foo/bar', ['foo/bar', 'foo/bar/', 'a/foo/bar'], ['foo/', 'bar/foo', 'bar']), |
| 35 | + # Trailing /** matches everything inside a root-relative directory |
| 36 | + ('foo/**', ['foo/', 'foo/x', 'foo/x/y/z'], ['foo', 'bar/foo/x/y/z']), |
| 37 | + # /**/ matches zero or more directories |
| 38 | + ('a/**/b', ['a/b', 'a/x/b', 'a/x/y/b'], ['x/a/b', 'x/a/y/b']), |
| 39 | + # ** surrounded by something other than slashes acts like a regular * |
| 40 | + ('a/x**/b', ['a/x/b', 'a/xy/b'], ['x/a/b', 'x/a/y/b', 'a/x/y/b']), |
| 41 | + # Escaped special prefixes |
| 42 | + (r'\#*', ['#', '#foo'], ['foo', 'bar#']), |
| 43 | + (r'\!*', ['!', '!foo'], ['foo', 'bar!']), |
| 44 | + ], |
| 45 | +) |
| 46 | +def test_patterns(pattern, hits, misses): |
| 47 | + """Test expected hits and misses of ignore patterns.""" |
| 48 | + regex = compile_pat(pattern) |
| 49 | + for fname in hits: |
| 50 | + assert regex.match(fname), f'"{fname}" should match "{pattern}"' |
| 51 | + for fname in misses: |
| 52 | + assert not regex.match(fname), f'"{fname}" should not match "{pattern}"' |
| 53 | + |
| 54 | + |
| 55 | +def test_skipped_patterns(): |
| 56 | + """Test ignore patterns that should match nothing.""" |
| 57 | + assert compile_pat('') is None |
| 58 | + assert compile_pat('# commented line') is None |
| 59 | + assert compile_pat(' ') is None |
| 60 | + with pytest.raises(ValueError, match='Inverted patterns not supported'): |
| 61 | + compile_pat('!inverted pattern') |
| 62 | + |
| 63 | + |
| 64 | +def test_Ignore_ds000117(examples): |
| 65 | + """Test that we can load a .bidsignore file and match a file.""" |
| 66 | + ds000117 = FileTree.read_from_filesystem(examples / 'ds000117') |
| 67 | + ignore = Ignore.from_file(ds000117.children['.bidsignore']) |
| 68 | + assert 'run-*_echo-*_FLASH.json' in ignore.patterns |
| 69 | + assert 'sub-01/ses-mri/anat/sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz' in ds000117 |
| 70 | + assert ignore.match('sub-01/ses-mri/anat/sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz') |
| 71 | + assert not ignore.match('acq-mprage_T1w.json') |
| 72 | + flash_file = ( |
| 73 | + ds000117.children['sub-01'] |
| 74 | + .children['ses-mri'] |
| 75 | + .children['anat'] |
| 76 | + .children['sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz'] |
| 77 | + ) |
| 78 | + assert ignore.match(flash_file.relative_path) |
| 79 | + |
| 80 | + |
| 81 | +def test_filter_file_tree(examples): |
| 82 | + """Test file tree filtering with .bidsignore.""" |
| 83 | + ds000117 = FileTree.read_from_filesystem(examples / 'ds000117') |
| 84 | + assert '.bidsignore' in ds000117 |
| 85 | + assert 'sub-01/ses-mri/anat/sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz' in ds000117 |
| 86 | + |
| 87 | + filtered = filter_file_tree(ds000117) |
| 88 | + assert '.bidsignore' not in filtered |
| 89 | + assert 'sub-01/ses-mri/anat/sub-01_ses-mri_run-1_echo-1_FLASH.nii.gz' not in filtered |
| 90 | + |
| 91 | + ds000247 = FileTree.read_from_filesystem(examples / 'ds000247') |
| 92 | + assert '.bidsignore' not in ds000247 |
| 93 | + |
| 94 | + filtered = filter_file_tree(ds000247) |
| 95 | + assert filtered is ds000247 |
| 96 | + |
| 97 | + |
| 98 | +def _walk(tree: FileTree): |
| 99 | + for child in tree.children.values(): |
| 100 | + if child.is_dir: |
| 101 | + yield from _walk(child) |
| 102 | + else: |
| 103 | + yield child |
| 104 | + |
| 105 | + |
| 106 | +def test_gitignore_battery(gitignore_test): |
| 107 | + """Test our implementation against a gitignore battery.""" |
| 108 | + filetree = FileTree.read_from_filesystem(gitignore_test) |
| 109 | + ignore = Ignore.from_file(filetree.children['.gitignore']) |
| 110 | + # Remove inverted patterns |
| 111 | + ignore.patterns = [patt for patt in ignore.patterns if not patt.startswith('!')] |
| 112 | + |
| 113 | + expected_failures = Ignore( |
| 114 | + [ |
| 115 | + '.git*', # Ignore .git/, .gitignore, etc |
| 116 | + 'README.md', # README is an exception |
| 117 | + # Inverted patterns are not supported |
| 118 | + 'foo*.html', |
| 119 | + '/log/foo.log', |
| 120 | + 'findthis*', |
| 121 | + # Nested gitignore swaps expectations for all files |
| 122 | + 'git-sample-3/', |
| 123 | + # Inversions in nested gitignores |
| 124 | + 'arch/foo/kernel/vmlinux*', |
| 125 | + 'htmldoc/*.html', |
| 126 | + ] |
| 127 | + ) |
| 128 | + |
| 129 | + for file in _walk(filetree): |
| 130 | + if expected_failures.match(file.relative_path): |
| 131 | + continue |
| 132 | + if ignore.match(file.relative_path): |
| 133 | + assert Path(file).read_text().strip() == 'foo: FAIL', ( |
| 134 | + f'{file.relative_path} should have failed' |
| 135 | + ) |
| 136 | + else: |
| 137 | + assert Path(file).read_text().strip() == 'foo: OK', ( |
| 138 | + f'{file.relative_path} should have passed' |
| 139 | + ) |
0 commit comments