| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium 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 import json | 5 import json |
| 6 import os | 6 import os |
| 7 import zipfile | 7 import tempfile |
| 8 | 8 |
| 9 from devil.utils import cmd_helper | 9 from devil.utils import cmd_helper |
| 10 from devil.utils import reraiser_thread | |
| 11 from pylib import constants | 10 from pylib import constants |
| 12 from pylib.base import base_test_result | 11 from pylib.base import base_test_result |
| 13 from pylib.base import test_run | 12 from pylib.base import test_run |
| 14 from pylib.results import json_results | 13 from pylib.results import json_results |
| 15 from py_utils import tempfile_ext | |
| 16 | 14 |
| 17 | 15 |
| 18 class LocalMachineJunitTestRun(test_run.TestRun): | 16 class LocalMachineJunitTestRun(test_run.TestRun): |
| 19 def __init__(self, env, test_instance): | 17 def __init__(self, env, test_instance): |
| 20 super(LocalMachineJunitTestRun, self).__init__(env, test_instance) | 18 super(LocalMachineJunitTestRun, self).__init__(env, test_instance) |
| 21 | 19 |
| 22 #override | 20 #override |
| 23 def TestPackage(self): | 21 def TestPackage(self): |
| 24 return self._test_instance.suite | 22 return self._test_instance.suite |
| 25 | 23 |
| 26 #override | 24 #override |
| 27 def SetUp(self): | 25 def SetUp(self): |
| 28 pass | 26 pass |
| 29 | 27 |
| 30 #override | 28 #override |
| 31 def RunTests(self): | 29 def RunTests(self): |
| 32 with tempfile_ext.NamedTemporaryDirectory() as temp_dir: | 30 with tempfile.NamedTemporaryFile() as json_file: |
| 33 json_file_path = os.path.join(temp_dir, 'results.json') | 31 java_script = os.path.join(constants.GetOutDirectory(), 'bin', 'helper', |
| 34 | 32 self._test_instance.suite) |
| 35 # Extract resources needed for test. | |
| 36 # TODO(mikecase): Investigate saving md5sums of zipfiles, and only | |
| 37 # extract zipfiles when they change. | |
| 38 def extract_resource_zip(resource_zip): | |
| 39 def helper(): | |
| 40 extract_dest = os.path.join( | |
| 41 temp_dir, os.path.splitext(os.path.basename(resource_zip))[0]) | |
| 42 with zipfile.ZipFile(resource_zip, 'r') as zf: | |
| 43 zf.extractall(extract_dest) | |
| 44 return extract_dest | |
| 45 return helper | |
| 46 | |
| 47 resource_dirs = reraiser_thread.RunAsync( | |
| 48 [extract_resource_zip(resource_zip) | |
| 49 for resource_zip in self._test_instance.resource_zips | |
| 50 if os.path.exists(resource_zip)]) | |
| 51 | |
| 52 java_script = os.path.join( | |
| 53 constants.GetOutDirectory(), 'bin', 'helper', | |
| 54 self._test_instance.suite) | |
| 55 command = [java_script] | 33 command = [java_script] |
| 56 | 34 |
| 57 # Add Jar arguments. | 35 # Add Jar arguments. |
| 58 jar_args = ['-test-jars', self._test_instance.suite + '.jar', | 36 jar_args = ['-test-jars', self._test_instance.suite + '.jar', |
| 59 '-json-results-file', json_file_path] | 37 '-json-results-file', json_file.name] |
| 60 if self._test_instance.test_filter: | 38 if self._test_instance.test_filter: |
| 61 jar_args.extend(['-gtest-filter', self._test_instance.test_filter]) | 39 jar_args.extend(['-gtest-filter', self._test_instance.test_filter]) |
| 62 if self._test_instance.package_filter: | 40 if self._test_instance.package_filter: |
| 63 jar_args.extend(['-package-filter', | 41 jar_args.extend(['-package-filter', |
| 64 self._test_instance.package_filter]) | 42 self._test_instance.package_filter]) |
| 65 if self._test_instance.runner_filter: | 43 if self._test_instance.runner_filter: |
| 66 jar_args.extend(['-runner-filter', self._test_instance.runner_filter]) | 44 jar_args.extend(['-runner-filter', self._test_instance.runner_filter]) |
| 67 command.extend(['--jar-args', '"%s"' % ' '.join(jar_args)]) | 45 command.extend(['--jar-args', '"%s"' % ' '.join(jar_args)]) |
| 68 | 46 |
| 69 # Add JVM arguments. | 47 # Add JVM arguments. |
| 70 jvm_args = ['-Drobolectric.dependency.dir=%s' % | 48 jvm_args = [] |
| 71 self._test_instance.robolectric_runtime_deps_dir, | 49 # TODO(mikecase): Add a --robolectric-dep-dir arg to test runner. |
| 72 '-Ddir.source.root=%s' % constants.DIR_SOURCE_ROOT,] | 50 # Have this arg set by GN in the generated test runner scripts. |
| 73 | 51 jvm_args += [ |
| 74 if self._test_instance.android_manifest_path: | 52 '-Drobolectric.dependency.dir=%s' % os.path.join( |
| 75 jvm_args += ['-Dchromium.robolectric.manifest=%s' % | 53 constants.GetOutDirectory(), 'lib.java', 'third_party', |
| 76 self._test_instance.android_manifest_path] | 54 'robolectric'), |
| 77 | 55 '-Ddir.source.root=%s' % constants.DIR_SOURCE_ROOT, |
| 78 if self._test_instance.package_name: | 56 ] |
| 79 jvm_args += ['-Dchromium.robolectric.package.name=%s' % | |
| 80 self._test_instance.package_name] | |
| 81 | |
| 82 if resource_dirs: | |
| 83 jvm_args += ['-Dchromium.robolectric.resource.dirs=%s' % | |
| 84 ':'.join(resource_dirs)] | |
| 85 | |
| 86 if self._test_instance.coverage_dir: | 57 if self._test_instance.coverage_dir: |
| 87 if not os.path.exists(self._test_instance.coverage_dir): | 58 if not os.path.exists(self._test_instance.coverage_dir): |
| 88 os.makedirs(self._test_instance.coverage_dir) | 59 os.makedirs(self._test_instance.coverage_dir) |
| 89 elif not os.path.isdir(self._test_instance.coverage_dir): | 60 elif not os.path.isdir(self._test_instance.coverage_dir): |
| 90 raise Exception('--coverage-dir takes a directory, not file path.') | 61 raise Exception('--coverage-dir takes a directory, not file path.') |
| 91 jvm_args.append('-Demma.coverage.out.file=%s' % os.path.join( | 62 jvm_args.append('-Demma.coverage.out.file=%s' % os.path.join( |
| 92 self._test_instance.coverage_dir, | 63 self._test_instance.coverage_dir, |
| 93 '%s.ec' % self._test_instance.suite)) | 64 '%s.ec' % self._test_instance.suite)) |
| 94 | |
| 95 if jvm_args: | 65 if jvm_args: |
| 96 command.extend(['--jvm-args', '"%s"' % ' '.join(jvm_args)]) | 66 command.extend(['--jvm-args', '"%s"' % ' '.join(jvm_args)]) |
| 97 | 67 |
| 98 cmd_helper.RunCmd(command) | 68 cmd_helper.RunCmd(command) |
| 99 with open(json_file_path, 'r') as f: | 69 results_list = json_results.ParseResultsFromJson( |
| 100 results_list = json_results.ParseResultsFromJson( | 70 json.loads(json_file.read())) |
| 101 json.loads(f.read())) | |
| 102 | 71 |
| 103 test_run_results = base_test_result.TestRunResults() | 72 test_run_results = base_test_result.TestRunResults() |
| 104 test_run_results.AddResults(results_list) | 73 test_run_results.AddResults(results_list) |
| 105 | 74 |
| 106 return [test_run_results] | 75 return [test_run_results] |
| 107 | 76 |
| 108 #override | 77 #override |
| 109 def TearDown(self): | 78 def TearDown(self): |
| 110 pass | 79 pass |
| OLD | NEW |