Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(273)

Side by Side Diff: native_client_sdk/src/tools/zip_test.py

Issue 11571032: [NaCl SDK] cleanup python unittests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « native_client_sdk/src/tools/tests/test_httpd.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
5
6 import os
7 import oshelpers
8 import shutil
9 import subprocess
10 import sys
11 import tempfile
12 import unittest
13 import zipfile
14
15
16 class RunZipError(subprocess.CalledProcessError):
17 def __init__(self, retcode, command, output, error_output):
18 subprocess.CalledProcessError.__init__(self, retcode, command)
19 self.output = output
20 self.error_output = error_output
21
22 def __str__(self):
23 msg = subprocess.CalledProcessError.__str__(self)
24 msg += '.\nstdout: """%s"""' % (self.output,)
25 msg += '.\nstderr: """%s"""' % (self.error_output,)
26 return msg
27
28
29 def RunZip(args, cwd):
30 command = [sys.executable, 'oshelpers.py', 'zip'] + args
31 process = subprocess.Popen(stdout=subprocess.PIPE,
32 stderr=subprocess.PIPE,
33 args=command,
34 cwd=cwd)
35 output, error_output = process.communicate()
36 retcode = process.returncode
37
38 if retcode:
39 raise RunZipError(retcode, command, output, error_output)
40 return output, error_output
41
42
43 class TestZip(unittest.TestCase):
44 def setUp(self):
45 # make zipname -> "testFooBar.zip"
46 self.zipname = self.id().split('.')[-1] + '.zip'
47 self.zipfile = None
48 self.tempdir = tempfile.mkdtemp()
49 shutil.copy(os.path.join(os.path.dirname(__file__), 'oshelpers.py'),
50 self.tempdir)
51
52 def tearDown(self):
53 if self.zipfile:
54 self.zipfile.close()
55 shutil.rmtree(self.tempdir)
56
57 def GetTempPath(self, basename):
58 return os.path.join(self.tempdir, basename)
59
60 def MakeFile(self, rel_path, size):
61 with open(os.path.join(self.tempdir, rel_path), 'wb') as f:
62 f.write('0' * size)
63 return rel_path
64
65 def RunZip(self, *args):
66 return RunZip(*args, cwd=self.tempdir)
67
68 def OpenZipFile(self):
69 self.zipfile = zipfile.ZipFile(self.GetTempPath(self.zipname), 'r')
70
71 def CloseZipFile(self):
72 self.zipfile.close()
73 self.zipfile = None
74
75 def GetZipInfo(self, path):
76 return self.zipfile.getinfo(oshelpers.OSMakeZipPath(path))
77
78
79 def testNothingToDo(self):
80 self.assertRaises(subprocess.CalledProcessError, self.RunZip,
81 [self.zipname, 'nonexistent_file'])
82 self.assertFalse(os.path.exists(self.zipname))
83
84 def testAddSomeFiles(self):
85 file1 = self.MakeFile('file1', 1024)
86 file2 = self.MakeFile('file2', 3354)
87 self.RunZip([self.zipname, file1, file2])
88 self.OpenZipFile()
89 self.assertEqual(len(self.zipfile.namelist()), 2)
90 self.assertEqual(self.GetZipInfo(file1).file_size, 1024)
91 self.assertEqual(self.GetZipInfo(file2).file_size, 3354)
92 # make sure files are added in order
93 self.assertEqual(self.zipfile.namelist()[0], file1)
94
95 def testAddFilesWithGlob(self):
96 self.MakeFile('file1', 1024)
97 self.MakeFile('file2', 3354)
98 self.RunZip([self.zipname, 'file*'])
99 self.OpenZipFile()
100 self.assertEqual(len(self.zipfile.namelist()), 2)
101
102 def testAddDir(self):
103 os.mkdir(self.GetTempPath('dir1'))
104 self.RunZip([self.zipname, 'dir1'])
105 self.OpenZipFile()
106 self.assertEqual(len(self.zipfile.namelist()), 1)
107 self.assertRaises(KeyError, self.zipfile.getinfo, 'dir1')
108 self.zipfile.getinfo('dir1/')
109
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)
118
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)
124
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)
130
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()
140
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)
153
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 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()
164
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)
170
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()
178
179 file3 = self.MakeFile('file3', 768)
180 self.RunZip([self.zipname, file3])
181 self.OpenZipFile()
182 self.assertEqual(len(self.zipfile.namelist()), 3)
183
184
185 if __name__ == '__main__':
186 unittest.main()
OLDNEW
« no previous file with comments | « native_client_sdk/src/tools/tests/test_httpd.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698