def test_not_recursive_with_matches(): global tmpdir tmpdir2 = tempfile.mkdtemp(dir=tmpdir) tmpfile = tempfile.NamedTemporaryFile(dir=tmpdir2) pattern = r'(\d[.-])?\d{3,}[.-]\d{3,}[.-]\d{4,}' s = b""" never call the number 1-800-666-1337. instead, you sould call the number 333-3737-13 but actually, that number sucks, so call "9-373-185-7242" or really just hit me up at777.777.7777 """ tmpfile.seek(0) tmpfile.write(s) os.fsync(tmpfile.fileno()) tmpfile.seek(0) regex = re.compile(pattern) matches = list(find_regex_in_directory(regex=regex, directory=tmpdir2, recursive=False, file_patterns=['*'])) assert len(matches) == 3 for m in matches: assert m['match'].re.pattern == pattern if sys.version_info.major > 2: assert m['match'].re.flags == re.UNICODE else: assert m['match'].re.flags == 0 assert matches[0]['match'].group() == '1-800-666-1337' assert matches[1]['match'].group() == '9-373-185-7242' assert matches[2]['match'].group() == '777.777.7777'
def test_recursive_no_file_patterns(): global tmpdir tmpdir2 = tempfile.mkdtemp(dir=tmpdir) tmpfile = tempfile.NamedTemporaryFile(dir=tmpdir2) pattern = r'(\d[.-])?\d{3,}[.-]\d{3,}[.-]\d{4,}' s = b""" never call the number 1-800-666-1337. instead, you sould call the number 333-3737-13 but actually, that number sucks, so call "9-373-185-7242" or really just hit me up at777.777.7777 """ tmpfile.seek(0) tmpfile.write(s) os.fsync(tmpfile.fileno()) tmpfile.seek(0) regex = re.compile(pattern) assert list(find_regex_in_directory(regex=regex, directory=tmpdir, recursive=True, file_patterns=[])) == []
def test_file_patterns_only(): global tmpfile assert list(find_regex_in_directory(file_patterns=[tmpfile.name])) == []
def test_recursive_only(): assert list(find_regex_in_directory(recursive=True)) == []
def test_directory_only(): assert list(find_regex_in_directory(directory=os.path.dirname(__file__))) == []
def test_regex_only(): regex = re.compile('test') assert list(find_regex_in_directory(regex=regex)) == []
def test_no_arguments(): assert list(find_regex_in_directory()) == []