1cb93a386Sopenharmony_ci#!/usr/bin/env python
2cb93a386Sopenharmony_ci#
3cb93a386Sopenharmony_ci# Copyright 2016 Google Inc.
4cb93a386Sopenharmony_ci#
5cb93a386Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
6cb93a386Sopenharmony_ci# found in the LICENSE file.
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ci"""Tests for zip_utils."""
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ciimport filecmp
13cb93a386Sopenharmony_ciimport os
14cb93a386Sopenharmony_ciimport test_utils
15cb93a386Sopenharmony_ciimport unittest
16cb93a386Sopenharmony_ciimport utils
17cb93a386Sopenharmony_ciimport uuid
18cb93a386Sopenharmony_ciimport zip_utils
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ciclass ZipUtilsTest(unittest.TestCase):
22cb93a386Sopenharmony_ci  def test_zip_unzip(self):
23cb93a386Sopenharmony_ci    with utils.tmp_dir():
24cb93a386Sopenharmony_ci      fw = test_utils.FileWriter(os.path.join(os.getcwd(), 'input'))
25cb93a386Sopenharmony_ci      # Create input files and directories.
26cb93a386Sopenharmony_ci      fw.mkdir('mydir')
27cb93a386Sopenharmony_ci      fw.mkdir('anotherdir', 0o666)
28cb93a386Sopenharmony_ci      fw.mkdir('dir3', 0o600)
29cb93a386Sopenharmony_ci      fw.mkdir('subdir')
30cb93a386Sopenharmony_ci      fw.write('a.txt', 0o777)
31cb93a386Sopenharmony_ci      fw.write('b.txt', 0o751)
32cb93a386Sopenharmony_ci      fw.write('c.txt', 0o640)
33cb93a386Sopenharmony_ci      fw.write(os.path.join('subdir', 'd.txt'), 0o640)
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci      # Zip, unzip.
36cb93a386Sopenharmony_ci      zip_utils.zip('input', 'test.zip')
37cb93a386Sopenharmony_ci      zip_utils.unzip('test.zip', 'output')
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci      # Compare the inputs and outputs.
40cb93a386Sopenharmony_ci      test_utils.compare_trees(self, 'input', 'output')
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci  def test_to_skip(self):
43cb93a386Sopenharmony_ci    with utils.tmp_dir():
44cb93a386Sopenharmony_ci      # Create input files and directories.
45cb93a386Sopenharmony_ci      fw = test_utils.FileWriter(os.path.join(os.getcwd(), 'input'))
46cb93a386Sopenharmony_ci      fw.mkdir('.git')
47cb93a386Sopenharmony_ci      fw.write(os.path.join('.git', 'index'))
48cb93a386Sopenharmony_ci      fw.write('somefile')
49cb93a386Sopenharmony_ci      fw.write('.DS_STORE')
50cb93a386Sopenharmony_ci      fw.write('leftover.pyc')
51cb93a386Sopenharmony_ci      fw.write('.pycfile')
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci      # Zip, unzip.
54cb93a386Sopenharmony_ci      zip_utils.zip('input', 'test.zip', to_skip=['.git', '.DS*', '*.pyc'])
55cb93a386Sopenharmony_ci      zip_utils.unzip('test.zip', 'output')
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci      # Remove the files/dirs we don't expect to see in output, so that we can
58cb93a386Sopenharmony_ci      # use self._compare_trees to check the results.
59cb93a386Sopenharmony_ci      fw.remove(os.path.join('.git', 'index'))
60cb93a386Sopenharmony_ci      fw.remove('.git')
61cb93a386Sopenharmony_ci      fw.remove('.DS_STORE')
62cb93a386Sopenharmony_ci      fw.remove('leftover.pyc')
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci      # Compare results.
65cb93a386Sopenharmony_ci      test_utils.compare_trees(self, 'input', 'output')
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci  def test_nonexistent_dir(self):
68cb93a386Sopenharmony_ci    with utils.tmp_dir():
69cb93a386Sopenharmony_ci      with self.assertRaises(IOError):
70cb93a386Sopenharmony_ci        zip_utils.zip('input', 'test.zip')
71cb93a386Sopenharmony_ci
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ciif __name__ == '__main__':
74cb93a386Sopenharmony_ci  unittest.main()
75