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
12import filecmp
13import os
14import test_utils
15import unittest
16import utils
17import uuid
18import zip_utils
19
20
21class 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)
34
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
73if __name__ == '__main__':
74  unittest.main()
75