fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / storage / test_zip.py
blob815d979e6e636a98950aa8c3189ed42c9c31bfaf
1 #!/usr/bin/env python
3 """Tests for the zip storage module"""
5 from translate.storage import directory
6 from translate.storage import zip
7 import os
8 from zipfile import ZipFile
10 class TestZIPFile(object):
11 """A test class to test the zip class that provides the directory interface."""
13 def setup_method(self, method):
14 """sets up a test directory"""
15 print "setup_method called on", self.__class__.__name__
16 self.testzip = "%s_testzip.zip" % (self.__class__.__name__)
17 self.cleardir(self.testzip)
18 self.zip = ZipFile(self.testzip, mode="w")
20 def teardown_method(self, method):
21 """removes the attributes set up by setup_method"""
22 self.cleardir(self.testzip)
24 def cleardir(self, dirname):
25 """removes the given directory"""
26 if os.path.exists(self.testzip):
27 os.remove(self.testzip)
28 assert not os.path.exists(self.testzip)
30 def touchfiles(self, dir, filenames, content="", last=False):
31 for filename in filenames:
32 if dir:
33 self.zip.writestr(os.path.join(dir, filename), content)
34 else:
35 self.zip.writestr(filename, content)
36 if last:
37 self.zip.close()
39 def mkdir(self, dir):
40 """Makes a directory inside self.testzip."""
41 pass
43 def test_created(self):
44 """test that the directory actually exists"""
45 print self.testzip
46 assert os.path.isfile(self.testzip)
48 def test_basic(self):
49 """Tests basic functionality."""
50 files = ["a.po", "b.po", "c.po"]
51 self.touchfiles(None, files, last=True)
53 d = zip.ZIPFile(self.testzip)
54 filenames = [name for dir, name in d.getfiles()]
55 assert filenames == files
57 def test_structure(self):
58 """Tests a small directory structure."""
59 files = ["a.po", "b.po", "c.po"]
60 self.touchfiles(self.testzip, files)
61 self.mkdir("bla")
62 self.touchfiles(os.path.join(self.testzip, "bla"), files, last=True)
64 d = zip.ZIPFile(self.testzip)
65 filenames = [name for dir, name in d.getfiles()]
66 assert filenames == files*2
68 def test_getunits(self):
69 """Tests basic functionality."""
70 files = ["a.po", "b.po", "c.po"]
71 posource = '''msgid "bla"\nmsgstr "blabla"\n'''
72 self.touchfiles(self.testzip, files, posource, last=True)
74 d = zip.ZIPFile(self.testzip)
75 for unit in d.getunits():
76 assert unit.target == "blabla"
77 assert len(d.getunits()) == 3