Commit 28a0c466 authored by Kai Willadsen's avatar Kai Willadsen

Merge branch 'dirdiff._files_same.need_contents' into 'master'

files_same.need_contents - When `ignore blank line` retain content for rematch after remove blank lines See merge request !19
parents bfb0eed3 489a400f
Pipeline #19034 passed with stages
in 9 minutes 0 seconds
......@@ -111,8 +111,9 @@ def _files_same(files, regexes, comparison_args):
shallow_comparison = comparison_args['shallow-comparison']
time_resolution_ns = comparison_args['time-resolution']
ignore_blank_lines = comparison_args['ignore_blank_lines']
apply_text_filters = comparison_args['apply-text-filters']
need_contents = comparison_args['apply-text-filters']
need_contents = ignore_blank_lines or apply_text_filters
# If all entries are directories, they are considered to be the same
if all([stat.S_ISDIR(s.mode) for s in stats]):
......@@ -189,7 +190,9 @@ def _files_same(files, regexes, comparison_args):
else:
contents = [b"\n".join(c.splitlines()) for c in contents]
contents = [misc.apply_text_filters(c, regexes) for c in contents]
if apply_text_filters:
contents = [misc.apply_text_filters(c, regexes) for c in contents]
result = SameFiltered if all_same(contents) else Different
_cache[cache_key] = CacheResult(stats, result)
......
from os import mkdir, path
CHUNK_SIZE = 4096
diff_definition = {
'a': {
'a.txt': lambda: b'',
'c': {
'c.txt': lambda: b''
},
'd': {
'd.txt': lambda: (b'd' * CHUNK_SIZE) + b'd'
},
'e': {
'f': {},
'g': {
'g.txt': lambda: b'g'
},
'h': {
'h.txt': lambda: b'h'
},
'e.txt': lambda: b''
},
'crlf.txt': lambda: b'foo\r\nbar\r\n',
'crlftrailing.txt': lambda: b'foo\r\nbar\r\n\r\n',
},
'b': {
'b.txt': lambda: b'',
'c': {
'c.txt': lambda: b''
},
'd': {
'd.txt': lambda: (b'd' * CHUNK_SIZE) + b'd',
'd.1.txt': lambda: (b'D' * CHUNK_SIZE) + b'D',
'd.2.txt': lambda: (b'd' * CHUNK_SIZE) + b'D'
},
'e': {
'f': {
'f.txt': lambda: b''
},
'g': {
'g.txt': lambda: b''
},
'h': {
'h.txt': lambda: b'h'
},
'e.txt': lambda: b''
},
'lf.txt': lambda: b'foo\nbar\n',
'lftrailing.txt': lambda: b'foo\nbar\n\n',
}
}
CUR_DIR = path.dirname(__file__)
ROOT_DIR = path.join(CUR_DIR, 'diffs')
def make(definition=diff_definition, root_dir=ROOT_DIR):
if not path.exists(root_dir):
mkdir(root_dir, 0o755)
for k, v in definition.items():
file_path = path.join(root_dir, k)
if isinstance(v, dict):
make(v, file_path)
else:
with open(file_path, 'bw') as open_file:
open_file.write(v())
if __name__ == '__main__':
make()
import pytest
from enum import Enum
from os import path
from meld.dirdiff import _files_same
from .fixture import make
DiffResult = Enum('DiffResult', 'Same SameFiltered DodgySame DodgyDifferent Different FileError')
@pytest.fixture
def differnt_dirs():
make()
def abspath(*args):
d = path.dirname(__file__)
return list(path.join(d, arg) for arg in args)
cmp_args = {
'shallow-comparison': False,
'time-resolution': 10000000000,
'ignore_blank_lines': True,
'apply-text-filters': True
}
no_ignore_args = dict(cmp_args)
no_ignore_args['ignore_blank_lines'] = False
no_ignore_args['apply-text-filters'] = False
dodgy_args = dict(cmp_args)
dodgy_args['shallow-comparison'] = True
@pytest.mark.parametrize('files, regexes, comparison_args, expected', [
# empty file list
((), [], cmp_args, DiffResult.Same),
# dirs are same
(('diffs/a', 'diffs/b'), [], cmp_args, DiffResult.Same),
# dir and file ar diffent
(('diffs/a', 'diffs/b/b.txt'), [], cmp_args, DiffResult.Different),
# shallow equal (time + size)
(('diffs/a/d/d.txt', 'diffs/b/d/d.1.txt'), [], dodgy_args, DiffResult.DodgySame),
# empty files (fastest equal, wont read files)
(('diffs/a/c/c.txt', 'diffs/b/c/c.txt'), [], cmp_args, DiffResult.Same),
# 4.1kb vs 4.1kb file (slow equal, read both until end)
(('diffs/a/d/d.txt', 'diffs/b/d/d.txt'), [], cmp_args, DiffResult.Same),
# 4.1kb vs 4.1kb file (fast different, first chunk diff)
(('diffs/a/d/d.txt', 'diffs/b/d/d.1.txt'), [], cmp_args, DiffResult.Different),
# 4.1kb vs 4.1kb file (slow different, read both until end)
(('diffs/a/d/d.txt', 'diffs/b/d/d.2.txt'), [], cmp_args, DiffResult.Different),
# empty vs 1b file (fast different, first chunk diff)
(('diffs/a/e/g/g.txt', 'diffs/b/e/g/g.txt'), [], cmp_args, DiffResult.Different),
# CRLF vs CRLF with trailing, ignoring blank lines
(('diffs/a/crlf.txt', 'diffs/a/crlftrailing.txt'), [], cmp_args, DiffResult.SameFiltered),
# CRLF vs CRLF with trailing, not ignoring blank lines
(('diffs/a/crlf.txt', 'diffs/a/crlftrailing.txt'), [], no_ignore_args, DiffResult.Different),
# LF vs LF with trailing, ignoring blank lines
(('diffs/b/lf.txt', 'diffs/b/lftrailing.txt'), [], cmp_args, DiffResult.SameFiltered),
# LF vs LF with trailing, not ignoring blank lines
(('diffs/b/lf.txt', 'diffs/b/lftrailing.txt'), [], no_ignore_args, DiffResult.Different),
# CRLF vs LF, ignoring blank lines
(('diffs/a/crlf.txt', 'diffs/b/lf.txt'), [], cmp_args, DiffResult.SameFiltered),
# CRLF vs LF, not ignoring blank lines
(('diffs/a/crlf.txt', 'diffs/b/lf.txt'), [], no_ignore_args, DiffResult.Different),
# CRLF with trailing vs LF with trailing, ignoring blank lines
(('diffs/a/crlftrailing.txt', 'diffs/b/lftrailing.txt'), [], cmp_args, DiffResult.SameFiltered),
# CRLF with trailing vs LF with trailing, not ignoring blank lines
(('diffs/a/crlftrailing.txt', 'diffs/b/lftrailing.txt'), [], no_ignore_args, DiffResult.Different),
])
def test_files_same(files, regexes, comparison_args, expected, differnt_dirs):
files_path = abspath(*files)
result = _files_same(files_path, regexes, comparison_args)
actual = DiffResult(result + 1)
assert actual == expected
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment