[game-data-packager] 13/51: check_equivalence: Try to show diffs between YAML, not JSON, renditions

Simon McVittie smcv at debian.org
Fri Dec 29 01:23:35 UTC 2017


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch master
in repository game-data-packager.

commit e46a0ca4bfc432687026e7f0cef5e3186a60095c
Author: Simon McVittie <smcv at debian.org>
Date:   Tue Dec 26 16:59:31 2017 +0000

    check_equivalence: Try to show diffs between YAML, not JSON, renditions
    
    This is a bit better to diff.
    
    Signed-off-by: Simon McVittie <smcv at debian.org>
---
 tools/check_equivalence.py | 71 ++++++++++++++++++++++++++++++----------------
 1 file changed, 47 insertions(+), 24 deletions(-)

diff --git a/tools/check_equivalence.py b/tools/check_equivalence.py
index 33c0bd7..a104dff 100755
--- a/tools/check_equivalence.py
+++ b/tools/check_equivalence.py
@@ -20,6 +20,7 @@ import json
 import os
 import sys
 import time
+import yaml
 
 from game_data_packager import load_games
 from game_data_packager.util import ascii_safe
@@ -27,6 +28,33 @@ from game_data_packager.util import ascii_safe
 def dump(serialized):
     return json.dumps(serialized, sort_keys=True, indent=2)
 
+def yaml_dump(serialized):
+    return yaml.dump(
+        serialized, default_flow_style=False)
+
+def compare(
+        to_data, to_json, label,
+        expected_data, expected_json, expected_label):
+    if to_json == expected_json:
+        return True
+
+    to_yaml = yaml_dump(to_data)
+    expected_to_yaml = yaml_dump(expected_data)
+
+    if to_yaml == expected_to_yaml:
+        print('# JSON differs but YAML is the same?!')
+        sys.stdout.writelines(difflib.unified_diff(
+            to_json.splitlines(True),
+            expected_json.splitlines(True),
+            label, expected_label, n=50))
+    else:
+        sys.stdout.writelines(difflib.unified_diff(
+            to_yaml.splitlines(True),
+            expected_to_yaml.splitlines(True),
+            label, expected_label, n=50))
+
+    return False
+
 if __name__ == '__main__':
     games = '*'
 
@@ -78,43 +106,38 @@ if __name__ == '__main__':
         game.load_file_data(check=False)
         ascii_safe(game.longname, force=True).encode('ascii')
         ascii_safe(game.help_text, force=True).encode('ascii')
-        vfs_to_json = dump(game.to_data())
+        vfs_to_data = game.to_data()
+        vfs_to_json = dump(vfs_to_data)
 
         json_game = from_json[name]
         json_game.load_file_data(check=True, use_vfs=False)
-        json_to_json = dump(json_game.to_data())
+        json_to_data = json_game.to_data()
+        json_to_json = dump(json_to_data)
 
         yaml_game = from_yaml[name]
         yaml_game.load_file_data(check=True, datadir='data')
-        yaml_to_json = dump(yaml_game.to_data())
-
-        if yaml_to_json != vfs_to_json:
-            sys.stdout.writelines(difflib.unified_diff(
-                yaml_to_json.splitlines(True),
-                vfs_to_json.splitlines(True),
-                '%s loaded from YAML' % name,
-                '%s loaded from vfs.zip' % name, n=50))
+        yaml_to_data = yaml_game.to_data()
+        yaml_to_json = dump(yaml_to_data)
+
+        if not compare(
+                yaml_to_data, yaml_to_json, '%s loaded from YAML' % name,
+                vfs_to_data, vfs_to_json, '%s loaded from vfs.zip' % name):
             fail = True
 
-        if json_to_json != vfs_to_json:
-            sys.stdout.writelines(difflib.unified_diff(
-                json_to_json.splitlines(True),
-                vfs_to_json.splitlines(True),
-                '%s loaded from JSON' % name,
-                '%s loaded from vfs.zip' % name, n=50))
+        if not compare(
+                json_to_data, json_to_json, '%s loaded from JSON' % name,
+                vfs_to_data, vfs_to_json, '%s loaded from vfs.zip' % name):
             fail = True
 
         if from_ref is not None:
             ref_game = from_ref[name]
             ref_game.load_file_data(use_vfs='ref.zip')
-            ref_to_json = dump(ref_game.to_data())
-
-            if ref_to_json != vfs_to_json:
-                sys.stdout.writelines(difflib.unified_diff(
-                    ref_to_json.splitlines(True),
-                    vfs_to_json.splitlines(True),
-                    '%s loaded from ref.zip' % name,
-                    '%s loaded from vfs.zip' % name, n=50))
+            ref_to_data = ref_game.to_data()
+            ref_to_json = dump(ref_to_data)
+
+            compare(
+                ref_to_data, ref_to_json, '%s loaded from ref.zip' % name,
+                vfs_to_data, vfs_to_json, '%s loaded from vfs.zip' % name)
 
         with open('out/%s-derived.txt' % name, 'w') as writer:
             writer.write(vfs_to_json)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/game-data-packager.git



More information about the Pkg-games-commits mailing list