| borenet | 0f1469b | 2016-06-15 12:07:42 -0700 | [diff] [blame] | 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 test_utils |
| 15 | import unittest |
| 16 | import utils |
| 17 | import uuid |
| 18 | import zip_utils |
| 19 | |
| 20 | |
| 21 | class ZipUtilsTest(unittest.TestCase): |
| 22 | def test_zip_unzip(self): |
| 23 | with utils.tmp_dir(): |
| 24 | fw = test_utils.FileWriter(os.path.join(os.getcwd(), 'input')) |
| 25 | # Create input files and directories. |
| 26 | fw.mkdir('mydir') |
| 27 | fw.mkdir('anotherdir', 0666) |
| 28 | fw.mkdir('dir3', 0600) |
| 29 | fw.mkdir('subdir') |
| 30 | fw.write('a.txt', 0777) |
| 31 | fw.write('b.txt', 0751) |
| 32 | fw.write('c.txt', 0640) |
| 33 | fw.write(os.path.join('subdir', 'd.txt'), 0640) |
| Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 34 | |
| borenet | 0f1469b | 2016-06-15 12:07:42 -0700 | [diff] [blame] | 35 | # Zip, unzip. |
| 36 | zip_utils.zip('input', 'test.zip') |
| 37 | zip_utils.unzip('test.zip', 'output') |
| 38 | |
| 39 | # Compare the inputs and outputs. |
| 40 | test_utils.compare_trees(self, 'input', 'output') |
| 41 | |
| 42 | def test_blacklist(self): |
| 43 | with utils.tmp_dir(): |
| 44 | # Create input files and directories. |
| 45 | fw = test_utils.FileWriter(os.path.join(os.getcwd(), 'input')) |
| 46 | fw.mkdir('.git') |
| 47 | fw.write(os.path.join('.git', 'index')) |
| 48 | fw.write('somefile') |
| 49 | fw.write('.DS_STORE') |
| 50 | fw.write('leftover.pyc') |
| 51 | fw.write('.pycfile') |
| 52 | |
| 53 | # Zip, unzip. |
| 54 | zip_utils.zip('input', 'test.zip', blacklist=['.git', '.DS*', '*.pyc']) |
| 55 | zip_utils.unzip('test.zip', 'output') |
| 56 | |
| 57 | # Remove the files/dirs we don't expect to see in output, so that we can |
| 58 | # use self._compare_trees to check the results. |
| 59 | fw.remove(os.path.join('.git', 'index')) |
| 60 | fw.remove('.git') |
| 61 | fw.remove('.DS_STORE') |
| 62 | fw.remove('leftover.pyc') |
| 63 | |
| 64 | # Compare results. |
| 65 | test_utils.compare_trees(self, 'input', 'output') |
| 66 | |
| 67 | def test_nonexistent_dir(self): |
| 68 | with utils.tmp_dir(): |
| 69 | with self.assertRaises(IOError): |
| 70 | zip_utils.zip('input', 'test.zip') |
| 71 | |
| 72 | |
| 73 | if __name__ == '__main__': |
| 74 | unittest.main() |