Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: tests/test_packagerWebExt.py

Issue 29586561: Noissue - Make comparable_xml safe against ambiguous tags (Closed)
Left Patch Set: Created Oct. 23, 2017, 10:20 a.m.
Right Patch Set: Addressing Vasily's comments Created Oct. 23, 2017, 1:19 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This Source Code Form is subject to the terms of the Mozilla Public 1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 4
5 import difflib 5 import difflib
6 import json 6 import json
7 import os 7 import os
8 import re 8 import re
9 import shutil 9 import shutil
10 import zipfile 10 import zipfile
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 files['ext/c.js'] = 'var this_is_c;' 233 files['ext/c.js'] = 'var this_is_c;'
234 234
235 tmpdir.mkdir('lib').join('b.js').write(files['lib/b.js']) 235 tmpdir.mkdir('lib').join('b.js').write(files['lib/b.js'])
236 ext_dir = tmpdir.mkdir('ext') 236 ext_dir = tmpdir.mkdir('ext')
237 ext_dir.join('a.js').write(files['ext/a.js']) 237 ext_dir.join('a.js').write(files['ext/a.js'])
238 ext_dir.join('c.js').write(files['ext/c.js']) 238 ext_dir.join('c.js').write(files['ext/c.js'])
239 239
240 return files 240 return files
241 241
242 242
243 def comparable_json(json_string): 243 def comparable_json(json_data):
244 """Create a nonambiguous representation of a given JSON string.""" 244 """Create a nonambiguous representation of the given JSON data."""
245 if isinstance(json_data, basestring):
246 json_data = json.loads(json_data)
245 return json.dumps( 247 return json.dumps(
246 json.loads(json_string), sort_keys=True, indent=0 248 json_data, sort_keys=True, indent=0
247 ).split('\n') 249 ).split('\n')
248 250
249 251
250 def comparable_xml(xml): 252 def comparable_xml(xml):
251 """Create a nonambiguous representation of a given XML tree.""" 253 """Create a nonambiguous representation of a given XML tree."""
252 def strip(s): 254 def strip(s):
253 if s is None: 255 if s is None:
254 return '' 256 return ''
255 return s.strip() 257 return s.strip()
256 258
257 def transform(elt): 259 def transform(elt):
258 subs = sorted(transform(s) for s in elt) 260 subs = sorted(transform(s) for s in elt)
259 attrs = tuple('{}={}'.format(k, v) for k, v in elt.attrib.items()) 261 return (elt.tag, strip(elt.tail), strip(elt.text), elt.attrib, subs)
Vasily Kuznetsov 2017/10/23 12:48:20 Since you're doing a `comparable_json` later, we c
tlucas 2017/10/23 13:21:36 Good point - Done.
260 return (elt.tag, strip(elt.tail), strip(elt.text), attrs, subs) 262
261 263 return comparable_json(transform(ElementTree.fromstring(xml)))
262 return comparable_json(json.dumps(transform(ElementTree.fromstring(xml))))
Vasily Kuznetsov 2017/10/23 12:48:20 Its kind of a pity that we're doing `json.dumps` h
tlucas 2017/10/23 13:21:36 No, you are right - dumps(loads(dumps())) is defin
263 264
264 265
265 def assert_manifest_content(manifest, expected_path): 266 def assert_manifest_content(manifest, expected_path):
266 extension = os.path.basename(expected_path).split('.')[-1] 267 extension = os.path.basename(expected_path).split('.')[-1]
267 268
268 with open(expected_path, 'r') as fp: 269 with open(expected_path, 'r') as fp:
269 if extension == 'xml': 270 if extension == 'xml':
270 generated = comparable_xml(manifest) 271 generated = comparable_xml(manifest)
271 expected = comparable_xml(fp.read()) 272 expected = comparable_xml(fp.read())
272 else: 273 else:
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 expected = os.path.join( 459 expected = os.path.join(
459 os.path.dirname(__file__), 460 os.path.dirname(__file__),
460 'expecteddata', 461 'expecteddata',
461 filename.format(name, ext), 462 filename.format(name, ext),
462 ) 463 )
463 464
464 assert_manifest_content( 465 assert_manifest_content(
465 package.read(os.path.join(folder, '{}.{}'.format(name, ext))), 466 package.read(os.path.join(folder, '{}.{}'.format(name, ext))),
466 expected, 467 expected,
467 ) 468 )
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld