Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: scripts/slave/unittests/expect_tests/serialize.py

Issue 220353003: Replace recipes_test.py with a new parallel expectation-based test runner. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/build
Patch Set: refactor Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import difflib
6 import json
7 import os
8 import pprint
9
10 try:
11 import yaml
12 except ImportError:
13 yaml = None
14
15 NonExistant = object()
16
17 SUPPORTED_SERIALIZERS = {'json', 'yaml'}
18 SERIALIZERS = {}
19
20 def re_encode(obj):
21 if isinstance(obj, dict):
22 return {re_encode(k): re_encode(v) for k, v in obj.iteritems()}
23 elif isinstance(obj, list):
24 return [re_encode(i) for i in obj]
25 elif isinstance(obj, unicode):
26 return obj.encode('utf-8')
27 else:
28 return obj
29
30 SERIALIZERS['json'] = (
31 lambda s: re_encode(json.load(s)),
32 lambda data, stream: json.dump(
33 data, stream, sort_keys=True, indent=2, separators=(',', ': ')))
34
35 if yaml:
36 _YAMLSafeLoader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
37 _YAMLSafeDumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
38 SERIALIZERS['yaml'] = (
39 lambda stream: yaml.load(stream, _YAMLSafeLoader),
40 lambda data, stream: yaml.dump(
41 data, stream, _YAMLSafeDumper, default_flow_style=False,
42 encoding='utf-8'))
43
44
45 def GetCurrentData(test):
46 """
47 @type test: Test()
48 @returns: The deserialized data (or NonExistant), and a boolean indicating if
49 the current serialized data is in the same format which was
50 requested by |test|.
51 @rtype: (dict, bool)
52 """
53 for ext in sorted(SUPPORTED_SERIALIZERS, key=lambda s: s != test.ext):
54 path = test.expect_path(ext)
55 if ext not in SERIALIZERS:
56 raise Exception('The package to support %s is not installed.' % ext)
57 if os.path.exists(path):
58 with open(path, 'rb') as f:
59 data = SERIALIZERS[ext][0](f)
60 return data, ext == test.ext
61 return NonExistant, True
62
63
64 def WriteNewData(test, data):
65 """
66 @type test: Test()
67 """
68 if test.ext not in SUPPORTED_SERIALIZERS:
69 raise Exception('%s is not a supported serializer.' % test.ext)
70 if test.ext not in SERIALIZERS:
71 raise Exception('The package to support %s is not installed.' % test.ext)
72 with open(test.expect_path(), 'wb') as f:
73 SERIALIZERS[test.ext][1](data, f)
74
75
76 def DiffData(old, new):
77 """
78 Takes old data and new data, then returns a textual diff as a list of lines.
79 @type old: dict
80 @type new: dict
81 @rtype: [str]
82 """
83 if old is NonExistant:
84 return new
85 if old == new:
86 return None
87 else:
88 return list(difflib.context_diff(
89 pprint.pformat(old).splitlines(),
90 pprint.pformat(new).splitlines(),
91 fromfile='expected', tofile='current',
92 n=4, lineterm=''
93 ))
94
Vadim Sh. 2014/04/02 22:37:59 remove trailing empty line :)
95
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698