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.
14 SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
15 PARENT_DIR
= os
.path
.dirname(SCRIPT_DIR
)
17 sys
.path
.append(PARENT_DIR
)
21 class RunZipError(subprocess
.CalledProcessError
):
22 def __init__(self
, retcode
, command
, output
, error_output
):
23 subprocess
.CalledProcessError
.__init
__(self
, retcode
, command
)
25 self
.error_output
= error_output
28 msg
= subprocess
.CalledProcessError
.__str
__(self
)
29 msg
+= '.\nstdout: """%s"""' % (self
.output
,)
30 msg
+= '.\nstderr: """%s"""' % (self
.error_output
,)
34 def RunZip(args
, cwd
):
35 command
= [sys
.executable
, 'oshelpers.py', 'zip'] + args
36 process
= subprocess
.Popen(stdout
=subprocess
.PIPE
,
37 stderr
=subprocess
.PIPE
,
40 output
, error_output
= process
.communicate()
41 retcode
= process
.returncode
44 raise RunZipError(retcode
, command
, output
, error_output
)
45 return output
, error_output
48 class TestZip(unittest
.TestCase
):
50 # make zipname -> "testFooBar.zip"
51 self
.zipname
= self
.id().split('.')[-1] + '.zip'
53 self
.tempdir
= tempfile
.mkdtemp()
54 shutil
.copy(os
.path
.join(PARENT_DIR
, 'oshelpers.py'),
60 shutil
.rmtree(self
.tempdir
)
62 def GetTempPath(self
, basename
):
63 return os
.path
.join(self
.tempdir
, basename
)
65 def MakeFile(self
, rel_path
, size
):
66 with
open(os
.path
.join(self
.tempdir
, rel_path
), 'wb') as f
:
70 def RunZip(self
, *args
):
71 return RunZip(*args
, cwd
=self
.tempdir
)
73 def OpenZipFile(self
):
74 self
.zipfile
= zipfile
.ZipFile(self
.GetTempPath(self
.zipname
), 'r')
76 def CloseZipFile(self
):
80 def GetZipInfo(self
, path
):
81 return self
.zipfile
.getinfo(oshelpers
.OSMakeZipPath(path
))
84 def testNothingToDo(self
):
85 self
.assertRaises(subprocess
.CalledProcessError
, self
.RunZip
,
86 [self
.zipname
, 'nonexistent_file'])
87 self
.assertFalse(os
.path
.exists(self
.zipname
))
89 def testAddSomeFiles(self
):
90 file1
= self
.MakeFile('file1', 1024)
91 file2
= self
.MakeFile('file2', 3354)
92 self
.RunZip([self
.zipname
, file1
, file2
])
94 self
.assertEqual(len(self
.zipfile
.namelist()), 2)
95 self
.assertEqual(self
.GetZipInfo(file1
).file_size
, 1024)
96 self
.assertEqual(self
.GetZipInfo(file2
).file_size
, 3354)
97 # make sure files are added in order
98 self
.assertEqual(self
.zipfile
.namelist()[0], file1
)
100 def testAddFilesWithGlob(self
):
101 self
.MakeFile('file1', 1024)
102 self
.MakeFile('file2', 3354)
103 self
.RunZip([self
.zipname
, 'file*'])
105 self
.assertEqual(len(self
.zipfile
.namelist()), 2)
107 def testAddDir(self
):
108 os
.mkdir(self
.GetTempPath('dir1'))
109 self
.RunZip([self
.zipname
, 'dir1'])
111 self
.assertEqual(len(self
.zipfile
.namelist()), 1)
112 self
.assertRaises(KeyError, self
.zipfile
.getinfo
, 'dir1')
113 self
.zipfile
.getinfo('dir1/')
115 def testAddRecursive(self
):
116 os
.mkdir(self
.GetTempPath('dir1'))
117 self
.MakeFile(os
.path
.join('dir1', 'file1'), 256)
118 os
.mkdir(self
.GetTempPath(os
.path
.join('dir1', 'dir2')))
119 self
.MakeFile(os
.path
.join('dir1', 'dir2', 'file2'), 1234)
120 self
.RunZip([self
.zipname
, '-r', 'dir1'])
122 self
.assertEqual(len(self
.zipfile
.namelist()), 4)
124 def testUpdate(self
):
125 file1
= self
.MakeFile('file1', 1223)
126 self
.RunZip([self
.zipname
, file1
])
128 self
.assertEqual(self
.GetZipInfo(file1
).file_size
, 1223)
130 file1
= self
.MakeFile('file1', 2334)
131 self
.RunZip([self
.zipname
, file1
])
133 self
.assertEqual(len(self
.zipfile
.namelist()), 1)
134 self
.assertEqual(self
.GetZipInfo(file1
).file_size
, 2334)
136 def testUpdateOneFileOutOfMany(self
):
137 file1
= self
.MakeFile('file1', 128)
138 file2
= self
.MakeFile('file2', 256)
139 file3
= self
.MakeFile('file3', 512)
140 file4
= self
.MakeFile('file4', 1024)
141 self
.RunZip([self
.zipname
, file1
, file2
, file3
, file4
])
143 self
.assertEqual(len(self
.zipfile
.namelist()), 4)
146 file3
= self
.MakeFile('file3', 768)
147 self
.RunZip([self
.zipname
, file3
])
149 self
.assertEqual(len(self
.zipfile
.namelist()), 4)
150 self
.assertEqual(self
.zipfile
.namelist()[0], file1
)
151 self
.assertEqual(self
.GetZipInfo(file1
).file_size
, 128)
152 self
.assertEqual(self
.zipfile
.namelist()[1], file2
)
153 self
.assertEqual(self
.GetZipInfo(file2
).file_size
, 256)
154 self
.assertEqual(self
.zipfile
.namelist()[2], file3
)
155 self
.assertEqual(self
.GetZipInfo(file3
).file_size
, 768)
156 self
.assertEqual(self
.zipfile
.namelist()[3], file4
)
157 self
.assertEqual(self
.GetZipInfo(file4
).file_size
, 1024)
159 def testUpdateSubdirectory(self
):
160 os
.mkdir(self
.GetTempPath('dir1'))
161 file1
= self
.MakeFile(os
.path
.join('dir1', 'file1'), 256)
162 os
.mkdir(self
.GetTempPath(os
.path
.join('dir1', 'dir2')))
163 self
.MakeFile(os
.path
.join('dir1', 'dir2', 'file2'), 1234)
164 self
.RunZip([self
.zipname
, '-r', 'dir1'])
166 self
.assertEqual(len(self
.zipfile
.namelist()), 4)
167 self
.assertEqual(self
.GetZipInfo(file1
).file_size
, 256)
170 self
.MakeFile(file1
, 2560)
171 self
.RunZip([self
.zipname
, file1
])
173 self
.assertEqual(len(self
.zipfile
.namelist()), 4)
174 self
.assertEqual(self
.GetZipInfo(file1
).file_size
, 2560)
176 def testAppend(self
):
177 file1
= self
.MakeFile('file1', 128)
178 file2
= self
.MakeFile('file2', 256)
179 self
.RunZip([self
.zipname
, file1
, file2
])
181 self
.assertEqual(len(self
.zipfile
.namelist()), 2)
184 file3
= self
.MakeFile('file3', 768)
185 self
.RunZip([self
.zipname
, file3
])
187 self
.assertEqual(len(self
.zipfile
.namelist()), 3)
190 if __name__
== '__main__':