| 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 logging | 6 import logging |
| 7 import os | 7 import os |
| 8 import shutil | 8 import shutil |
| 9 import subprocess | 9 import subprocess |
| 10 import sys | 10 import sys |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 class TraceInputs(unittest.TestCase): | 26 class TraceInputs(unittest.TestCase): |
| 27 def setUp(self): | 27 def setUp(self): |
| 28 self.tempdir = tempfile.mkdtemp() | 28 self.tempdir = tempfile.mkdtemp() |
| 29 self.log = os.path.join(self.tempdir, 'log') | 29 self.log = os.path.join(self.tempdir, 'log') |
| 30 | 30 |
| 31 def tearDown(self): | 31 def tearDown(self): |
| 32 shutil.rmtree(self.tempdir) | 32 shutil.rmtree(self.tempdir) |
| 33 | 33 |
| 34 def _execute(self, args): | 34 def _execute(self, args): |
| 35 cmd = [ | 35 cmd = [ |
| 36 sys.executable, os.path.join(ROOT_DIR, 'trace_inputs.py'), | 36 sys.executable, os.path.join(ROOT_DIR, 'trace_inputs.py'), |
| 37 '--log', self.log, | 37 '--log', self.log, |
| 38 '--gyp', os.path.join('data', 'trace_inputs'), | 38 '--root-dir', ROOT_DIR, |
| 39 '--product', '.', # Not tested. | |
| 40 '--root-dir', ROOT_DIR, | |
| 41 ] + args | 39 ] + args |
| 42 p = subprocess.Popen( | 40 p = subprocess.Popen( |
| 43 cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=ROOT_DIR) | 41 cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=ROOT_DIR) |
| 44 out = p.communicate()[0] | 42 out = p.communicate()[0] |
| 45 if p.returncode: | 43 if p.returncode: |
| 46 raise CalledProcessError(p.returncode, cmd, out) | 44 raise CalledProcessError(p.returncode, cmd, out) |
| 47 return out | 45 return out |
| 48 | 46 |
| 47 @staticmethod |
| 48 def _gyp(): |
| 49 return [ |
| 50 '--gyp', os.path.join('data', 'trace_inputs'), |
| 51 '--product', '.', # Not tested. |
| 52 ] |
| 53 |
| 49 def test_trace(self): | 54 def test_trace(self): |
| 50 if sys.platform == 'linux2': | 55 if sys.platform == 'linux2': |
| 51 return self._test_trace_linux() | 56 return self._test_trace_linux() |
| 52 if sys.platform == 'darwin': | 57 if sys.platform == 'darwin': |
| 53 return self._test_trace_mac() | 58 return self._test_trace_mac() |
| 54 print 'Unsupported: %s' % sys.platform | 59 print 'Unsupported: %s' % sys.platform |
| 55 | 60 |
| 61 def test_trace_gyp(self): |
| 62 if sys.platform == 'linux2': |
| 63 return self._test_trace_gyp_linux() |
| 64 if sys.platform == 'darwin': |
| 65 return self._test_trace_gyp_mac() |
| 66 print 'Unsupported: %s' % sys.platform |
| 67 |
| 56 def _test_trace_linux(self): | 68 def _test_trace_linux(self): |
| 57 # TODO(maruel): BUG: Note that child.py is missing. | 69 expected = ( |
| 70 "Total: 4\n" |
| 71 "Non existent: 0\n" |
| 72 "Interesting: 4 reduced to 3\n" |
| 73 " data/trace_inputs/\n" |
| 74 " trace_inputs.py\n" |
| 75 " trace_inputs_test.py\n") |
| 76 actual = self._execute( |
| 77 ['trace_inputs_test.py', '--child1']).splitlines(True) |
| 78 self.assertTrue(actual[0].startswith('Tracing... [')) |
| 79 self.assertTrue(actual[1].startswith('Loading traces... ')) |
| 80 self.assertEquals(expected, ''.join(actual[2:])) |
| 81 |
| 82 def _test_trace_gyp_linux(self): |
| 58 expected = ( | 83 expected = ( |
| 59 "{\n" | 84 "{\n" |
| 60 " 'variables': {\n" | 85 " 'variables': {\n" |
| 61 " 'isolate_files': [\n" | 86 " 'isolate_files': [\n" |
| 62 " '<(DEPTH)/trace_inputs.py',\n" | 87 " '<(DEPTH)/trace_inputs.py',\n" |
| 63 " '<(DEPTH)/trace_inputs_test.py',\n" | 88 " '<(DEPTH)/trace_inputs_test.py',\n" |
| 64 " ],\n" | 89 " ],\n" |
| 65 " 'isolate_dirs': [\n" | 90 " 'isolate_dirs': [\n" |
| 91 " './',\n" |
| 66 " ],\n" | 92 " ],\n" |
| 67 " },\n" | 93 " },\n" |
| 68 "},\n") | 94 "},\n") |
| 69 gyp = self._execute(['trace_inputs_test.py', '--child1']) | 95 actual = self._execute(self._gyp() + ['trace_inputs_test.py', '--child1']) |
| 70 self.assertEquals(expected, gyp) | 96 self.assertEquals(expected, actual) |
| 71 | 97 |
| 72 def _test_trace_mac(self): | 98 def _test_trace_mac(self): |
| 73 # It is annoying in the case of dtrace because it requires root access. | 99 # It is annoying in the case of dtrace because it requires root access. |
| 74 # TODO(maruel): BUG: Note that child.py is missing. | 100 # TODO(maruel): BUG: Note that child.py is missing. |
| 75 expected = ( | 101 expected = ( |
| 102 "Total: 2\n" |
| 103 "Non existent: 0\n" |
| 104 "Interesting: 2 reduced to 2\n" |
| 105 " trace_inputs.py\n" |
| 106 " trace_inputs_test.py\n") |
| 107 actual = self._execute( |
| 108 ['trace_inputs_test.py', '--child1']).splitlines(True) |
| 109 self.assertTrue(actual[0].startswith('Tracing... [')) |
| 110 self.assertTrue(actual[1].startswith('Loading traces... ')) |
| 111 self.assertEquals(expected, ''.join(actual[2:])) |
| 112 |
| 113 def _test_trace_gyp_mac(self): |
| 114 # It is annoying in the case of dtrace because it requires root access. |
| 115 # TODO(maruel): BUG: Note that child.py is missing. |
| 116 expected = ( |
| 76 "{\n" | 117 "{\n" |
| 77 " 'variables': {\n" | 118 " 'variables': {\n" |
| 78 " 'isolate_files': [\n" | 119 " 'isolate_files': [\n" |
| 79 " '<(DEPTH)/trace_inputs.py',\n" | 120 " '<(DEPTH)/trace_inputs.py',\n" |
| 80 " '<(DEPTH)/trace_inputs_test.py',\n" | 121 " '<(DEPTH)/trace_inputs_test.py',\n" |
| 81 " ],\n" | 122 " ],\n" |
| 82 " 'isolate_dirs': [\n" | 123 " 'isolate_dirs': [\n" |
| 83 " ],\n" | 124 " ],\n" |
| 84 " },\n" | 125 " },\n" |
| 85 "},\n") | 126 "},\n") |
| 86 gyp = self._execute(['trace_inputs_test.py', '--child1']) | 127 actual = self._execute(self._gyp() + ['trace_inputs_test.py', '--child1']) |
| 87 self.assertEquals(expected, gyp) | 128 self.assertEquals(expected, actual) |
| 88 | 129 |
| 89 | 130 |
| 90 def child1(): | 131 def child1(): |
| 91 print 'child1' | 132 print 'child1' |
| 92 # Implicitly force file opening. | 133 # Implicitly force file opening. |
| 93 import trace_inputs # pylint: disable=W0612 | 134 import trace_inputs # pylint: disable=W0612 |
| 94 # Do not wait for the child to exit. | 135 # Do not wait for the child to exit. |
| 95 # Use relative directory. | 136 # Use relative directory. |
| 96 subprocess.Popen( | 137 subprocess.Popen( |
| 97 ['python', 'child2.py'], cwd=os.path.join('data', 'trace_inputs')) | 138 ['python', 'child2.py'], cwd=os.path.join('data', 'trace_inputs')) |
| 98 return 0 | 139 return 0 |
| 99 | 140 |
| 100 | 141 |
| 101 def main(): | 142 def main(): |
| 102 global VERBOSE | 143 global VERBOSE |
| 103 VERBOSE = '-v' in sys.argv | 144 VERBOSE = '-v' in sys.argv |
| 104 level = logging.DEBUG if VERBOSE else logging.ERROR | 145 level = logging.DEBUG if VERBOSE else logging.ERROR |
| 105 logging.basicConfig(level=level) | 146 logging.basicConfig(level=level) |
| 106 if len(sys.argv) == 1: | 147 if len(sys.argv) == 1: |
| 107 unittest.main() | 148 unittest.main() |
| 108 | 149 |
| 109 if sys.argv[1] == '--child1': | 150 if sys.argv[1] == '--child1': |
| 110 return child1() | 151 return child1() |
| 111 | 152 |
| 112 unittest.main() | 153 unittest.main() |
| 113 | 154 |
| 114 | 155 |
| 115 if __name__ == '__main__': | 156 if __name__ == '__main__': |
| 116 sys.exit(main()) | 157 sys.exit(main()) |
| OLD | NEW |