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

Delta Between Two Patch Sets: tests/tools.py

Issue 29501558: Issue 5383 - Add tests for the Chrome and Firefox packagers (Closed)
Left Patch Set: Completely purge PIL / Pillow, added edge-extension fixture / assert Created Sept. 11, 2017, 8:43 a.m.
Right Patch Set: Readme, difflib, buildnum Created Sept. 13, 2017, 1:29 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 | « tests/test_packagerWebExt.py ('k') | tox.ini » ('j') | 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 os 5 import os
6 import shutil 6 import shutil
7 import zipfile 7 import zipfile
8 import json 8 import json
9 import difflib
9 10
10 from xml.etree import ElementTree 11 from xml.etree import ElementTree
11 12
12 from buildtools.packagerChrome import defaultLocale 13 from buildtools.packagerChrome import defaultLocale
13 from buildtools.tests.conftest import MESSAGES_EN_US 14 from buildtools.tests.conftest import MESSAGES_EN_US
14 15
15 16
16 ALL_LANGUAGES = ['en_US', 'de', 'it'] 17 ALL_LANGUAGES = ['en_US', 'de', 'it']
17 18
18 19
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 101
101 102
102 def copy_metadata(filename, tmpdir): 103 def copy_metadata(filename, tmpdir):
103 """Copy the used metadata to the used temporary directory.""" 104 """Copy the used metadata to the used temporary directory."""
104 path = os.path.join(os.path.dirname(__file__), filename) 105 path = os.path.join(os.path.dirname(__file__), filename)
105 destination = str(tmpdir.join(filename)) 106 destination = str(tmpdir.join(filename))
106 shutil.copy(path, destination) 107 shutil.copy(path, destination)
107 108
108 109
109 def run_webext_build(ext_type, build_type, srcdir, packager_module, 110 def run_webext_build(ext_type, build_type, srcdir, packager_module,
110 keyfile=None): 111 buildnum=None, keyfile=None):
111 """Run a build process 112 """Run a build process.
112 113
113 Run a desired build process without unintenionally invoking the 114 Run a desired build process without unintenionally invoking the
114 releaseAutomation. 115 releaseAutomation.
115 """ 116 """
116 from buildtools.build import processArgs 117 from buildtools.build import processArgs
117 args = ['build.py', '-t', ext_type, build_type] 118 args = ['build.py', '-t', ext_type, build_type]
118 119
119 if keyfile: 120 if keyfile:
120 args += ['-k', keyfile] 121 args += ['-k', keyfile]
122 if buildnum:
123 args += ['-b', buildnum]
121 124
122 if not build_type == 'release': 125 if not build_type == 'release':
123 processArgs(str(srcdir), args) 126 processArgs(str(srcdir), args)
124 else: 127 else:
125 # We don't want to accidentaly run releaseAutomation in tests 128 # We don't want to accidentaly run releaseAutomation in tests
126 packager_module.createBuild( 129 packager_module.createBuild(
127 str(srcdir), 130 str(srcdir),
128 ext_type, 131 ext_type,
129 releaseBuild=True, 132 releaseBuild=True,
130 keyFile=keyfile, 133 keyFile=keyfile,
134 buildNum=buildnum,
131 ) 135 )
132 136
133 137
134 def locale_files(languages, rootfolder, srcdir): 138 def locale_files(languages, rootfolder, srcdir):
135 """Generate example locales. 139 """Generate example locales.
136 140
137 languages: tuple, list or set of languages to include 141 languages: tuple, list or set of languages to include
138 rootdir: folder-name to create the locale-folders in 142 rootdir: folder-name to create the locale-folders in
139 """ 143 """
140 for lang in languages: 144 for lang in languages:
141 subfolders = rootfolder.split(os.pathsep) + [lang, 'messages.json'] 145 subfolders = rootfolder.split(os.pathsep) + [lang, 'messages.json']
142 json_file = srcdir.ensure(*subfolders) 146 json_file = srcdir.ensure(*subfolders)
143 if lang == defaultLocale: 147 if lang == defaultLocale:
144 json_file.write(MESSAGES_EN_US) 148 json_file.write(MESSAGES_EN_US)
145 else: 149 else:
146 json_file.write('{}') 150 json_file.write('{}')
147 151
148 152
149 def assert_all_locales_present(package, locales_path): 153 def assert_all_locales_present(package, locales_path):
150 names = {x for x in package.namelist() if x.startswith(locales_path)} 154 names = {x for x in package.namelist() if x.startswith(locales_path)}
151 assert names == { 155 assert names == {
152 os.path.join(locales_path, lang, 'messages.json') 156 os.path.join(locales_path, lang, 'messages.json')
153 for lang in ALL_LANGUAGES 157 for lang in ALL_LANGUAGES
154 } 158 }
155 159
156 160
157 def compare_xml(a, b): 161 def compare_xml(a, b):
162 """Assert equal content in two manifests in XML format."""
158 def get_leafs_string(tree): 163 def get_leafs_string(tree):
159 """Recursively build a string representing all leaf-nodes""" 164 """Recursively build a string representing all xml leaf-nodes."""
160 root_str = '{}|{}|{}'.format(tree.tag, tree.tail, tree.text).strip() 165 root_str = '{}|{}|{}'.format(tree.tag, tree.tail, tree.text).strip()
161 result = [] 166 result = []
162 167
163 if len(tree) > 0: 168 if len(tree) > 0:
164 for subtree in tree: 169 for subtree in tree:
165 for leaf in get_leafs_string(subtree): 170 for leaf in get_leafs_string(subtree):
166 result.append('{}__{}'.format(root_str, leaf)) 171 result.append('{}__{}'.format(root_str, leaf))
167 else: 172 for k, v in tree.attrib.items():
168 result.append(root_str) 173 result.append('{}__{}:{}'.format(root_str, k, v))
169 return result 174 return result
170 175
171 tree_a = ElementTree.fromstring(a) 176 # XML data itself shall not be sorted, hence we can safely sort
172 tree_b = ElementTree.fromstring(b) 177 # our string representations in order to produce a valid unified diff.
173 178 strs_a = sorted(get_leafs_string(ElementTree.fromstring(a)))
174 return set(get_leafs_string(tree_a)) == set(get_leafs_string(tree_b)) 179 strs_b = sorted(get_leafs_string(ElementTree.fromstring(b)))
Vasily Kuznetsov 2017/09/11 12:50:08 This function and the one below seems to be only u
tlucas 2017/09/12 11:32:12 Acknowledged.
180
181 diff = list(difflib.unified_diff(strs_a, strs_b, n=0))
182 assert len(diff) == 0, '\n'.join(diff)
175 183
176 184
177 def compare_json(a, b): 185 def compare_json(a, b):
Vasily Kuznetsov 2017/09/11 12:50:08 It seems like this function essentially compares t
tlucas 2017/09/12 11:32:11 I'm not sure if it would be a good idea to just ig
Sebastian Noack 2017/09/12 21:55:26 I agree, turning lists into sets when comparing th
tlucas 2017/09/13 13:43:25 Agreed - i came up with a different solution, plea
178 def get_leafs_string(obj): 186 """Assert equal content in two manifests in JSON format.
179 """Recursively build a string representing all leaf-nodes""" 187
180 result = [] 188 Compare the content of two JSON strings, respecting the order of items in
181 if isinstance(obj, list): 189 a list as well as possible duplicates.
182 for item in obj: 190 Raise a unified diff if equality could not be asserted.
183 for repr_str in get_leafs_string(item): 191 """
184 result.append(':,' + repr_str) 192 json_a = json.dumps(json.loads(a), sort_keys=True, indent=0).split('\n')
185 elif isinstance(obj, dict): 193 json_b = json.dumps(json.loads(b), sort_keys=True, indent=0).split('\n')
186 for k, v in obj.items(): 194
187 for repr_str in get_leafs_string(v): 195 diff = list(difflib.unified_diff(json_a, json_b, n=0))
188 result.append('{}:{}'.format(k, repr_str)) 196 assert len(diff) == 0, '\n'.join(diff)
189 else:
190 result = [str(obj)]
191
192 return result
193
194 tree_a = json.loads(a)
195 tree_b = json.loads(b)
196 return set(get_leafs_string(tree_a)) == set(get_leafs_string(tree_b))
197 197
198 198
199 def assert_manifest_content(manifest, expected_path): 199 def assert_manifest_content(manifest, expected_path):
200 extension = os.path.basename(expected_path).split('.')[-1] 200 extension = os.path.basename(expected_path).split('.')[-1]
201 201
202 with open(expected_path, 'r') as fp: 202 with open(expected_path, 'r') as fp:
203 if extension == 'xml': 203 if extension == 'xml':
204 assert compare_xml(manifest, fp.read()) 204 compare_xml(manifest, fp.read())
205 else: 205 else:
206 assert compare_json(manifest, fp.read()) 206 compare_json(manifest, fp.read())
LEFTRIGHT

Powered by Google App Engine
This is Rietveld