Rename GDataSystemService to DriveSystemService
[chromium-blink-merge.git] / native_client_sdk / src / tools / zip_test.py
blob76dffba01c10482ebe6a417fcc277d7939bfb634
1 #!/usr/bin/env python
2 # Copyright (c) 2012 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.
6 import doctest
7 import os
8 import oshelpers
9 import shutil
10 import subprocess
11 import sys
12 import tempfile
13 import unittest
14 import zipfile
17 class RunZipError(subprocess.CalledProcessError):
18 def __init__(self, retcode, command, output, error_output):
19 subprocess.CalledProcessError.__init__(self, retcode, command)
20 self.output = output
21 self.error_output = error_output
23 def __str__(self):
24 msg = subprocess.CalledProcessError.__str__(self)
25 msg += '.\nstdout: """%s"""' % (self.output,)
26 msg += '.\nstderr: """%s"""' % (self.error_output,)
27 return msg
29 def RunZip(args, cwd):
30 command = [sys.executable,
31 os.path.join(os.path.dirname(__file__), 'oshelpers.py'),
32 'zip'] + args
33 process = subprocess.Popen(stdout=subprocess.PIPE,
34 stderr=subprocess.PIPE,
35 args=command,
36 cwd=cwd)
37 output, error_output = process.communicate()
38 retcode = process.poll()
40 if retcode != 0:
41 raise RunZipError(retcode, command, output, error_output)
42 return output, error_output
45 class TestZip(unittest.TestCase):
46 def setUp(self):
47 # make zipname -> "testFooBar.zip"
48 self.zipname = self.id().split('.')[-1] + '.zip'
49 self.zipfile = None
50 self.tempdir = tempfile.mkdtemp()
51 shutil.copy(os.path.join(os.path.dirname(__file__), 'oshelpers.py'),
52 self.tempdir)
54 def tearDown(self):
55 if self.zipfile:
56 self.zipfile.close()
57 shutil.rmtree(self.tempdir)
59 def GetTempPath(self, basename):
60 return os.path.join(self.tempdir, basename)
62 def MakeFile(self, rel_path, size):
63 with open(os.path.join(self.tempdir, rel_path), 'wb') as f:
64 f.write('0' * size)
65 return rel_path
67 def RunZip(self, *args):
68 return RunZip(*args, cwd=self.tempdir)
70 def OpenZipFile(self):
71 self.zipfile = zipfile.ZipFile(self.GetTempPath(self.zipname), 'r')
73 def CloseZipFile(self):
74 self.zipfile.close()
75 self.zipfile = None
77 def GetZipInfo(self, path):
78 return self.zipfile.getinfo(oshelpers.OSMakeZipPath(path))
81 def testNothingToDo(self):
82 self.assertRaises(subprocess.CalledProcessError, self.RunZip,
83 [self.zipname, 'nonexistent_file'])
84 self.assertFalse(os.path.exists(self.zipname))
86 def testAddSomeFiles(self):
87 file1 = self.MakeFile('file1', 1024)
88 file2 = self.MakeFile('file2', 3354)
89 self.RunZip([self.zipname, file1, file2])
90 self.OpenZipFile()
91 self.assertEqual(len(self.zipfile.namelist()), 2)
92 self.assertEqual(self.GetZipInfo(file1).file_size, 1024)
93 self.assertEqual(self.GetZipInfo(file2).file_size, 3354)
94 # make sure files are added in order
95 self.assertEqual(self.zipfile.namelist()[0], file1)
97 def testAddFilesWithGlob(self):
98 file1 = self.MakeFile('file1', 1024)
99 file2 = self.MakeFile('file2', 3354)
100 self.RunZip([self.zipname, 'file*'])
101 self.OpenZipFile()
102 self.assertEqual(len(self.zipfile.namelist()), 2)
104 def testAddDir(self):
105 os.mkdir(self.GetTempPath('dir1'))
106 self.RunZip([self.zipname, 'dir1'])
107 self.OpenZipFile()
108 self.assertEqual(len(self.zipfile.namelist()), 1)
110 def testAddRecursive(self):
111 os.mkdir(self.GetTempPath('dir1'))
112 self.MakeFile(os.path.join('dir1', 'file1'), 256)
113 os.mkdir(self.GetTempPath(os.path.join('dir1', 'dir2')))
114 self.MakeFile(os.path.join('dir1', 'dir2', 'file2'), 1234)
115 self.RunZip([self.zipname, '-r', 'dir1'])
116 self.OpenZipFile()
117 self.assertEqual(len(self.zipfile.namelist()), 4)
119 def testUpdate(self):
120 file1 = self.MakeFile('file1', 1223)
121 self.RunZip([self.zipname, file1])
122 self.OpenZipFile()
123 self.assertEqual(self.GetZipInfo(file1).file_size, 1223)
125 file1 = self.MakeFile('file1', 2334)
126 self.RunZip([self.zipname, file1])
127 self.OpenZipFile()
128 self.assertEqual(len(self.zipfile.namelist()), 1)
129 self.assertEqual(self.GetZipInfo(file1).file_size, 2334)
131 def testUpdateOneFileOutOfMany(self):
132 file1 = self.MakeFile('file1', 128)
133 file2 = self.MakeFile('file2', 256)
134 file3 = self.MakeFile('file3', 512)
135 file4 = self.MakeFile('file4', 1024)
136 self.RunZip([self.zipname, file1, file2, file3, file4])
137 self.OpenZipFile()
138 self.assertEqual(len(self.zipfile.namelist()), 4)
139 self.CloseZipFile()
141 file3 = self.MakeFile('file3', 768)
142 self.RunZip([self.zipname, file3])
143 self.OpenZipFile()
144 self.assertEqual(len(self.zipfile.namelist()), 4)
145 self.assertEqual(self.zipfile.namelist()[0], file1)
146 self.assertEqual(self.GetZipInfo(file1).file_size, 128)
147 self.assertEqual(self.zipfile.namelist()[1], file2)
148 self.assertEqual(self.GetZipInfo(file2).file_size, 256)
149 self.assertEqual(self.zipfile.namelist()[2], file3)
150 self.assertEqual(self.GetZipInfo(file3).file_size, 768)
151 self.assertEqual(self.zipfile.namelist()[3], file4)
152 self.assertEqual(self.GetZipInfo(file4).file_size, 1024)
154 def testUpdateSubdirectory(self):
155 os.mkdir(self.GetTempPath('dir1'))
156 file1 = self.MakeFile(os.path.join('dir1', 'file1'), 256)
157 os.mkdir(self.GetTempPath(os.path.join('dir1', 'dir2')))
158 file2 = self.MakeFile(os.path.join('dir1', 'dir2', 'file2'), 1234)
159 self.RunZip([self.zipname, '-r', 'dir1'])
160 self.OpenZipFile()
161 self.assertEqual(len(self.zipfile.namelist()), 4)
162 self.assertEqual(self.GetZipInfo(file1).file_size, 256)
163 self.CloseZipFile()
165 self.MakeFile(file1, 2560)
166 self.RunZip([self.zipname, file1])
167 self.OpenZipFile()
168 self.assertEqual(len(self.zipfile.namelist()), 4)
169 self.assertEqual(self.GetZipInfo(file1).file_size, 2560)
171 def testAppend(self):
172 file1 = self.MakeFile('file1', 128)
173 file2 = self.MakeFile('file2', 256)
174 self.RunZip([self.zipname, file1, file2])
175 self.OpenZipFile()
176 self.assertEqual(len(self.zipfile.namelist()), 2)
177 self.CloseZipFile()
179 file3 = self.MakeFile('file3', 768)
180 self.RunZip([self.zipname, file3])
181 self.OpenZipFile()
182 self.assertEqual(len(self.zipfile.namelist()), 3)
185 def main():
186 suite = unittest.TestLoader().loadTestsFromTestCase(TestZip)
187 suite.addTests(doctest.DocTestSuite(oshelpers))
188 result = unittest.TextTestRunner(verbosity=2).run(suite)
189 return int(not result.wasSuccessful())
192 if __name__=='__main__':
193 sys.exit(main())