| 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 logging | 6 import logging |
| 8 import os | 7 import os |
| 9 import sys | 8 import sys |
| 10 import tempfile | |
| 11 import unittest | 9 import unittest |
| 12 | 10 |
| 13 import isolate | 11 import isolate |
| 14 | 12 |
| 15 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | 13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 | 14 |
| 17 | 15 |
| 18 class Isolate(unittest.TestCase): | 16 class Isolate(unittest.TestCase): |
| 19 def setUp(self): | 17 def setUp(self): |
| 20 # Everything should work even from another directory. | 18 # Everything should work even from another directory. |
| 21 os.chdir(os.path.dirname(ROOT_DIR)) | 19 os.chdir(os.path.dirname(ROOT_DIR)) |
| 22 | 20 |
| 23 def _run_process_options(self, values, variables, more_expected_data): | 21 def test_load_isolate_empty(self): |
| 24 """Runs isolate.process_options() and verify the results.""" | |
| 25 fd, temp_path = tempfile.mkstemp() | |
| 26 try: | |
| 27 # Reuse the file descriptor. On windows, it needs to be closed before | |
| 28 # process_options() opens it, because mkstemp() opens it without file | |
| 29 # sharing enabled. | |
| 30 with os.fdopen(fd, 'w') as f: | |
| 31 json.dump(values, f) | |
| 32 root_dir, infiles, data = isolate.process_options( | |
| 33 variables, | |
| 34 temp_path, | |
| 35 os.path.join('isolate', 'data', 'isolate', 'touch_root.isolate'), | |
| 36 self.fail) | |
| 37 finally: | |
| 38 os.remove(temp_path) | |
| 39 | |
| 40 expected_data = { | |
| 41 u'command': ['python', 'touch_root.py'], | |
| 42 u'read_only': None, | |
| 43 u'relative_cwd': 'data/isolate', | |
| 44 u'resultfile': temp_path, | |
| 45 u'resultdir': tempfile.gettempdir(), | |
| 46 u'variables': {}, | |
| 47 } | |
| 48 expected_data.update(more_expected_data) | |
| 49 expected_files = sorted( | |
| 50 ('isolate.py', os.path.join('data', 'isolate', 'touch_root.py'))) | |
| 51 self.assertEquals(ROOT_DIR, root_dir) | |
| 52 self.assertEquals(expected_files, sorted(infiles)) | |
| 53 self.assertEquals(expected_data, data) | |
| 54 | |
| 55 def test_load_empty(self): | |
| 56 content = "{}" | 22 content = "{}" |
| 57 command, infiles, read_only = isolate.load_isolate( | 23 command, infiles, read_only = isolate.load_isolate( |
| 58 content, self.fail) | 24 content, self.fail) |
| 59 self.assertEquals([], command) | 25 self.assertEquals([], command) |
| 60 self.assertEquals([], infiles) | 26 self.assertEquals([], infiles) |
| 61 self.assertEquals(None, read_only) | 27 self.assertEquals(None, read_only) |
| 62 | 28 |
| 63 def test_process_options_empty(self): | 29 def test_result_load_empty(self): |
| 64 # Passing nothing generates nothing unexpected. | 30 values = { |
| 65 self._run_process_options({}, {}, {}) | 31 } |
| 32 expected = { |
| 33 'command': [], |
| 34 'files': {}, |
| 35 'read_only': None, |
| 36 'relative_cwd': None, |
| 37 } |
| 38 self.assertEquals(expected, isolate.Result.load(values).flatten()) |
| 66 | 39 |
| 67 def test_process_options(self): | 40 def test_result_load(self): |
| 68 # The previous unexpected variables are kept, the 'variables' dictionary is | |
| 69 # updated. | |
| 70 values = { | 41 values = { |
| 71 'command': 'maybe', | 42 'command': 'maybe', |
| 72 'foo': 'bar', | 43 'files': {'foo': 42}, |
| 73 'read_only': 2, | 44 'read_only': 2, |
| 74 'relative_cwd': None, | 45 'relative_cwd': None, |
| 75 'resultdir': '2', | |
| 76 'resultfile': [], | |
| 77 'variables': { | |
| 78 'unexpected': 'seriously', | |
| 79 # This value is updated. | |
| 80 'expected': 'stale', | |
| 81 }, | |
| 82 } | 46 } |
| 47 expected = { |
| 48 'command': 'maybe', |
| 49 'files': {'foo': 42}, |
| 50 'read_only': 2, |
| 51 'relative_cwd': None, |
| 52 } |
| 53 self.assertEquals(expected, isolate.Result.load(values).flatten()) |
| 83 | 54 |
| 84 expected_data = { | 55 def test_result_load_unexpected(self): |
| 85 u'foo': u'bar', | 56 values = { |
| 86 u'variables': { | 57 'foo': 'bar', |
| 87 'expected': 'very', | |
| 88 u'unexpected': u'seriously', | |
| 89 }, | |
| 90 } | 58 } |
| 91 self._run_process_options(values, {'expected': 'very'}, expected_data) | 59 try: |
| 60 isolate.Result.load(values) |
| 61 self.fail() |
| 62 except AssertionError: |
| 63 pass |
| 64 |
| 65 def test_savedstate_load_empty(self): |
| 66 values = { |
| 67 } |
| 68 expected = { |
| 69 'isolate_file': None, |
| 70 'variables': {}, |
| 71 } |
| 72 self.assertEquals(expected, isolate.SavedState.load(values).flatten()) |
| 73 |
| 74 def test_savedstate_load(self): |
| 75 values = { |
| 76 'isolate_file': 'maybe', |
| 77 'variables': {'foo': 42}, |
| 78 } |
| 79 expected = { |
| 80 'isolate_file': 'maybe', |
| 81 'variables': {'foo': 42}, |
| 82 } |
| 83 self.assertEquals(expected, isolate.SavedState.load(values).flatten()) |
| 92 | 84 |
| 93 | 85 |
| 94 if __name__ == '__main__': | 86 if __name__ == '__main__': |
| 95 logging.basicConfig( | 87 logging.basicConfig( |
| 96 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR, | 88 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR, |
| 97 format='%(levelname)5s %(filename)15s(%(lineno)3d): %(message)s') | 89 format='%(levelname)5s %(filename)15s(%(lineno)3d): %(message)s') |
| 98 unittest.main() | 90 unittest.main() |
| OLD | NEW |