|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +import difflib |
15 | 16 | import hashlib
|
16 | 17 | import os
|
17 | 18 | import unittest
|
| 19 | +import zipfile |
18 | 20 |
|
19 | 21 |
|
20 | 22 | def TestData(name):
|
21 | 23 | return os.path.join(os.environ['TEST_SRCDIR'], 'io_bazel_rules_python', name)
|
22 | 24 |
|
23 | 25 |
|
| 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 | + |
24 | 43 | class WheelTest(unittest.TestCase):
|
25 | 44 |
|
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')) |
43 | 91 |
|
44 | 92 | if __name__ == '__main__':
|
45 | 93 | unittest.main()
|
0 commit comments