Skip to content

Commit

Permalink
Merge pull request #34 from opensourcerouting/jsonpp
Browse files Browse the repository at this point in the history
Improve JSON error output
  • Loading branch information
mwinter-osr authored Sep 21, 2017
2 parents 388561d + f3d92e0 commit a616c33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
1 change: 0 additions & 1 deletion lib/topogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import grp
import platform
import pwd
import re
import subprocess

from mininet.net import Mininet
Expand Down
38 changes: 26 additions & 12 deletions lib/topotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# OF THIS SOFTWARE.
#

import json
import os
import errno
import re
Expand Down Expand Up @@ -51,14 +52,26 @@ def __init__(self):

def add_error(self, error):
"Append error message to the result"
self.errors.append(error)
for line in error.splitlines():
self.errors.append(line)

def has_errors(self):
"Returns True if there were errors, otherwise False."
return len(self.errors) > 0

def json_diff(d1, d2):
"""
Returns a string with the difference between JSON data.
"""
json_format_opts = {
'indent': 4,
'sort_keys': True,
}
dstr1 = json.dumps(d1, **json_format_opts)
dstr2 = json.dumps(d2, **json_format_opts)
return difflines(dstr2, dstr1, title1='Expected value', title2='Current value', n=0)

def json_cmp(d1, d2, reason=False):
def json_cmp(d1, d2):
"""
JSON compare function. Receives two parameters:
* `d1`: json value
Expand Down Expand Up @@ -109,8 +122,9 @@ def json_cmp(d1, d2, reason=False):
if len(nd2[key]) > len(nd1[key]):
result.add_error(
'{}["{}"] too few items '.format(parent, key) +
'(have ({}) "{}", expected ({}) "{}")'.format(
len(nd1[key]), str(nd1[key]), len(nd2[key]), str(nd2[key])))
'(have {}, expected {}:\n {})'.format(
len(nd1[key]), len(nd2[key]),
json_diff(nd1[key], nd2[key])))
continue

# List all unmatched items errors
Expand All @@ -130,15 +144,15 @@ def json_cmp(d1, d2, reason=False):
# If there are unmatched items, error out.
if unmatched:
result.add_error(
'{}["{}"] value is different (have "{}", expected "{}")'.format(
parent, key, str(nd1[key]), str(nd2[key])))
'{}["{}"] value is different (\n{})'.format(
parent, key, json_diff(nd1[key], nd2[key])))
continue

# Compare JSON values
if nd1[key] != nd2[key]:
result.add_error(
'{}["{}"] value is different (have "{}", expected "{}")'.format(
parent, key, str(nd1[key]), str(nd2[key])))
'{}["{}"] value is different (\n{})'.format(
parent, key, json_diff(nd1[key], nd2[key])))
continue

if result.has_errors():
Expand Down Expand Up @@ -198,20 +212,20 @@ def pid_exists(pid):
else:
return True

def get_textdiff(text1, text2, title1="", title2=""):
def get_textdiff(text1, text2, title1="", title2="", **opts):
"Returns empty string if same or formatted diff"

diff = '\n'.join(difflib.unified_diff(text1, text2,
fromfile=title1, tofile=title2))
fromfile=title1, tofile=title2, **opts))
# Clean up line endings
diff = os.linesep.join([s for s in diff.splitlines() if s])
return diff

def difflines(text1, text2, title1='', title2=''):
def difflines(text1, text2, title1='', title2='', **opts):
"Wrapper for get_textdiff to avoid string transformations."
text1 = ('\n'.join(text1.rstrip().splitlines()) + '\n').splitlines(1)
text2 = ('\n'.join(text2.rstrip().splitlines()) + '\n').splitlines(1)
return get_textdiff(text1, text2, title1, title2)
return get_textdiff(text1, text2, title1, title2, **opts)

def get_file(content):
"""
Expand Down

0 comments on commit a616c33

Please sign in to comment.