mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 01:51:46 +04:00
Remove encoding dependence
This commit is contained in:
parent
3793bc32c0
commit
25c06e6525
2 changed files with 21 additions and 4 deletions
|
|
@ -30,10 +30,10 @@ def _process_filename_by_line(pattern, filename):
|
||||||
def _process_filename_at_once(pattern, filename):
|
def _process_filename_at_once(pattern, filename):
|
||||||
retv = 0
|
retv = 0
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
match = pattern.search(f.read().decode('utf-8').replace('\n', ''))
|
match = pattern.search(f.read())
|
||||||
if match:
|
if match:
|
||||||
retv = 1
|
retv = 1
|
||||||
output.write('{}:'.format(filename))
|
output.write('{}:{}-{}:'.format(filename, match.start(), match.end()))
|
||||||
output.write_line(match.group())
|
output.write_line(match.group())
|
||||||
return retv
|
return retv
|
||||||
|
|
||||||
|
|
@ -59,6 +59,9 @@ def main(argv=None):
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
flags = re.IGNORECASE if args.ignore_case else 0
|
flags = re.IGNORECASE if args.ignore_case else 0
|
||||||
|
if args.null_data:
|
||||||
|
flags = flags | re.MULTILINE | re.DOTALL
|
||||||
|
|
||||||
pattern = re.compile(args.pattern.encode(), flags)
|
pattern = re.compile(args.pattern.encode(), flags)
|
||||||
|
|
||||||
retv = 0
|
retv = 0
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,21 @@ def test_ignore_case(some_files, cap_out):
|
||||||
|
|
||||||
|
|
||||||
def test_null_data(some_files, cap_out):
|
def test_null_data(some_files, cap_out):
|
||||||
ret = pygrep.main(('--null-data', r'foo.*bar', 'f1', 'f2', 'f3'))
|
ret = pygrep.main(('--null-data', r'foo\nbar', 'f1', 'f2', 'f3'))
|
||||||
out = cap_out.get()
|
out = cap_out.get()
|
||||||
assert ret == 1
|
assert ret == 1
|
||||||
assert out == 'f1:foobar\n'
|
assert out == 'f1:0-7:foo\nbar\n'
|
||||||
|
|
||||||
|
|
||||||
|
def test_null_data_dotall_flag_is_enabled(some_files, cap_out):
|
||||||
|
ret = pygrep.main(('--null-data', r'o.*bar', 'f1', 'f2', 'f3'))
|
||||||
|
out = cap_out.get()
|
||||||
|
assert ret == 1
|
||||||
|
assert out == 'f1:1-7:oo\nbar\n'
|
||||||
|
|
||||||
|
|
||||||
|
def test_null_data_multiline_flag_is_enabled(some_files, cap_out):
|
||||||
|
ret = pygrep.main(('--null-data', r'foo$.*bar', 'f1', 'f2', 'f3'))
|
||||||
|
out = cap_out.get()
|
||||||
|
assert ret == 1
|
||||||
|
assert out == 'f1:0-7:foo\nbar\n'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue