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 json |
| 7 import logging |
| 8 import os |
| 9 import re |
| 10 import subprocess |
| 11 import sys |
| 12 import tempfile |
| 13 import unittest |
| 14 |
| 15 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 ROOT_DIR = os.path.dirname(BASE_DIR) |
| 17 sys.path.insert(0, ROOT_DIR) |
| 18 |
| 19 import trace_inputs |
| 20 |
| 21 FILE_PATH = os.path.realpath(unicode(os.path.abspath(__file__))) |
| 22 TARGET_UTIL_PATH = os.path.join(BASE_DIR, 'gtest_fake', 'gtest_fake_base.py') |
| 23 TARGET_PATH = os.path.join(BASE_DIR, 'gtest_fake', 'gtest_fake_fail.py') |
| 24 |
| 25 |
| 26 class TraceTestCases(unittest.TestCase): |
| 27 def setUp(self): |
| 28 self.temp_file = None |
| 29 |
| 30 self.initial_cwd = ROOT_DIR |
| 31 if sys.platform == 'win32': |
| 32 # Windows has no kernel mode concept of current working directory. |
| 33 self.initial_cwd = None |
| 34 |
| 35 # There's 2 kinds of references to python, self.executable, |
| 36 # self.real_executable. It depends how python was started and on which OS. |
| 37 self.executable = unicode(sys.executable) |
| 38 if sys.platform == 'darwin': |
| 39 # /usr/bin/python is a thunk executable that decides which version of |
| 40 # python gets executed. |
| 41 suffix = '.'.join(map(str, sys.version_info[0:2])) |
| 42 if os.access(self.executable + suffix, os.X_OK): |
| 43 # So it'll look like /usr/bin/python2.7 |
| 44 self.executable += suffix |
| 45 |
| 46 self.real_executable = trace_inputs.get_native_path_case(self.executable) |
| 47 # Make sure there's no environment variable that could do side effects. |
| 48 os.environ.pop('GTEST_SHARD_INDEX', '') |
| 49 os.environ.pop('GTEST_TOTAL_SHARDS', '') |
| 50 |
| 51 def tearDown(self): |
| 52 if self.temp_file: |
| 53 os.remove(self.temp_file) |
| 54 |
| 55 def test_simple(self): |
| 56 file_handle, self.temp_file = tempfile.mkstemp( |
| 57 prefix='trace_test_cases_test') |
| 58 os.close(file_handle) |
| 59 |
| 60 cmd = [ |
| 61 sys.executable, |
| 62 os.path.join(ROOT_DIR, 'trace_test_cases.py'), |
| 63 # Forces 4 parallel jobs. |
| 64 '--jobs', '4', |
| 65 '--timeout', '0', |
| 66 '--out', self.temp_file, |
| 67 ] |
| 68 if VERBOSE: |
| 69 cmd.extend(['-v'] * 3) |
| 70 cmd.append(TARGET_PATH) |
| 71 logging.debug(' '.join(cmd)) |
| 72 proc = subprocess.Popen( |
| 73 cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 74 out, err = proc.communicate() or ('', '') # pylint is confused. |
| 75 self.assertEquals(0, proc.returncode, (out, err)) |
| 76 lines = out.splitlines() |
| 77 expected_out_re = [ |
| 78 r'\[1/4\] \d\.\d\ds .+', |
| 79 r'\[2/4\] \d\.\d\ds .+', |
| 80 r'\[3/4\] \d\.\d\ds .+', |
| 81 r'\[4/4\] \d\.\d\ds .+', |
| 82 ] |
| 83 self.assertEquals(len(expected_out_re), len(lines), lines) |
| 84 for index in range(len(expected_out_re)): |
| 85 self.assertTrue( |
| 86 re.match('^%s$' % expected_out_re[index], lines[index]), |
| 87 '%d: %s\n%r\n%s' % ( |
| 88 index, expected_out_re[index], lines[index], out)) |
| 89 # Junk is printed on win32. |
| 90 if sys.platform != 'win32' and not VERBOSE: |
| 91 self.assertEquals('', err) |
| 92 |
| 93 with open(self.temp_file, 'r') as f: |
| 94 content = f.read() |
| 95 try: |
| 96 result = json.loads(content) |
| 97 except: |
| 98 print repr(content) |
| 99 raise |
| 100 |
| 101 test_cases = ( |
| 102 'Baz.Fail', |
| 103 'Foo.Bar1', |
| 104 'Foo.Bar2', |
| 105 'Foo.Bar3', |
| 106 ) |
| 107 self.assertEquals(dict, result.__class__) |
| 108 self.assertEquals(['traces'], result.keys()) |
| 109 for index, trace in enumerate( |
| 110 sorted(result['traces'], key=lambda x: x['trace'])): |
| 111 self.assertEquals(test_cases[index], trace['trace']) |
| 112 self.assertEquals( |
| 113 [u'cmd', u'cwd', u'output', u'pid', u'trace'], sorted(trace)) |
| 114 self.assertEquals( |
| 115 [sys.executable, TARGET_PATH, '--gtest_filter=%s' % trace['trace']], |
| 116 trace['cmd']) |
| 117 self.assertEquals(int, trace['pid'].__class__) |
| 118 |
| 119 |
| 120 if __name__ == '__main__': |
| 121 VERBOSE = '-v' in sys.argv |
| 122 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR) |
| 123 unittest.main() |
OLD | NEW |