8000 Merge branch 'master' into pip_upgrade · sorensenjs/rules_python@e4c3412 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4c3412

Browse files
author
Douglas Greiman
authored
Merge branch 'master' into pip_upgrade
2 parents 9e89e13 + 3c0e5df commit e4c3412

File tree

1 file changed

+65
-17
lines changed

1 file changed

+65
-17
lines changed

tools/par_test.py

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,82 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import difflib
1516
import hashlib
1617
import os
1718
import unittest
19+
import zipfile
1820

1921

2022
def TestData(name):
2123
return os.path.join(os.environ['TEST_SRCDIR'], 'io_bazel_rules_python', name)
2224

2325

26+
def _format_toc(filename):
27+
"""Return a table of contents for the zip file as a string.
28+
29+
Args:
30+
filename (str): Path to zip file
31+
32+
Returns: directory listed in format matching zipfile.printdir()
33+
"""
34+
zf = zipfile.ZipFile(filename)
35+
lines = []
36+
lines.append("%-46s %19s %12s" % ("File Name", "Modified ", "Size"))
37+
for zinfo in zf.filelist:
38+
date = "%d-%02d-%02d %02d:%02d:%02d" % zinfo.date_time[:6]
39+
lines.append("%-46s %s %12d" % (zinfo.filename, date, zinfo.file_size))
40+
return lines
41+
42+
2443
class WheelTest(unittest.TestCase):
2544

26-
def test_piptool_matches(self):
27-
with open(TestData('rules_python/piptool.par'), 'r') as f:
28-
built = f.read()
29-
with open(TestData('tools/piptool.par'), 'r') as f:
30-
checked_in = f.read()
31-
self.assertEquals(
32-
hashlib.sha256(built).hexdigest(), hashlib.sha256(checked_in).hexdigest(),
33-
'The checked in tools/piptool.par does not match the latest build.')
34-
35-
def test_whltool_matches(self):
36-
with open(TestData('rules_python/whltool.par'), 'r') as f:
37-
built = f.read()
38-
with open(TestData('tools/whltool.par'), 'r') as f:
39-
checked_in = f.read()
40-
self.assertEquals(
41-
hashlib.sha256(built).hexdigest(), hashlib.sha256(checked_in).hexdigest(),
42-
'The checked in tools/whltool.par does not match the latest build.')
45+
def _diff_zip(self, filename1, filename2):
46+
"""Compare two zip files for equality, pretty-printing differences."""
47+
with open(filename1, 'rb') as file1:
48+
contents1 = file1.read()
49+
with open (filename2, 'rb') as file2:
50+
contents2 = file2.read()
51+
if contents1 != contents2:
52+
toc1 = _format_toc(filename1)
53+
toc2 = _format_toc(filename2)
54+
sha1 = hashlib.sha256(filename1).hexdigest()
55+
sha2 = hashlib.sha256(filename2).hexdigest()
56+
diff = difflib.unified_diff(toc1, toc2)
57+
diff_str = '\n'.join(diff) or (
58+
'No differences in zip contents, only in zip headers [not shown]')
59+
message = r'''Files do not match.
60+
61+
************************************************************************
62+
File 1: %s
63+
Length in bytes: %s
64+
SHA256: %s
65+
************************************************************************
66+
File 2: %s
67+
Length in Bytes: %s
68+
SHA256: %s
69+
************************************************************************
70+
Zip Content Diff:
71+
%s
72+
************************************************************************
73+
''' % (
74+
filename1,
75+
len(contents1),
76+
sha1,
77+
filename2,
78+
len(contents2),
79+
sha2,
80+
diff_str,
81+
)
82+
self.assertEquals(contents1, contents2, message)
83+
84+
def test_piptool_matches(self):
85+
self._diff_zip(TestData('rules_python/piptool.par'),
86+
TestData('tools/piptool.par'))
87+
88+
def test_whltool_matches(self):
89+
self._diff_zip(TestData('rules_python/whltool.par'),
90+
TestData('tools/whltool.par'))
4391

4492
if __name__ == '__main__':
4593
unittest.main()

0 commit comments

Comments
 (0)
0