| OLD | NEW |
| (Empty) |
| 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. | |
| 5 | |
| 6 import hashlib | |
| 7 import json | |
| 8 import logging | |
| 9 import os | |
| 10 import shutil | |
| 11 import subprocess | |
| 12 import sys | |
| 13 import tempfile | |
| 14 import unittest | |
| 15 | |
| 16 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| 17 ROOT_DIR = os.path.dirname(BASE_DIR) | |
| 18 sys.path.insert(0, ROOT_DIR) | |
| 19 | |
| 20 import run_isolated | |
| 21 | |
| 22 | |
| 23 class FixTestCases(unittest.TestCase): | |
| 24 def setUp(self): | |
| 25 self.tempdir = tempfile.mkdtemp(prefix='fix_test_case') | |
| 26 self.srcdir = os.path.join(self.tempdir, 'srcdir') | |
| 27 os.mkdir(self.srcdir) | |
| 28 | |
| 29 def tearDown(self): | |
| 30 if self.tempdir: | |
| 31 if VERBOSE: | |
| 32 # If -v is used, this means the user wants to do further analisys on | |
| 33 # the data. | |
| 34 print('Leaking %s' % self.tempdir) | |
| 35 else: | |
| 36 shutil.rmtree(self.tempdir) | |
| 37 | |
| 38 def _run(self, cmd): | |
| 39 if VERBOSE: | |
| 40 cmd = cmd + ['--verbose'] * 3 | |
| 41 logging.info(cmd) | |
| 42 proc = subprocess.Popen( | |
| 43 [sys.executable, os.path.join(ROOT_DIR, cmd[0])] + cmd[1:], | |
| 44 cwd=self.srcdir, | |
| 45 stdout=subprocess.PIPE, | |
| 46 stderr=subprocess.STDOUT) | |
| 47 out = proc.communicate()[0] | |
| 48 if VERBOSE: | |
| 49 print '\n-----' | |
| 50 print out.strip() | |
| 51 print '-----\n' | |
| 52 self.assertEqual(0, proc.returncode) | |
| 53 return out | |
| 54 | |
| 55 def test_simple(self): | |
| 56 # Create a directory with nothing in it and progressively add more stuff. | |
| 57 isolate = os.path.join(self.srcdir, 'gtest_fake_pass.isolate') | |
| 58 chromeos_value = int(run_isolated.get_flavor() == 'linux') | |
| 59 condition = 'OS=="%s" and chromeos==%d' % (run_isolated.get_flavor(), | |
| 60 chromeos_value) | |
| 61 with open(isolate, 'w') as f: | |
| 62 # Write a minimal .isolate file. | |
| 63 f.write(str({ | |
| 64 'conditions': [ | |
| 65 [condition, { | |
| 66 'variables': { | |
| 67 'command': [ | |
| 68 'run_test_cases.py', 'gtest_fake_pass.py', | |
| 69 ], | |
| 70 }, | |
| 71 }], | |
| 72 ], | |
| 73 })) | |
| 74 def _copy(filename): | |
| 75 shutil.copy( | |
| 76 os.path.join(BASE_DIR, 'gtest_fake', filename), | |
| 77 os.path.join(self.srcdir, filename)) | |
| 78 _copy('gtest_fake_base.py') | |
| 79 _copy('gtest_fake_pass.py') | |
| 80 shutil.copy( | |
| 81 os.path.join(ROOT_DIR, 'run_test_cases.py'), | |
| 82 os.path.join(self.srcdir, 'run_test_cases.py')) | |
| 83 shutil.copy( | |
| 84 os.path.join(ROOT_DIR, 'run_isolated.py'), | |
| 85 os.path.join(self.srcdir, 'run_isolated.py')) | |
| 86 | |
| 87 logging.debug('1. Create a .isolated file out of the .isolate file.') | |
| 88 isolated = os.path.join(self.srcdir, 'gtest_fake_pass.isolated') | |
| 89 out = self._run(['isolate.py', 'check', '-i', isolate, '-s', isolated, | |
| 90 '-V', 'chromeos', str(chromeos_value)]) | |
| 91 if not VERBOSE: | |
| 92 self.assertEqual('', out) | |
| 93 | |
| 94 logging.debug('2. Run fix_test_cases.py on it.') | |
| 95 # Give up on looking at stdout. | |
| 96 _ = self._run(['fix_test_cases.py', '-s', isolated]) | |
| 97 | |
| 98 logging.debug('3. Asserting the content of the .isolated file.') | |
| 99 with open(isolated) as f: | |
| 100 actual_isolated = json.load(f) | |
| 101 gtest_fake_base_py = os.path.join(self.srcdir, 'gtest_fake_base.py') | |
| 102 gtest_fake_pass_py = os.path.join(self.srcdir, 'gtest_fake_pass.py') | |
| 103 run_isolated_py = os.path.join(self.srcdir, 'run_isolated.py') | |
| 104 run_test_cases_py = os.path.join(self.srcdir, 'run_test_cases.py') | |
| 105 expected_isolated = { | |
| 106 u'command': [u'run_test_cases.py', u'gtest_fake_pass.py'], | |
| 107 u'files': { | |
| 108 u'gtest_fake_base.py': { | |
| 109 u'm': 416, | |
| 110 u'h': unicode(hashlib.sha1( | |
| 111 open(gtest_fake_base_py, 'rb').read()).hexdigest()), | |
| 112 u's': os.stat(gtest_fake_base_py).st_size, | |
| 113 }, | |
| 114 u'gtest_fake_pass.py': { | |
| 115 u'm': 488, | |
| 116 u'h': unicode(hashlib.sha1( | |
| 117 open(gtest_fake_pass_py, 'rb').read()).hexdigest()), | |
| 118 u's': os.stat(gtest_fake_pass_py).st_size, | |
| 119 }, | |
| 120 u'run_isolated.py': { | |
| 121 u'm': 488, | |
| 122 u'h': unicode(hashlib.sha1( | |
| 123 open(run_isolated_py, 'rb').read()).hexdigest()), | |
| 124 u's': os.stat(run_isolated_py).st_size, | |
| 125 }, | |
| 126 u'run_test_cases.py': { | |
| 127 u'm': 488, | |
| 128 u'h': unicode(hashlib.sha1( | |
| 129 open(run_test_cases_py, 'rb').read()).hexdigest()), | |
| 130 u's': os.stat(run_test_cases_py).st_size, | |
| 131 }, | |
| 132 }, | |
| 133 u'os': unicode(run_isolated.get_flavor()), | |
| 134 u'relative_cwd': u'.', | |
| 135 } | |
| 136 if sys.platform == 'win32': | |
| 137 for value in expected_isolated['files'].itervalues(): | |
| 138 self.assertTrue(value.pop('m')) | |
| 139 self.assertEqual(expected_isolated, actual_isolated) | |
| 140 | |
| 141 # Now verify the .isolate file was updated! (That's the magical part where | |
| 142 # you say wow!) | |
| 143 with open(isolate) as f: | |
| 144 actual = eval(f.read(), {'__builtins__': None}, None) | |
| 145 expected = { | |
| 146 'conditions': [ | |
| 147 [condition, { | |
| 148 'variables': { | |
| 149 'command': [ | |
| 150 'run_test_cases.py', 'gtest_fake_pass.py' | |
| 151 ], | |
| 152 'isolate_dependency_tracked': [ | |
| 153 'gtest_fake_base.py', | |
| 154 'gtest_fake_pass.py', | |
| 155 'run_isolated.py', | |
| 156 'run_test_cases.py', | |
| 157 ], | |
| 158 }, | |
| 159 }], | |
| 160 ], | |
| 161 } | |
| 162 self.assertEqual(expected, actual) | |
| 163 | |
| 164 | |
| 165 if __name__ == '__main__': | |
| 166 VERBOSE = '-v' in sys.argv | |
| 167 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) | |
| 168 unittest.main() | |
| OLD | NEW |