def test_extend_patterns(): ignore = jujuignore.JujuIgnore(["foo"]) assert ignore.match("foo", is_dir=False) assert not ignore.match("bar", is_dir=False) ignore.extend_patterns(["bar"]) assert ignore.match("foo", is_dir=False) assert ignore.match("bar", is_dir=False)
def test_jujuignore_multiple_doublestar(): ignore = jujuignore.JujuIgnore(['foo/**/bar/**/baz']) assert ignore.match('/foo/1/2/bar/baz', is_dir=True) assert ignore.match('/foo/1/2/bar/1/2/baz', is_dir=True) assert ignore.match('/foo/bar/1/2/baz', is_dir=True) assert ignore.match('/foo/1/bar/baz', is_dir=True) assert ignore.match('/foo/bar/baz', is_dir=True)
def test_extend_patterns(): ignore = jujuignore.JujuIgnore(['foo']) assert ignore.match('foo', is_dir=False) assert not ignore.match('bar', is_dir=False) ignore.extend_patterns(['bar']) assert ignore.match('foo', is_dir=False) assert ignore.match('bar', is_dir=False)
def test_jujuignore_only_root_target_file_or_dir(): ignore = jujuignore.JujuIgnore(['/target']) assert ignore.match('/target', is_dir=True) assert not ignore.match('/foo/target', is_dir=True) assert not ignore.match('/foo/1target', is_dir=True) assert not ignore.match('/foo/target', is_dir=False) assert ignore.match('/target', is_dir=False)
def assertMatchedAndNonMatched(globs, matched, unmatched, skip_git=False): """For a given set of globs, check that it does and doesn't match as expected""" ignore = jujuignore.JujuIgnore(globs) for m in matched: assert ignore.match(m, is_dir=False), '{} should have matched'.format(m) for m in unmatched: assert not ignore.match( m, is_dir=False), '{} should not have matched'.format(m) if skip_git: return with tempfile.TemporaryDirectory() as tmpdir: subprocess.run(['git', 'init', tmpdir], check=True) with open(str(pathlib.Path(tmpdir) / '.gitignore'), 'wt') as gitignore: gitignore.writelines([g + '\n' for g in globs]) input = ''.join([m.lstrip('/') + '\n' for m in matched + unmatched]) check = True if len(matched) == 0: # We don't check git return value because it returns nonzero if no paths match check = False p = subprocess.run(['git', 'check-ignore', '--stdin'], check=check, input=input, stdout=subprocess.PIPE, cwd=tmpdir, universal_newlines=True) matched_out = p.stdout.splitlines() assert sorted(matched) == sorted(matched_out),\ 'expected exactly {} to match not {}'.format(matched, matched_out)
def test_jujuignore_all_py_files_in_foo(): ignore = jujuignore.JujuIgnore(['/foo/*.py']) assert not ignore.match('/target.py', is_dir=True) assert not ignore.match('/target.py', is_dir=False) assert ignore.match('/foo/target.py', is_dir=True) assert ignore.match('/foo/target.py', is_dir=False) assert not ignore.match('/foo/sub/target.py', is_dir=False)
def test_jujuignore_subdirectory_match(): ignore = jujuignore.JujuIgnore(["apps/logs/"]) assert not ignore.match("/logs", is_dir=True) assert not ignore.match("/apps", is_dir=True) assert ignore.match("/apps/logs", is_dir=True) assert not ignore.match("/apps/logs", is_dir=False) assert not ignore.match("/apps/foo/logs", is_dir=True)
def test_jujuignore_only_root_target_dir(): ignore = jujuignore.JujuIgnore(["/target/"]) assert ignore.match("/target", is_dir=True) assert not ignore.match("/foo/target", is_dir=True) assert not ignore.match("/foo/1target", is_dir=True) assert not ignore.match("/foo/target", is_dir=False) assert not ignore.match("/target", is_dir=False)
def test_jujuignore_subdirectory_match(): ignore = jujuignore.JujuIgnore(['apps/logs/']) assert not ignore.match('/logs', is_dir=True) assert not ignore.match('/apps', is_dir=True) assert ignore.match('/apps/logs', is_dir=True) assert not ignore.match('/apps/logs', is_dir=False) assert not ignore.match('/apps/foo/logs', is_dir=True)
def test_jujuignore_simple_match(): ignore = jujuignore.JujuIgnore([ r"foo", ]) assert ignore.match("/foo", is_dir=True) assert ignore.match("/bar/foo", is_dir=True) assert not ignore.match("/bar/bfoo", is_dir=True) assert not ignore.match("/bfoo", is_dir=True)
def test_jujuignore_bracket(): ignore = jujuignore.JujuIgnore([ r"*.py[cod]", ]) assert not ignore.match("foo.py", is_dir=False) assert ignore.match("foo.pyc", is_dir=False) assert ignore.match("foo.pyo", is_dir=False) assert ignore.match("foo.pyd", is_dir=False)
def test_jujuignore_trim_unescaped_trailing_space(): ignore = jujuignore.JujuIgnore([ r"foo ", r"bar\ \ ", ]) assert ignore.match("/foo", is_dir=True) assert not ignore.match("/bar", is_dir=True) assert ignore.match("/bar ", is_dir=True)
def test_jujuignore_paths_with_newlines(): ignore = jujuignore.JujuIgnore([ r'bar/**/*.py' ]) assert ignore.match('/bar/foo.py', is_dir=False) assert ignore.match('/bar/f\noo.py', is_dir=False) assert ignore.match('/bar/baz/f\noo.py', is_dir=False) assert ignore.match('/bar/b\nz/f\noo.py', is_dir=False)
def test_jujuignore_bracket(): ignore = jujuignore.JujuIgnore([ r'*.py[cod]', ]) assert not ignore.match('foo.py', is_dir=False) assert ignore.match('foo.pyc', is_dir=False) assert ignore.match('foo.pyo', is_dir=False) assert ignore.match('foo.pyd', is_dir=False)
def test_jujuignore_simple_match(): ignore = jujuignore.JujuIgnore([ r'foo', ]) assert ignore.match('/foo', is_dir=True) assert ignore.match('/bar/foo', is_dir=True) assert not ignore.match('/bar/bfoo', is_dir=True) assert not ignore.match('/bfoo', is_dir=True)
def test_jujuignore_trim_unescaped_trailing_space(): ignore = jujuignore.JujuIgnore([ r'foo ', r'bar\ \ ', ]) assert ignore.match('/foo', is_dir=True) assert not ignore.match('/bar', is_dir=True) assert ignore.match('/bar ', is_dir=True)
def test_jujuignore_escaped_bang(): ignore = jujuignore.JujuIgnore([ r'foo', r'!/foo', r'\!foo', ]) assert not ignore.match('/foo', is_dir=True) assert ignore.match('/bar/foo', is_dir=True) assert ignore.match('!foo', is_dir=True)
def test_jujuignore_star_match(): ignore = jujuignore.JujuIgnore([ r'foo*.py', ]) assert ignore.match('/foo.py', is_dir=False) assert ignore.match('/foo2.py', is_dir=False) assert ignore.match('/foobar.py', is_dir=False) assert ignore.match('/bar/foo.py', is_dir=False) assert not ignore.match('/foo/2.py', is_dir=False)
def test_jujuignore_escaped_bang(): ignore = jujuignore.JujuIgnore([ r"foo", r"!/foo", r"\!foo", ]) assert not ignore.match("/foo", is_dir=True) assert ignore.match("/bar/foo", is_dir=True) assert ignore.match("!foo", is_dir=True)
def test_jujuignore_all_py_files(): ignore = jujuignore.JujuIgnore(["*.py"]) assert not ignore.match("/target", is_dir=True) assert ignore.match("/target.py", is_dir=True) assert ignore.match("/target.py", is_dir=False) assert ignore.match("/foo/target.py", is_dir=True) assert ignore.match("/foo/target.py", is_dir=False) assert not ignore.match("/target.pyT", is_dir=True) assert not ignore.match("/target.pyT", is_dir=False)
def test_from_file(): content = io.StringIO(textwrap.dedent("""\ foo /bar """)) ignore = jujuignore.JujuIgnore(content) assert ignore.match("foo", is_dir=False) assert ignore.match("/foo", is_dir=False) assert ignore.match("/bar", is_dir=False) assert not ignore.match("/foo/bar", is_dir=False)
def test_jujuignore_everything_under_foo_but_readme(): ignore = jujuignore.JujuIgnore([ 'foo/**', '!foo/README.md', ]) assert not ignore.match('/foo', is_dir=True) assert ignore.match('/foo/a', is_dir=True) assert ignore.match('/foo/a', is_dir=False) assert ignore.match('/foo/a/b', is_dir=False) assert not ignore.match('/foo/README.md', is_dir=False)
def test_jujuignore_escaped_comment(): ignore = jujuignore.JujuIgnore([ "\\#comment", "target", ]) assert ignore.match("/target", is_dir=True) assert ignore.match("/#comment", is_dir=False) assert ignore.match("#comment", is_dir=False) assert not ignore.match("/foo/comment", is_dir=False) assert ignore.match("/foo/#comment", is_dir=False)
def test_jujuignore_escaped_comment(): ignore = jujuignore.JujuIgnore([ '\\#comment', 'target', ]) assert ignore.match('/target', is_dir=True) assert ignore.match('/#comment', is_dir=False) assert ignore.match('#comment', is_dir=False) assert not ignore.match('/foo/comment', is_dir=False) assert ignore.match('/foo/#comment', is_dir=False)
def test_jujuignore_everything_under_foo_but_readme(): ignore = jujuignore.JujuIgnore([ "foo/**", "!foo/README.md", ]) assert not ignore.match("/foo", is_dir=True) assert ignore.match("/foo/a", is_dir=True) assert ignore.match("/foo/a", is_dir=False) assert ignore.match("/foo/a/b", is_dir=False) assert not ignore.match("/foo/README.md", is_dir=False)
def test_from_file(): content = io.StringIO(textwrap.dedent('''\ foo /bar ''')) ignore = jujuignore.JujuIgnore(content) assert ignore.match('foo', is_dir=False) assert ignore.match('/foo', is_dir=False) assert ignore.match('/bar', is_dir=False) assert not ignore.match('/foo/bar', is_dir=False)
def test_jujuignore_leading_whitespace(): ignore = jujuignore.JujuIgnore([ r'foo', r' bar', r' #comment', ]) assert ignore.match('/foo', is_dir=True) assert ignore.match('/bar', is_dir=True) assert ignore.match('/bar/foo', is_dir=True) assert not ignore.match('#comment', is_dir=True) assert not ignore.match('comment', is_dir=True)
def test_jujuignore_leading_whitespace(): ignore = jujuignore.JujuIgnore([ r"foo", r" bar", r" #comment", ]) assert ignore.match("/foo", is_dir=True) assert ignore.match("/bar", is_dir=True) assert ignore.match("/bar/foo", is_dir=True) assert not ignore.match("#comment", is_dir=True) assert not ignore.match("comment", is_dir=True)
def test_jujuignore_negation(): ignore = jujuignore.JujuIgnore([ "*.py", "!foo.py", "!!!bar.py", ]) assert not ignore.match("bar.py", is_dir=False) assert not ignore.match("foo.py", is_dir=False) assert ignore.match("baz.py", is_dir=False) assert not ignore.match("foo/bar.py", is_dir=False) assert not ignore.match("foo/foo.py", is_dir=False) assert ignore.match("foo/baz.py", is_dir=False)
def test_jujuignore_negation(): ignore = jujuignore.JujuIgnore([ '*.py', '!foo.py', '!!!bar.py', ]) assert not ignore.match('bar.py', is_dir=False) assert not ignore.match('foo.py', is_dir=False) assert ignore.match('baz.py', is_dir=False) assert not ignore.match('foo/bar.py', is_dir=False) assert not ignore.match('foo/foo.py', is_dir=False) assert ignore.match('foo/baz.py', is_dir=False)