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

Side by Side Diff: prebuilt_unittest.py

Issue 6261013: Update prebuilt.py to handle files not existing. Add a unittest to test the case (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils@master
Patch Set: Created 9 years, 11 months 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
« no previous file with comments | « prebuilt.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
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import copy 6 import copy
7 import mox 7 import mox
8 import os 8 import os
9 import prebuilt 9 import prebuilt
10 import shutil 10 import shutil
(...skipping 27 matching lines...) Expand all
38 'portage portage-20100310.tar.bz2', 38 'portage portage-20100310.tar.bz2',
39 'COMPILE_FLAGS="some_value=some_other"', 39 'COMPILE_FLAGS="some_value=some_other"',
40 ] 40 ]
41 temp_fd, self.version_file = tempfile.mkstemp() 41 temp_fd, self.version_file = tempfile.mkstemp()
42 os.write(temp_fd, '\n'.join(self.contents_str)) 42 os.write(temp_fd, '\n'.join(self.contents_str))
43 os.close(temp_fd) 43 os.close(temp_fd)
44 44
45 def tearDown(self): 45 def tearDown(self):
46 os.remove(self.version_file) 46 os.remove(self.version_file)
47 47
48 def _read_version_file(self): 48 def _read_version_file(self, version_file=None):
49 """Read the contents of self.version_file and return as a list.""" 49 """Read the contents of self.version_file and return as a list."""
50 version_fh = open(self.version_file) 50 if not version_file:
51 version_file = self.version_file
52
53 version_fh = open(version_file)
51 try: 54 try:
52 return [line.strip() for line in version_fh.readlines()] 55 return [line.strip() for line in version_fh.readlines()]
53 finally: 56 finally:
54 version_fh.close() 57 version_fh.close()
55 58
56 def _verify_key_pair(self, key, val): 59 def _verify_key_pair(self, key, val):
57 file_contents = self._read_version_file() 60 file_contents = self._read_version_file()
58 # ensure key for verify is wrapped on quotes 61 # ensure key for verify is wrapped on quotes
59 if '"' not in val: 62 if '"' not in val:
60 val = '"%s"' % val 63 val = '"%s"' % val
(...skipping 18 matching lines...) Expand all
79 print self.version_file 82 print self.version_file
80 83
81 def testUpdateVariable(self): 84 def testUpdateVariable(self):
82 """Test updating a variable that already exists.""" 85 """Test updating a variable that already exists."""
83 key, val = self.contents_str[2].split('=') 86 key, val = self.contents_str[2].split('=')
84 new_val = 'test_update' 87 new_val = 'test_update'
85 self._verify_key_pair(key, val) 88 self._verify_key_pair(key, val)
86 prebuilt.UpdateLocalFile(self.version_file, new_val) 89 prebuilt.UpdateLocalFile(self.version_file, new_val)
87 self._verify_key_pair(key, new_val) 90 self._verify_key_pair(key, new_val)
88 91
92 def testUpdateNonExistentFile(self):
93 key = 'PORTAGE_BINHOST'
94 value = '1234567'
95 non_existent_file = tempfile.mktemp()
96 try:
97 prebuilt.UpdateLocalFile(non_existent_file, value)
98 file_contents = self._read_version_file(non_existent_file)
99 self.assertEqual(file_contents, ['%s=%s' % (key, value)])
100 finally:
101 if os.path.exists(non_existent_file):
102 os.remove(non_existent_file)
103
89 104
90 class TestPrebuiltFilters(unittest.TestCase): 105 class TestPrebuiltFilters(unittest.TestCase):
91 106
92 def setUp(self): 107 def setUp(self):
93 self.tmp_dir = tempfile.mkdtemp() 108 self.tmp_dir = tempfile.mkdtemp()
94 self.private_dir = os.path.join(self.tmp_dir, 109 self.private_dir = os.path.join(self.tmp_dir,
95 prebuilt._PRIVATE_OVERLAY_DIR) 110 prebuilt._PRIVATE_OVERLAY_DIR)
96 self.private_structure_base = 'chromeos-overlay/chromeos-base' 111 self.private_structure_base = 'chromeos-overlay/chromeos-base'
97 self.private_pkgs = ['test-package/salt-flavor-0.1.r3.ebuild', 112 self.private_pkgs = ['test-package/salt-flavor-0.1.r3.ebuild',
98 'easy/alpha_beta-0.1.41.r3.ebuild', 113 'easy/alpha_beta-0.1.41.r3.ebuild',
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 pkgindex = SimplePackageIndex() 331 pkgindex = SimplePackageIndex()
317 self.mox.StubOutWithMock(pkgindex, 'Write') 332 self.mox.StubOutWithMock(pkgindex, 'Write')
318 pkgindex.Write(mox.IgnoreArg()) 333 pkgindex.Write(mox.IgnoreArg())
319 self.mox.ReplayAll() 334 self.mox.ReplayAll()
320 f = pkgindex.WriteToNamedTemporaryFile() 335 f = pkgindex.WriteToNamedTemporaryFile()
321 self.assertEqual(f.read(), '') 336 self.assertEqual(f.read(), '')
322 337
323 338
324 if __name__ == '__main__': 339 if __name__ == '__main__':
325 unittest.main() 340 unittest.main()
OLDNEW
« no previous file with comments | « prebuilt.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698