OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 json |
| 7 import os |
| 8 import tempfile |
6 import unittest | 9 import unittest |
7 | 10 |
8 import isolate | 11 import isolate |
9 | 12 |
| 13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 14 |
10 | 15 |
11 class Isolate(unittest.TestCase): | 16 class Isolate(unittest.TestCase): |
| 17 def setUp(self): |
| 18 # Everything should work even from another directory. |
| 19 os.chdir(os.path.dirname(ROOT_DIR)) |
| 20 |
| 21 def _run_process_options(self, values, variables, more_expected_data): |
| 22 """Runs isolate.process_options() and verify the results.""" |
| 23 fd, temp_path = tempfile.mkstemp() |
| 24 try: |
| 25 # Reuse the file descriptor. On windows, it needs to be closed before |
| 26 # process_options() opens it, because mkstemp() opens it without file |
| 27 # sharing enabled. |
| 28 with os.fdopen(fd, 'w') as f: |
| 29 json.dump(values, f) |
| 30 root_dir, infiles, data = isolate.process_options( |
| 31 variables, |
| 32 temp_path, |
| 33 os.path.join('isolate', 'data', 'isolate', 'touch_root.isolate'), |
| 34 self.fail) |
| 35 finally: |
| 36 os.remove(temp_path) |
| 37 |
| 38 expected_data = { |
| 39 u'command': ['python', 'touch_root.py'], |
| 40 u'read_only': None, |
| 41 u'relative_cwd': 'data/isolate', |
| 42 u'resultfile': temp_path, |
| 43 u'resultdir': tempfile.gettempdir(), |
| 44 u'variables': {}, |
| 45 } |
| 46 expected_data.update(more_expected_data) |
| 47 expected_files = sorted( |
| 48 ('isolate.py', os.path.join('data', 'isolate', 'touch_root.py'))) |
| 49 self.assertEquals(ROOT_DIR, root_dir) |
| 50 self.assertEquals(expected_files, sorted(infiles)) |
| 51 self.assertEquals(expected_data, data) |
| 52 |
12 def test_load_empty(self): | 53 def test_load_empty(self): |
13 content = "{}" | 54 content = "{}" |
14 variables = {} | 55 variables = {} |
15 command, infiles, read_only = isolate.load_isolate( | 56 command, infiles, read_only = isolate.load_isolate( |
16 content, variables, self.fail) | 57 content, variables, self.fail) |
17 self.assertEquals([], command) | 58 self.assertEquals([], command) |
18 self.assertEquals([], infiles) | 59 self.assertEquals([], infiles) |
19 self.assertEquals(None, read_only) | 60 self.assertEquals(None, read_only) |
20 | 61 |
| 62 def test_process_options_empty(self): |
| 63 # Passing nothing generates nothing unexpected. |
| 64 self._run_process_options({}, {}, {}) |
| 65 |
| 66 def test_process_options(self): |
| 67 # The previous unexpected variables are kept, the 'variables' dictionary is |
| 68 # updated. |
| 69 values = { |
| 70 'command': 'maybe', |
| 71 'foo': 'bar', |
| 72 'read_only': 2, |
| 73 'relative_cwd': None, |
| 74 'resultdir': '2', |
| 75 'resultfile': [], |
| 76 'variables': { |
| 77 'unexpected': 'seriously', |
| 78 }, |
| 79 } |
| 80 |
| 81 expected_data = { |
| 82 u'foo': u'bar', |
| 83 u'variables': { |
| 84 'expected': 'very', |
| 85 u'unexpected': u'seriously', |
| 86 }, |
| 87 } |
| 88 self._run_process_options(values, {'expected': 'very'}, expected_data) |
| 89 |
21 | 90 |
22 if __name__ == '__main__': | 91 if __name__ == '__main__': |
23 unittest.main() | 92 unittest.main() |
OLD | NEW |