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.
17 class RunZipError(subprocess
.CalledProcessError
):
18 def __init__(self
, retcode
, command
, output
, error_output
):
19 subprocess
.CalledProcessError
.__init
__(self
, retcode
, command
)
21 self
.error_output
= error_output
24 msg
= subprocess
.CalledProcessError
.__str
__(self
)
25 msg
+= '.\nstdout: """%s"""' % (self
.output
,)
26 msg
+= '.\nstderr: """%s"""' % (self
.error_output
,)
29 def RunZip(args
, cwd
):
30 command
= [sys
.executable
,
31 os
.path
.join(os
.path
.dirname(__file__
), 'oshelpers.py'),
33 process
= subprocess
.Popen(stdout
=subprocess
.PIPE
,
34 stderr
=subprocess
.PIPE
,
37 output
, error_output
= process
.communicate()
38 retcode
= process
.poll()
41 raise RunZipError(retcode
, command
, output
, error_output
)
42 return output
, error_output
45 class TestZip(unittest
.TestCase
):
47 # make zipname -> "testFooBar.zip"
48 self
.zipname
= self
.id().split('.')[-1] + '.zip'
50 self
.tempdir
= tempfile
.mkdtemp()
51 shutil
.copy(os
.path
.join(os
.path
.dirname(__file__
), 'oshelpers.py'),
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
:
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
):
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
])
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*'])
102 self
.assertEqual(len(self
.zipfile
.namelist()), 2)
104 def testAddDir(self
):
105 os
.mkdir(self
.GetTempPath('dir1'))
106 self
.RunZip([self
.zipname
, 'dir1'])
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'])
117 self
.assertEqual(len(self
.zipfile
.namelist()), 4)
119 def testUpdate(self
):
120 file1
= self
.MakeFile('file1', 1223)
121 self
.RunZip([self
.zipname
, file1
])
123 self
.assertEqual(self
.GetZipInfo(file1
).file_size
, 1223)
125 file1
= self
.MakeFile('file1', 2334)
126 self
.RunZip([self
.zipname
, file1
])
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
])
138 self
.assertEqual(len(self
.zipfile
.namelist()), 4)
141 file3
= self
.MakeFile('file3', 768)
142 self
.RunZip([self
.zipname
, file3
])
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'])
161 self
.assertEqual(len(self
.zipfile
.namelist()), 4)
162 self
.assertEqual(self
.GetZipInfo(file1
).file_size
, 256)
165 self
.MakeFile(file1
, 2560)
166 self
.RunZip([self
.zipname
, file1
])
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
])
176 self
.assertEqual(len(self
.zipfile
.namelist()), 2)
179 file3
= self
.MakeFile('file3', 768)
180 self
.RunZip([self
.zipname
, file3
])
182 self
.assertEqual(len(self
.zipfile
.namelist()), 3)
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__':