[diffoscope] 01/01: Fix json presenter and handle recursion properly, it was completely wrong
Ximin Luo
infinity0 at debian.org
Mon May 29 21:22:39 CEST 2017
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch experimental
in repository diffoscope.
commit 065f9c0c1f4082cc17625d006f8be8a230452b9e
Author: Ximin Luo <infinity0 at debian.org>
Date: Mon May 29 21:22:20 2017 +0200
Fix json presenter and handle recursion properly, it was completely wrong
---
diffoscope/presenters/json.py | 35 +++++++++++++++++++++------------
tests/data/output.json | 45 ++++++++++++++++++-------------------------
2 files changed, 42 insertions(+), 38 deletions(-)
diff --git a/diffoscope/presenters/json.py b/diffoscope/presenters/json.py
index 0612670..18d8f56 100644
--- a/diffoscope/presenters/json.py
+++ b/diffoscope/presenters/json.py
@@ -29,26 +29,37 @@ JSON_FORMAT_MAGIC = "diffoscope-json-version"
class JSONPresenter(Presenter):
def __init__(self, print_func):
self.stack = []
- self.current = self.stack
self.print_func = print_func
super().__init__()
def start(self, difference):
+ root = []
+ self.stack = [root]
super().start(difference)
- self.stack[0][JSON_FORMAT_MAGIC] = JSON_FORMAT_MAGIC
- self.stack[0].move_to_end(JSON_FORMAT_MAGIC, last=False)
- self.print_func(json.dumps(self.stack[0], indent=2))
+ root[0][JSON_FORMAT_MAGIC] = JSON_FORMAT_VERSION
+ root[0].move_to_end(JSON_FORMAT_MAGIC, last=False)
+ self.print_func(json.dumps(root[0], indent=2))
def visit_difference(self, difference):
- child_differences = []
- self.current.append(OrderedDict([
+ while self.depth + 1 < len(self.stack):
+ self.stack.pop()
+
+ elements = [
('source1', difference.source1),
- ('source2', difference.source2),
- ('comments', [x for x in difference.comments]),
- ('differences', child_differences),
- ('unified_diff', difference.unified_diff),
- ]))
+ ('source2', difference.source2)
+ ]
+ if difference.comments:
+ elements += [('comments', [x for x in difference.comments])]
+ if difference.has_internal_linenos:
+ elements += [('has_internal_linenos', True)]
+ elements += [('unified_diff', difference.unified_diff)]
+
+ child_differences = []
+ if difference.details:
+ elements += [('details', child_differences)]
- self.current = child_differences
+ self.stack[-1].append(OrderedDict(elements))
+ if difference.details:
+ self.stack.append(child_differences)
diff --git a/tests/data/output.json b/tests/data/output.json
index 66cfeb7..d8b5eba 100644
--- a/tests/data/output.json
+++ b/tests/data/output.json
@@ -1,33 +1,26 @@
{
- "comments": [],
- "differences": [
+ "diffoscope-json-version": 1,
+ "source1": "test1.tar",
+ "source2": "test2.tar",
+ "unified_diff": null,
+ "details": [
{
- "comments": [],
- "differences": [
- {
- "comments": [],
- "differences": [
- {
- "comments": [
- "symlink"
- ],
- "differences": [],
- "source1": "dir/link",
- "source2": "dir/link",
- "unified_diff": "@@ -1 +1 @@\n-destination: broken\n+destination: really-broken\n"
- }
- ],
- "source1": "dir/text",
- "source2": "dir/text",
- "unified_diff": "@@ -1,6 +1,12 @@\n+A common form of lorem ipsum reads:\n+\n Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\n incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis\n nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu\n fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in\n culp [...]
- }
- ],
"source1": "file list",
"source2": "file list",
"unified_diff": "@@ -1,4 +1,4 @@\n-drwxr-xr-x 0 lunar (1000) lunar (1000) 0 2015-06-29 15:49:09.000000 dir/\n--rw-r--r-- 0 lunar (1000) lunar (1000) 446 2015-06-29 15:49:09.000000 dir/text\n-crw-r--r-- 0 root (0) root (0) 1, 3 2015-06-29 15:49:09.000000 dir/null\n-lrwxrwxrwx 0 lunar (1000) lunar (1000) 0 2015-06-29 15:49:09.000000 dir/link -> broken\n+drwxr-xr-x 0 lunar (1000) lunar (1000) 0 201 [...]
+ },
+ {
+ "source1": "dir/text",
+ "source2": "dir/text",
+ "unified_diff": "@@ -1,6 +1,12 @@\n+A common form of lorem ipsum reads:\n+\n Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\n incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis\n nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu\n fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in\n culpa qu [...]
+ },
+ {
+ "source1": "dir/link",
+ "source2": "dir/link",
+ "comments": [
+ "symlink"
+ ],
+ "unified_diff": "@@ -1 +1 @@\n-destination: broken\n+destination: really-broken\n"
}
- ],
- "source1": "test1.tar",
- "source2": "test2.tar",
- "unified_diff": null
+ ]
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list