OLD | NEW |
1 # Copyright 2016 the V8 project authors. All rights reserved. | 1 # Copyright 2016 the V8 project authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # Requires python-coverage. Native python coverage version >= 3.7.1 should | 5 # Requires python-coverage. Native python coverage version >= 3.7.1 should |
6 # be installed to get the best speed. | 6 # be installed to get the best speed. |
7 | 7 |
8 import copy | 8 import copy |
9 import coverage | 9 import coverage |
10 import logging | 10 import logging |
| 11 import json |
11 import os | 12 import os |
| 13 import shutil |
12 import sys | 14 import sys |
| 15 import tempfile |
13 import unittest | 16 import unittest |
14 | 17 |
15 | 18 |
16 # Directory of this file. | 19 # Directory of this file. |
17 LOCATION = os.path.dirname(os.path.abspath(__file__)) | 20 LOCATION = os.path.dirname(os.path.abspath(__file__)) |
18 | 21 |
19 # V8 checkout directory. | 22 # V8 checkout directory. |
20 BASE_DIR = os.path.dirname(os.path.dirname(LOCATION)) | 23 BASE_DIR = os.path.dirname(os.path.dirname(LOCATION)) |
21 | 24 |
22 # Executable location. | 25 # Executable location. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 'version': 1, | 124 'version': 1, |
122 'tests': ['cctest', 'd8', 'unittests'], | 125 'tests': ['cctest', 'd8', 'unittests'], |
123 'files': { | 126 'files': { |
124 'src/baz/bar.h': [[0, 0b0], [3, 0b11], [7, 0b11], [8, 0b0]], | 127 'src/baz/bar.h': [[0, 0b0], [3, 0b11], [7, 0b11], [8, 0b0]], |
125 'src/baz.cc': [[1, 0b0], [2, 0b101]], | 128 'src/baz.cc': [[1, 0b0], [2, 0b101]], |
126 'src/foo.cc': [[1, 0b1], [11, 0b10], [92, 0b100], [93, 0b0]], | 129 'src/foo.cc': [[1, 0b1], [11, 0b10], [92, 0b100], [93, 0b0]], |
127 }, | 130 }, |
128 } | 131 } |
129 | 132 |
130 | 133 |
| 134 #------------------------------------------------------------------------------ |
| 135 |
| 136 # Data for test_split. |
| 137 |
| 138 EXPECTED_SPLIT_FILES = [ |
| 139 ( |
| 140 os.path.join('src', 'baz', 'bar.h.json'), |
| 141 { |
| 142 'version': 1, |
| 143 'tests': ['cctest', 'd8', 'unittests'], |
| 144 'files': { |
| 145 'src/baz/bar.h': [[0, 0b0], [3, 0b11], [7, 0b11], [8, 0b0]], |
| 146 }, |
| 147 }, |
| 148 ), |
| 149 ( |
| 150 os.path.join('src', 'baz.cc.json'), |
| 151 { |
| 152 'version': 1, |
| 153 'tests': ['cctest', 'd8', 'unittests'], |
| 154 'files': { |
| 155 'src/baz.cc': [[1, 0b0], [2, 0b101]], |
| 156 }, |
| 157 }, |
| 158 ), |
| 159 ( |
| 160 os.path.join('src', 'foo.cc.json'), |
| 161 { |
| 162 'version': 1, |
| 163 'tests': ['cctest', 'd8', 'unittests'], |
| 164 'files': { |
| 165 'src/foo.cc': [[1, 0b1], [11, 0b10], [92, 0b100], [93, 0b0]], |
| 166 }, |
| 167 }, |
| 168 ), |
| 169 ] |
| 170 |
| 171 |
131 class FormatterTests(unittest.TestCase): | 172 class FormatterTests(unittest.TestCase): |
132 @classmethod | 173 @classmethod |
133 def setUpClass(cls): | 174 def setUpClass(cls): |
134 sys.path.append(LOCATION) | 175 sys.path.append(LOCATION) |
135 cls._cov = coverage.coverage( | 176 cls._cov = coverage.coverage( |
136 include=([os.path.join(LOCATION, 'sancov_formatter.py')])) | 177 include=([os.path.join(LOCATION, 'sancov_formatter.py')])) |
137 cls._cov.start() | 178 cls._cov.start() |
138 import sancov_formatter | 179 import sancov_formatter |
139 global sancov_formatter | 180 global sancov_formatter |
140 | 181 |
141 @classmethod | 182 @classmethod |
142 def tearDownClass(cls): | 183 def tearDownClass(cls): |
143 cls._cov.stop() | 184 cls._cov.stop() |
144 cls._cov.report() | 185 cls._cov.report() |
145 | 186 |
146 def test_process_symbolizer_output(self): | 187 def test_process_symbolizer_output(self): |
147 result = sancov_formatter.process_symbolizer_output(SYMBOLIZER_OUTPUT) | 188 result = sancov_formatter.process_symbolizer_output(SYMBOLIZER_OUTPUT) |
148 self.assertEquals(EXPECTED_PROCESSED_OUTPUT, result) | 189 self.assertEquals(EXPECTED_PROCESSED_OUTPUT, result) |
149 | 190 |
150 def test_merge_instrumented_line_results(self): | 191 def test_merge_instrumented_line_results(self): |
151 result = sancov_formatter.merge_instrumented_line_results( | 192 result = sancov_formatter.merge_instrumented_line_results( |
152 EXE_LIST, INSTRUMENTED_LINE_RESULTS) | 193 EXE_LIST, INSTRUMENTED_LINE_RESULTS) |
153 self.assertEquals(EXPECTED_INSTRUMENTED_LINES_DATA, result) | 194 self.assertEquals(EXPECTED_INSTRUMENTED_LINES_DATA, result) |
154 | 195 |
155 def test_merge_covered_line_results(self): | 196 def test_merge_covered_line_results(self): |
156 data = copy.deepcopy(EXPECTED_INSTRUMENTED_LINES_DATA) | 197 data = copy.deepcopy(EXPECTED_INSTRUMENTED_LINES_DATA) |
157 sancov_formatter.merge_covered_line_results( | 198 sancov_formatter.merge_covered_line_results( |
158 data, COVERED_LINE_RESULTS) | 199 data, COVERED_LINE_RESULTS) |
159 self.assertEquals(EXPECTED_COVERED_LINES_DATA, data) | 200 self.assertEquals(EXPECTED_COVERED_LINES_DATA, data) |
| 201 |
| 202 def test_split(self): |
| 203 _, json_input = tempfile.mkstemp(prefix='tmp_coverage_test_split') |
| 204 with open(json_input, 'w') as f: |
| 205 json.dump(EXPECTED_COVERED_LINES_DATA, f) |
| 206 output_dir = tempfile.mkdtemp(prefix='tmp_coverage_test_split') |
| 207 |
| 208 try: |
| 209 sancov_formatter.main([ |
| 210 'split', |
| 211 '--json-input', json_input, |
| 212 '--output-dir', output_dir, |
| 213 ]) |
| 214 |
| 215 for file_name, expected_data in EXPECTED_SPLIT_FILES: |
| 216 full_path = os.path.join(output_dir, file_name) |
| 217 self.assertTrue(os.path.exists(full_path)) |
| 218 with open(full_path) as f: |
| 219 self.assertEquals(expected_data, json.load(f)) |
| 220 finally: |
| 221 os.remove(json_input) |
| 222 shutil.rmtree(output_dir) |
OLD | NEW |