OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright 2016 Google Inc. |
| 4 # |
| 5 # Use of this source code is governed by a BSD-style license that can be |
| 6 # found in the LICENSE file. |
| 7 |
| 8 |
| 9 """Tests for zip_utils.""" |
| 10 |
| 11 |
| 12 import filecmp |
| 13 import os |
| 14 import unittest |
| 15 import utils |
| 16 import uuid |
| 17 import zip_utils |
| 18 |
| 19 |
| 20 def writefile(fname, mode=0640): |
| 21 """Write the file with the given mode and random contents.""" |
| 22 dirname = os.path.dirname(fname) |
| 23 if not os.path.isdir(dirname): |
| 24 os.makedirs(dirname) |
| 25 with open(fname, 'w') as f: |
| 26 f.write(str(uuid.uuid4())) |
| 27 os.chmod(fname, mode) |
| 28 |
| 29 |
| 30 class ZipUtilsTest(unittest.TestCase): |
| 31 def _compare_trees(self, a, b): |
| 32 """Compare two directory trees, assert if any differences.""" |
| 33 def _cmp(prefix, dcmp): |
| 34 # Verify that the file and directory names are the same. |
| 35 self.assertEqual(len(dcmp.left_only), 0) |
| 36 self.assertEqual(len(dcmp.right_only), 0) |
| 37 self.assertEqual(len(dcmp.diff_files), 0) |
| 38 self.assertEqual(len(dcmp.funny_files), 0) |
| 39 |
| 40 # Verify that the files are identical. |
| 41 for f in dcmp.common_files: |
| 42 pathA = os.path.join(a, prefix, f) |
| 43 pathB = os.path.join(b, prefix, f) |
| 44 self.assertTrue(filecmp.cmp(pathA, pathB, shallow=False)) |
| 45 statA = os.stat(pathA) |
| 46 statB = os.stat(pathB) |
| 47 self.assertEqual(statA.st_mode, statB.st_mode) |
| 48 with open(pathA, 'rb') as f: |
| 49 contentsA = f.read() |
| 50 with open(pathB, 'rb') as f: |
| 51 contentsB = f.read() |
| 52 self.assertEqual(contentsA, contentsB) |
| 53 |
| 54 # Recurse on subdirectories. |
| 55 for prefix, obj in dcmp.subdirs.iteritems(): |
| 56 _cmp(prefix, obj) |
| 57 |
| 58 _cmp('', filecmp.dircmp(a, b)) |
| 59 |
| 60 def test_zip_unzip(self): |
| 61 with utils.tmp_dir(): |
| 62 # Create input files and directories. |
| 63 os.mkdir('input') |
| 64 os.mkdir(os.path.join('input', 'mydir')) |
| 65 os.mkdir(os.path.join('input', 'anotherdir')) |
| 66 os.chmod(os.path.join('input', 'anotherdir'), 0666) |
| 67 os.mkdir(os.path.join('input', 'dir3')) |
| 68 os.chmod(os.path.join('input', 'dir3'), 0600) |
| 69 writefile(os.path.join('input', 'a.txt'), 0777) |
| 70 writefile(os.path.join('input', 'b.txt'), 0751) |
| 71 writefile(os.path.join('input', 'c.txt'), 0640) |
| 72 writefile(os.path.join('input', 'subdir', 'd.txt'), 0640) |
| 73 |
| 74 # Zip, unzip. |
| 75 zip_utils.zip('input', 'test.zip') |
| 76 zip_utils.unzip('test.zip', 'output') |
| 77 |
| 78 # Compare the inputs and outputs. |
| 79 self._compare_trees('input', 'output') |
| 80 |
| 81 def test_blacklist(self): |
| 82 with utils.tmp_dir(): |
| 83 # Create input files and directories. |
| 84 os.mkdir('input') |
| 85 os.mkdir(os.path.join('input', '.git')) |
| 86 writefile(os.path.join('input', '.git', 'index')) |
| 87 writefile(os.path.join('input', 'somefile')) |
| 88 writefile(os.path.join('input', '.DS_STORE')) |
| 89 writefile(os.path.join('input', 'leftover.pyc')) |
| 90 writefile(os.path.join('input', '.pycfile')) |
| 91 |
| 92 # Zip, unzip. |
| 93 zip_utils.zip('input', 'test.zip', blacklist=['.git', '.DS*', '*.pyc']) |
| 94 zip_utils.unzip('test.zip', 'output') |
| 95 |
| 96 # Remove the files/dirs we don't expect to see in output, so that we can |
| 97 # use self._compare_trees to check the results. |
| 98 os.remove(os.path.join('input', '.git', 'index')) |
| 99 os.rmdir(os.path.join('input', '.git')) |
| 100 os.remove(os.path.join('input', '.DS_STORE')) |
| 101 os.remove(os.path.join('input', 'leftover.pyc')) |
| 102 |
| 103 # Compare results. |
| 104 self._compare_trees('input', 'output') |
| 105 |
| 106 |
| 107 if __name__ == '__main__': |
| 108 unittest.main() |
OLD | NEW |