def test_read_stream_header(self): stream = cdiff.PatchStream(Sequential([])) self.assertEqual(stream.read_stream_header(1), []) items = ['hello', 'world', 'again'] stream = cdiff.PatchStream(Sequential(items)) self.assertEqual(stream.read_stream_header(2), items[:2]) stream = cdiff.PatchStream(Sequential(items)) self.assertEqual(stream.read_stream_header(4), items[:])
def show_diff(before_editing, after_editing): """Shows a diff between two strings. If the output is to a tty the diff will be colored. Inputs are expected to be unicode strings. """ def listify(string): return [l + '\n' for l in string.rstrip('\n').split('\n')] unified_diff = difflib.unified_diff(listify(before_editing), listify(after_editing)) if sys.stdout.isatty(): buf = io.StringIO() for line in unified_diff: # Force cast to unicode as difflib on Python 2.7 returns a mix of unicode and str. buf.write(text_type(line)) buf.seek(0) class opts: side_by_side = False width = 80 tab_width = 8 cdiff.markup_to_pager(cdiff.PatchStream(buf), opts) else: for line in unified_diff: click.echo(line.rstrip('\n'))
def test_parse_binary_differ_git(self): patch = """\ diff --git a/foo b/foo index 529d8a3..ad71911 100755 --- a/foo +++ b/foo @@ -1,2 +1,2 @@ -foo +bar common diff --git a/example.pdf b/example.pdf index 1eacfd8..3696851 100644 Binary files a/example.pdf and b/example.pdf differ diff --git a/bar b/bar index 529e8a3..ad71921 100755 --- a/bar +++ b/bar @@ -1,2 +1,2 @@ -foo +bar common """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) out = list(parser.get_diff_generator()) self.assertEqual(len(out), 3) self.assertEqual(len(out[1]._hunks), 0) self.assertEqual(out[1]._old_path, '') self.assertEqual(out[1]._new_path, '') self.assertEqual(len(out[1]._headers), 3) self.assertTrue(out[1]._headers[2].startswith('Binary files')) self.assertEqual(len(out[2]._hunks), 1) self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
def test_parse_binary_differ_diff_ru(self): patch = """\ --- a +++ b @@ -1,2 +1,2 @@ -foo +bar common Binary files a/1.pdf and b/1.pdf differ --- c +++ d @@ -1,2 +1,2 @@ -foo +bar common """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) out = list(parser.get_diff_generator()) self.assertEqual(len(out), 3) self.assertEqual(len(out[1]._hunks), 0) self.assertEqual(out[1]._old_path, '') self.assertEqual(out[1]._new_path, '') self.assertEqual(len(out[1]._headers), 1) self.assertTrue(out[1]._headers[0].startswith('Binary files')) self.assertEqual(len(out[2]._hunks), 1) self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
def test_iter_after_read_stream_header(self): items = ['hello', 'world', 'again', 'and', 'again'] stream = cdiff.PatchStream(Sequential(items)) _ = stream.read_stream_header(2) out = list(stream) self.assertEqual(out, items)
def test_parse_only_in_dir(self): patch = """\ --- a +++ b @@ -1,2 +1,2 @@ -foo +bar common Only in foo: foo --- c +++ d @@ -1,2 +1,2 @@ -foo +bar common """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) out = list(parser.get_diff_generator()) self.assertEqual(len(out), 3) self.assertEqual(len(out[1]._hunks), 0) self.assertEqual(out[1]._headers, ['Only in foo: foo\n']) self.assertEqual(len(out[2]._hunks), 1) self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
def test_type_detect_unified(self): patch = """\ spam --- a +++ b @@ -1,2 +1,2 @@ """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) self.assertEqual(parser._type, 'unified')
def test_parse_invalid_hunk_meta(self): patch = """\ spam --- a +++ b spam @@ -a,a +0 @@ """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) self.assertRaises(RuntimeError, list, parser.get_diff_generator())
def test_parse_missing_new_path(self): patch = """\ --- a +++ b @@ -1,2 +1,2 @@ -foo +bar common --- c """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) self.assertRaises(AssertionError, list, parser.get_diff_generator())
def test_type_detect_context(self): patch = """\ *** /path/to/original timestamp --- /path/to/new timestamp *************** *** 1,1 **** --- 1,2 ---- + This is an important This part of the """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) self.assertEqual(parser._type, 'context')
def test_parse_missing_hunk_meta(self): patch = """\ --- a +++ b @@ -1,2 +1,2 @@ -foo +bar common --- c +++ d """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) out = list(parser.get_diff_generator()) self.assertEqual(len(out), 2) self.assertEqual(len(out[1]._headers), 0) self.assertEqual(out[1]._old_path, '--- c\n') self.assertEqual(out[1]._new_path, '+++ d\n') self.assertEqual(len(out[1]._hunks), 0)
def test_parse_svn_prop(self): patch = """\ --- a +++ b Added: svn:executable ## -0,0 +1 ## +* \\ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +Id """ items = patch.splitlines(True) stream = cdiff.PatchStream(Sequential(items)) parser = cdiff.DiffParser(stream) out = list(parser.get_diff_generator()) self.assertEqual(len(out), 1) self.assertEqual(len(out[0]._hunks), 2) hunk = out[0]._hunks[1] self.assertEqual(hunk._hunk_headers, ['Added: svn:keywords\n']) self.assertEqual(hunk._hunk_list, [('+', 'Id\n')])
def test_is_empty(self): stream = cdiff.PatchStream(Sequential([])) self.assertTrue(stream.is_empty()) stream = cdiff.PatchStream(Sequential(['hello', 'world'])) self.assertFalse(stream.is_empty())