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