diff options
author | Cassidy Burden <cburden@google.com> | 2016-06-14 16:19:18 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-06-15 11:00:50 -0700 |
commit | 242c6cbf1a3c7d1d09cde13281d8bb36d7ef6001 (patch) | |
tree | 0f0566be3dd0ad326fd7eee649d100604392ecea /binary_search_tool | |
parent | 9c665a01b55ee4e672b29ff352989a640eac084f (diff) | |
download | toolchain-utils-242c6cbf1a3c7d1d09cde13281d8bb36d7ef6001.tar.gz |
Fix binary search tool's temp file creation
Fix the binary search tool so that temp files are created properly. This
means that temp files made for the switch scripts should be automatically
deleted once the switch script completes.
TEST=New unit test for temp file deletion
Change-Id: I5d36bfa07e274cd79965f182d85d6a7ea3006c85
Reviewed-on: https://chrome-internal-review.googlesource.com/264735
Commit-Ready: Cassidy Burden <cburden@google.com>
Tested-by: Cassidy Burden <cburden@google.com>
Reviewed-by: Caroline Tice <cmtice@google.com>
Diffstat (limited to 'binary_search_tool')
-rwxr-xr-x | binary_search_tool/binary_search_state.py | 22 | ||||
-rwxr-xr-x | binary_search_tool/test/binary_search_tool_tester.py | 17 | ||||
-rwxr-xr-x | binary_search_tool/test/switch_tmp.py | 34 |
3 files changed, 63 insertions, 10 deletions
diff --git a/binary_search_tool/binary_search_state.py b/binary_search_tool/binary_search_state.py index ecb3dd7e..7803cacb 100755 --- a/binary_search_tool/binary_search_state.py +++ b/binary_search_tool/binary_search_state.py @@ -103,15 +103,16 @@ class BinarySearchState(object): def RunSwitchScript(self, switch_script, item_list): if self.file_args: - temp_file = tempfile.mktemp() - f = open(temp_file, 'wb') - f.write('\n'.join(item_list)) - f.close() - command = '%s %s' % (switch_script, temp_file) + with tempfile.NamedTemporaryFile() as f: + f.write('\n'.join(item_list)) + f.flush() + command = '%s %s' % (switch_script, f.name) + ret, _, _ = self.ce.RunCommandWExceptionCleanup( + command, print_to_console=self.verbose) else: command = '%s %s' % (switch_script, ' '.join(item_list)) - ret, _, _ = self.ce.RunCommandWExceptionCleanup( - command, print_to_console=self.verbose) + ret, _, _ = self.ce.RunCommandWExceptionCleanup( + command, print_to_console=self.verbose) assert ret == 0, 'Switch script %s returned %d' % (switch_script, ret) def TestScript(self): @@ -301,9 +302,9 @@ class BinarySearchState(object): class MockBinarySearchState(BinarySearchState): """Mock class for BinarySearchState.""" - def __init__(self): + def __init__(self, **kwargs): # Initialize all arguments to None - kwargs = { + default_kwargs = { 'get_initial_items': 'echo "1"', 'switch_to_good': None, 'switch_to_bad': None, @@ -317,7 +318,8 @@ class MockBinarySearchState(BinarySearchState): 'file_args': None, 'verbose': None } - super(MockBinarySearchState, self).__init__(**kwargs) + default_kwargs.update(kwargs) + super(MockBinarySearchState, self).__init__(**default_kwargs) def _CanonicalizeScript(script_name): diff --git a/binary_search_tool/test/binary_search_tool_tester.py b/binary_search_tool/test/binary_search_tool_tester.py index 451543d1..0f7906b0 100755 --- a/binary_search_tool/test/binary_search_tool_tester.py +++ b/binary_search_tool/test/binary_search_tool_tester.py @@ -107,6 +107,22 @@ class BisectingUtilsTest(unittest.TestCase): self.assertTrue(os.readlink(state_file) != first_state) self.assertFalse(os.path.exists(first_state)) + def test_tmp_cleanup(self): + bss = binary_search_state.MockBinarySearchState( + get_initial_items='echo "0\n1\n2\n3"', switch_to_good='./switch_tmp.py', + file_args=True) + bss.SwitchToGood(['0', '1', '2', '3']) + + tmp_file = None + with open('tmp_file', 'r') as f: + tmp_file = f.read() + os.remove('tmp_file') + + self.assertFalse(os.path.exists(tmp_file)) + ws = common.ReadWorkingSet() + for i in range(3): + self.assertEquals(ws[i], 42) + def check_output(self): _, out, _ = command_executer.GetCommandExecuter().RunCommandWOutput( 'tail -n 10 logs/binary_search_tool_tester.py.out') @@ -132,6 +148,7 @@ def Main(argv): suite.addTest(BisectingUtilsTest('test_bad_install_script')) suite.addTest(BisectingUtilsTest('test_bad_save_state')) suite.addTest(BisectingUtilsTest('test_save_state')) + suite.addTest(BisectingUtilsTest('test_tmp_cleanup')) runner = unittest.TextTestRunner() runner.run(suite) diff --git a/binary_search_tool/test/switch_tmp.py b/binary_search_tool/test/switch_tmp.py new file mode 100755 index 00000000..165004ed --- /dev/null +++ b/binary_search_tool/test/switch_tmp.py @@ -0,0 +1,34 @@ +#!/usr/bin/python2 +"""Change portions of the object files to good. + +This file is a test switch script. Used only for the test test_tmp_cleanup. +The "portion" is defined by the file (which is passed as the only argument to +this script) content. Every line in the file is an object index, which will be +set to good (mark as 42). +""" + +from __future__ import print_function + +import sys + +import common + + +def Main(argv): + working_set = common.ReadWorkingSet() + object_index = common.ReadObjectIndex(argv[1]) + + # Random number so the results can be checked + for oi in object_index: + working_set[int(oi)] = 42 + + common.WriteWorkingSet(working_set) + with open('tmp_file', 'w') as f: + f.write(argv[1]) + + return 0 + + +if __name__ == '__main__': + retval = Main(sys.argv) + sys.exit(retval) |