Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / native_client_sdk / src / tools / tests / oshelpers_test.py
blobe73f97871531fad4d23c335e8e988833e248475f
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 os
7 import shutil
8 import subprocess
9 import sys
10 import tempfile
11 import unittest
12 import zipfile
14 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
15 PARENT_DIR = os.path.dirname(SCRIPT_DIR)
17 sys.path.append(PARENT_DIR)
19 import oshelpers
21 class RunZipError(subprocess.CalledProcessError):
22 def __init__(self, retcode, command, output, error_output):
23 subprocess.CalledProcessError.__init__(self, retcode, command)
24 self.output = output
25 self.error_output = error_output
27 def __str__(self):
28 msg = subprocess.CalledProcessError.__str__(self)
29 msg += '.\nstdout: """%s"""' % (self.output,)
30 msg += '.\nstderr: """%s"""' % (self.error_output,)
31 return msg
34 def RunZip(args, cwd):
35 command = [sys.executable, 'oshelpers.py', 'zip'] + args
36 process = subprocess.Popen(stdout=subprocess.PIPE,
37 stderr=subprocess.PIPE,
38 args=command,
39 cwd=cwd)
40 output, error_output = process.communicate()
41 retcode = process.returncode
43 if retcode:
44 raise RunZipError(retcode, command, output, error_output)
45 return output, error_output
48 class TestZip(unittest.TestCase):
49 def setUp(self):
50 # make zipname -> "testFooBar.zip"
51 self.zipname = self.id().split('.')[-1] + '.zip'
52 self.zipfile = None
53 self.tempdir = tempfile.mkdtemp()
54 shutil.copy(os.path.join(PARENT_DIR, 'oshelpers.py'),
55 self.tempdir)
57 def tearDown(self):
58 if self.zipfile:
59 self.zipfile.close()
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:
67 f.write('0' * size)
68 return rel_path
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):
77 self.zipfile.close()
78 self.zipfile = None
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])
93 self.OpenZipFile()
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*'])
104 self.OpenZipFile()
105 self.assertEqual(len(self.zipfile.namelist()), 2)
107 def testAddDir(self):
108 os.mkdir(self.GetTempPath('dir1'))
109 self.RunZip([self.zipname, 'dir1'])
110 self.OpenZipFile()
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'])
121 self.OpenZipFile()
122 self.assertEqual(len(self.zipfile.namelist()), 4)
124 def testUpdate(self):
125 file1 = self.MakeFile('file1', 1223)
126 self.RunZip([self.zipname, file1])
127 self.OpenZipFile()
128 self.assertEqual(self.GetZipInfo(file1).file_size, 1223)
130 file1 = self.MakeFile('file1', 2334)
131 self.RunZip([self.zipname, file1])
132 self.OpenZipFile()
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])
142 self.OpenZipFile()
143 self.assertEqual(len(self.zipfile.namelist()), 4)
144 self.CloseZipFile()
146 file3 = self.MakeFile('file3', 768)
147 self.RunZip([self.zipname, file3])
148 self.OpenZipFile()
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'])
165 self.OpenZipFile()
166 self.assertEqual(len(self.zipfile.namelist()), 4)
167 self.assertEqual(self.GetZipInfo(file1).file_size, 256)
168 self.CloseZipFile()
170 self.MakeFile(file1, 2560)
171 self.RunZip([self.zipname, file1])
172 self.OpenZipFile()
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])
180 self.OpenZipFile()
181 self.assertEqual(len(self.zipfile.namelist()), 2)
182 self.CloseZipFile()
184 file3 = self.MakeFile('file3', 768)
185 self.RunZip([self.zipname, file3])
186 self.OpenZipFile()
187 self.assertEqual(len(self.zipfile.namelist()), 3)
190 if __name__ == '__main__':
191 unittest.main()