1#!/usr/bin/env python 2# Copyright 2013 The Chromium Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import unittest 7from cStringIO import StringIO 8from zipfile import ZipFile 9 10from compiled_file_system import CompiledFileSystem 11from directory_zipper import DirectoryZipper 12from file_system import FileNotFoundError 13from test_file_system import TestFileSystem 14from object_store_creator import ObjectStoreCreator 15 16 17_TEST_DATA = { 18 'top': { 19 'one.txt': 'one.txt contents', 20 'two': { 21 'three.txt': 'three.txt contents', 22 'four.txt': 'four.txt contents', 23 }, 24 } 25} 26 27 28class DirectoryZipperTest(unittest.TestCase): 29 def setUp(self): 30 self._directory_zipper = DirectoryZipper( 31 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()), 32 TestFileSystem(_TEST_DATA)) 33 34 def testTopZip(self): 35 top_zip = ZipFile(StringIO(self._directory_zipper.Zip('top/').Get())) 36 self.assertEqual(['top/one.txt', 'top/two/four.txt', 'top/two/three.txt'], 37 sorted(top_zip.namelist())) 38 self.assertEqual('one.txt contents', top_zip.read('top/one.txt')) 39 self.assertEqual('three.txt contents', top_zip.read('top/two/three.txt')) 40 self.assertEqual('four.txt contents', top_zip.read('top/two/four.txt')) 41 42 def testTwoZip(self): 43 two_zip = ZipFile(StringIO(self._directory_zipper.Zip('top/two/').Get())) 44 self.assertEqual(['two/four.txt', 'two/three.txt'], 45 sorted(two_zip.namelist())) 46 self.assertEqual('three.txt contents', two_zip.read('two/three.txt')) 47 self.assertEqual('four.txt contents', two_zip.read('two/four.txt')) 48 49 def testNotFound(self): 50 self.assertRaises(FileNotFoundError, 51 self._directory_zipper.Zip('notfound/').Get) 52 53 54if __name__ == '__main__': 55 unittest.main() 56