def test_non_unicode_file(): with tempfile.NamedTemporaryFile(mode='wb') as tmpfile: tmpfile.seek(0) tmpfile.write(b'\x80\x02\x7d\x71') os.fsync(tmpfile.fileno()) tmpfile.seek(0) matches = list(find_regex_in_file(regex=re.compile(r''), filename=tmpfile.name)) if sys.version_info[0] >= 3: assert matches == [] else: assert len(matches) > 0
def test_complex_regex(): regex = re.compile(r'(\d[.-])?\d{3,}[.-]\d{3,}[.-]\d{4,}') s = """ 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 """ global tmpfile tmpfile.seek(0) tmpfile.write(s) os.fsync(tmpfile.fileno()) tmpfile.seek(0) matches = list(find_regex_in_file(regex=regex, filename=tmpfile.name)) assert len(matches) == 3 for m in matches: assert m['match'].re.pattern == regex.pattern assert m['match'].re.flags == regex.flags 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_empty_file(): global tmpfile os.fsync(tmpfile.fileno()) tmpfile.seek(0) assert list(find_regex_in_file(regex=re.compile(r''), filename=tmpfile.name)) == []
def test_filename_only(): global tmpfile assert list(find_regex_in_file(filename=tmpfile.name)) == []
def test_regex_only(): regex = re.compile('test') list(find_regex_in_file(regex=regex))
def test_no_arguments(): list(find_regex_in_file())