Commit 97dc1912 authored by yolandyan's avatar yolandyan Committed by Commit bot

Find annotated tests by exposing API in instrumentation_test_instance

BUG=601909

Review-Url: https://codereview.chromium.org/1851143002
Cr-Commit-Position: refs/heads/master@{#402699}
parent a54d7427
...@@ -59,6 +59,10 @@ class MissingSizeAnnotationError(Exception): ...@@ -59,6 +59,10 @@ class MissingSizeAnnotationError(Exception):
', '.join('@' + a for a in _VALID_ANNOTATIONS)) ', '.join('@' + a for a in _VALID_ANNOTATIONS))
class ProguardPickleException(Exception):
pass
# TODO(jbudorick): Make these private class methods of # TODO(jbudorick): Make these private class methods of
# InstrumentationTestInstance once the instrumentation test_runner is # InstrumentationTestInstance once the instrumentation test_runner is
# deprecated. # deprecated.
...@@ -198,6 +202,154 @@ def ParseCommandLineFlagParameters(annotations): ...@@ -198,6 +202,154 @@ def ParseCommandLineFlagParameters(annotations):
return result if result else None return result if result else None
def FilterTests(tests, test_filter=None, annotations=None,
excluded_annotations=None):
"""Filter a list of tests
Args:
tests: a list of tests. e.g. [
{'annotations": {}, 'class': 'com.example.TestA', 'methods':[]},
{'annotations": {}, 'class': 'com.example.TestB', 'methods':[]}]
test_filter: googletest-style filter string.
annotations: a dict of wanted annotations for test methods.
exclude_annotations: a dict of annotations to exclude.
Return:
A list of filtered tests
"""
def gtest_filter(c, m):
if not test_filter:
return True
# Allow fully-qualified name as well as an omitted package.
names = ['%s.%s' % (c['class'], m['method']),
'%s.%s' % (c['class'].split('.')[-1], m['method'])]
return unittest_util.FilterTestNames(names, test_filter)
def annotation_filter(all_annotations):
if not annotations:
return True
return any_annotation_matches(annotations, all_annotations)
def excluded_annotation_filter(all_annotations):
if not excluded_annotations:
return True
return not any_annotation_matches(excluded_annotations,
all_annotations)
def any_annotation_matches(filter_annotations, all_annotations):
return any(
ak in all_annotations
and annotation_value_matches(av, all_annotations[ak])
for ak, av in filter_annotations.iteritems())
def annotation_value_matches(filter_av, av):
if filter_av is None:
return True
elif isinstance(av, dict):
return filter_av in av['value']
elif isinstance(av, list):
return filter_av in av
return filter_av == av
filtered_classes = []
for c in tests:
filtered_methods = []
for m in c['methods']:
# Gtest filtering
if not gtest_filter(c, m):
continue
all_annotations = dict(c['annotations'])
all_annotations.update(m['annotations'])
# Enforce that all tests declare their size.
if not any(a in _VALID_ANNOTATIONS for a in all_annotations):
raise MissingSizeAnnotationError('%s.%s' % (c['class'], m['method']))
if (not annotation_filter(all_annotations)
or not excluded_annotation_filter(all_annotations)):
continue
filtered_methods.append(m)
if filtered_methods:
filtered_class = dict(c)
filtered_class['methods'] = filtered_methods
filtered_classes.append(filtered_class)
return filtered_classes
def GetAllTests(test_jar):
pickle_path = '%s-proguard.pickle' % test_jar
try:
tests = _GetTestsFromPickle(pickle_path, test_jar)
except ProguardPickleException as e:
logging.info('Could not get tests from pickle: %s', e)
logging.info('Getting tests from JAR via proguard.')
tests = _GetTestsFromProguard(test_jar)
_SaveTestsToPickle(pickle_path, test_jar, tests)
return tests
def _GetTestsFromPickle(pickle_path, jar_path):
if not os.path.exists(pickle_path):
raise ProguardPickleException('%s does not exist.' % pickle_path)
if os.path.getmtime(pickle_path) <= os.path.getmtime(jar_path):
raise ProguardPickleException(
'%s newer than %s.' % (jar_path, pickle_path))
with open(pickle_path, 'r') as pickle_file:
pickle_data = pickle.loads(pickle_file.read())
jar_md5 = md5sum.CalculateHostMd5Sums(jar_path)[jar_path]
if pickle_data['VERSION'] != _PICKLE_FORMAT_VERSION:
raise ProguardPickleException('PICKLE_FORMAT_VERSION has changed.')
if pickle_data['JAR_MD5SUM'] != jar_md5:
raise ProguardPickleException('JAR file MD5 sum differs.')
return pickle_data['TEST_METHODS']
def _GetTestsFromProguard(jar_path):
p = proguard.Dump(jar_path)
class_lookup = dict((c['class'], c) for c in p['classes'])
def is_test_class(c):
return c['class'].endswith('Test')
def is_test_method(m):
return m['method'].startswith('test')
def recursive_class_annotations(c):
s = c['superclass']
if s in class_lookup:
a = recursive_class_annotations(class_lookup[s])
else:
a = {}
a.update(c['annotations'])
return a
def stripped_test_class(c):
return {
'class': c['class'],
'annotations': recursive_class_annotations(c),
'methods': [m for m in c['methods'] if is_test_method(m)],
}
return [stripped_test_class(c) for c in p['classes']
if is_test_class(c)]
def _SaveTestsToPickle(pickle_path, jar_path, tests):
jar_md5 = md5sum.CalculateHostMd5Sums(jar_path)[jar_path]
pickle_data = {
'VERSION': _PICKLE_FORMAT_VERSION,
'JAR_MD5SUM': jar_md5,
'TEST_METHODS': tests,
}
with open(pickle_path, 'w') as pickle_file:
pickle.dump(pickle_data, pickle_file)
class InstrumentationTestInstance(test_instance.TestInstance): class InstrumentationTestInstance(test_instance.TestInstance):
def __init__(self, args, isolate_delegate, error_func): def __init__(self, args, isolate_delegate, error_func):
...@@ -294,6 +446,7 @@ class InstrumentationTestInstance(test_instance.TestInstance): ...@@ -294,6 +446,7 @@ class InstrumentationTestInstance(test_instance.TestInstance):
for package_info in constants.PACKAGE_INFO.itervalues(): for package_info in constants.PACKAGE_INFO.itervalues():
if package_under_test == package_info.package: if package_under_test == package_info.package:
self._package_info = package_info self._package_info = package_info
break
if not self._package_info: if not self._package_info:
logging.warning('Unable to find package info for %s', self._test_package) logging.warning('Unable to find package info for %s', self._test_package)
...@@ -493,144 +646,12 @@ class InstrumentationTestInstance(test_instance.TestInstance): ...@@ -493,144 +646,12 @@ class InstrumentationTestInstance(test_instance.TestInstance):
return self._data_deps return self._data_deps
def GetTests(self): def GetTests(self):
pickle_path = '%s-proguard.pickle' % self.test_jar tests = GetAllTests(self.test_jar)
try: filtered_tests = FilterTests(
tests = self._GetTestsFromPickle(pickle_path, self.test_jar) tests, self._test_filter, self._annotations, self._excluded_annotations)
except self.ProguardPickleException as e: return self._ParametrizeTestsWithFlags(self._InflateTests(filtered_tests))
logging.info('Getting tests from JAR via proguard. (%s)', str(e))
tests = self._GetTestsFromProguard(self.test_jar)
self._SaveTestsToPickle(pickle_path, self.test_jar, tests)
return self._ParametrizeTestsWithFlags(
self._InflateTests(self._FilterTests(tests)))
class ProguardPickleException(Exception):
pass
def _GetTestsFromPickle(self, pickle_path, jar_path):
if not os.path.exists(pickle_path):
raise self.ProguardPickleException('%s does not exist.' % pickle_path)
if os.path.getmtime(pickle_path) <= os.path.getmtime(jar_path):
raise self.ProguardPickleException(
'%s newer than %s.' % (jar_path, pickle_path))
with open(pickle_path, 'r') as pickle_file:
pickle_data = pickle.loads(pickle_file.read())
jar_md5 = md5sum.CalculateHostMd5Sums(jar_path)[jar_path]
try:
if pickle_data['VERSION'] != _PICKLE_FORMAT_VERSION:
raise self.ProguardPickleException('PICKLE_FORMAT_VERSION has changed.')
if pickle_data['JAR_MD5SUM'] != jar_md5:
raise self.ProguardPickleException('JAR file MD5 sum differs.')
return pickle_data['TEST_METHODS']
except TypeError as e:
logging.error(pickle_data)
raise self.ProguardPickleException(str(e))
# pylint: disable=no-self-use # pylint: disable=no-self-use
def _GetTestsFromProguard(self, jar_path):
p = proguard.Dump(jar_path)
def is_test_class(c):
return c['class'].endswith('Test')
def is_test_method(m):
return m['method'].startswith('test')
class_lookup = dict((c['class'], c) for c in p['classes'])
def recursive_get_class_annotations(c):
s = c['superclass']
if s in class_lookup:
a = recursive_get_class_annotations(class_lookup[s])
else:
a = {}
a.update(c['annotations'])
return a
def stripped_test_class(c):
return {
'class': c['class'],
'annotations': recursive_get_class_annotations(c),
'methods': [m for m in c['methods'] if is_test_method(m)],
}
return [stripped_test_class(c) for c in p['classes']
if is_test_class(c)]
def _SaveTestsToPickle(self, pickle_path, jar_path, tests):
jar_md5 = md5sum.CalculateHostMd5Sums(jar_path)[jar_path]
pickle_data = {
'VERSION': _PICKLE_FORMAT_VERSION,
'JAR_MD5SUM': jar_md5,
'TEST_METHODS': tests,
}
with open(pickle_path, 'w') as pickle_file:
pickle.dump(pickle_data, pickle_file)
def _FilterTests(self, tests):
def gtest_filter(c, m):
if not self._test_filter:
return True
# Allow fully-qualified name as well as an omitted package.
names = ['%s.%s' % (c['class'], m['method']),
'%s.%s' % (c['class'].split('.')[-1], m['method'])]
return unittest_util.FilterTestNames(names, self._test_filter)
def annotation_filter(all_annotations):
if not self._annotations:
return True
return any_annotation_matches(self._annotations, all_annotations)
def excluded_annotation_filter(all_annotations):
if not self._excluded_annotations:
return True
return not any_annotation_matches(self._excluded_annotations,
all_annotations)
def any_annotation_matches(filter_annotations, all_annotations):
return any(
ak in all_annotations
and annotation_value_matches(av, all_annotations[ak])
for ak, av in filter_annotations.iteritems())
def annotation_value_matches(filter_av, av):
if filter_av is None:
return True
elif isinstance(av, dict):
return filter_av in av['value']
elif isinstance(av, list):
return filter_av in av
return filter_av == av
filtered_classes = []
for c in tests:
filtered_methods = []
for m in c['methods']:
# Gtest filtering
if not gtest_filter(c, m):
continue
all_annotations = dict(c['annotations'])
all_annotations.update(m['annotations'])
# Enforce that all tests declare their size.
if not any(a in _VALID_ANNOTATIONS for a in all_annotations):
raise MissingSizeAnnotationError('%s.%s' % (c['class'], m['method']))
if (not annotation_filter(all_annotations)
or not excluded_annotation_filter(all_annotations)):
continue
filtered_methods.append(m)
if filtered_methods:
filtered_class = dict(c)
filtered_class['methods'] = filtered_methods
filtered_classes.append(filtered_class)
return filtered_classes
def _InflateTests(self, tests): def _InflateTests(self, tests):
inflated_tests = [] inflated_tests = []
for c in tests: for c in tests:
...@@ -687,4 +708,3 @@ class InstrumentationTestInstance(test_instance.TestInstance): ...@@ -687,4 +708,3 @@ class InstrumentationTestInstance(test_instance.TestInstance):
def TearDown(self): def TearDown(self):
if self._isolate_delegate: if self._isolate_delegate:
self._isolate_delegate.Clear() self._isolate_delegate.Clear()
...@@ -16,6 +16,8 @@ from pylib.instrumentation import instrumentation_test_instance ...@@ -16,6 +16,8 @@ from pylib.instrumentation import instrumentation_test_instance
with host_paths.SysPath(host_paths.PYMOCK_PATH): with host_paths.SysPath(host_paths.PYMOCK_PATH):
import mock # pylint: disable=import-error import mock # pylint: disable=import-error
_INSTRUMENTATION_TEST_INSTANCE_PATH = (
'pylib.instrumentation.instrumentation_test_instance.%s')
class InstrumentationTestInstanceTest(unittest.TestCase): class InstrumentationTestInstanceTest(unittest.TestCase):
...@@ -25,8 +27,7 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -25,8 +27,7 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
@staticmethod @staticmethod
def createTestInstance(): def createTestInstance():
c = ('pylib.instrumentation.instrumentation_test_instance.' c = _INSTRUMENTATION_TEST_INSTANCE_PATH % 'InstrumentationTestInstance'
'InstrumentationTestInstance')
with mock.patch('%s._initializeApkAttributes' % c), ( with mock.patch('%s._initializeApkAttributes' % c), (
mock.patch('%s._initializeDataDependencyAttributes' % c)), ( mock.patch('%s._initializeDataDependencyAttributes' % c)), (
mock.patch('%s._initializeTestFilterAttributes' % c)), ( mock.patch('%s._initializeTestFilterAttributes' % c)), (
...@@ -66,8 +67,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -66,8 +67,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
} }
] ]
o._GetTestsFromPickle = mock.MagicMock(return_value=raw_tests)
expected_tests = [ expected_tests = [
{ {
'annotations': { 'annotations': {
...@@ -95,7 +94,10 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -95,7 +94,10 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
}, },
] ]
with mock.patch(_INSTRUMENTATION_TEST_INSTANCE_PATH % '_GetTestsFromPickle',
return_value=raw_tests):
actual_tests = o.GetTests() actual_tests = o.GetTests()
self.assertEquals(actual_tests, expected_tests) self.assertEquals(actual_tests, expected_tests)
def testGetTests_simpleGtestFilter(self): def testGetTests_simpleGtestFilter(self):
...@@ -117,9 +119,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -117,9 +119,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
} }
] ]
o._GetTestsFromPickle = mock.MagicMock(return_value=raw_tests)
o._test_filter = 'org.chromium.test.SampleTest.testMethod1'
expected_tests = [ expected_tests = [
{ {
'annotations': { 'annotations': {
...@@ -131,7 +130,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -131,7 +130,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
}, },
] ]
o._test_filter = 'org.chromium.test.SampleTest.testMethod1'
with mock.patch(_INSTRUMENTATION_TEST_INSTANCE_PATH % '_GetTestsFromPickle',
return_value=raw_tests):
actual_tests = o.GetTests() actual_tests = o.GetTests()
self.assertEquals(actual_tests, expected_tests) self.assertEquals(actual_tests, expected_tests)
def testGetTests_wildcardGtestFilter(self): def testGetTests_wildcardGtestFilter(self):
...@@ -163,9 +166,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -163,9 +166,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
} }
] ]
o._GetTestsFromPickle = mock.MagicMock(return_value=raw_tests)
o._test_filter = 'org.chromium.test.SampleTest2.*'
expected_tests = [ expected_tests = [
{ {
'annotations': { 'annotations': {
...@@ -177,7 +177,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -177,7 +177,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
}, },
] ]
o._test_filter = 'org.chromium.test.SampleTest2.*'
with mock.patch(_INSTRUMENTATION_TEST_INSTANCE_PATH % '_GetTestsFromPickle',
return_value=raw_tests):
actual_tests = o.GetTests() actual_tests = o.GetTests()
self.assertEquals(actual_tests, expected_tests) self.assertEquals(actual_tests, expected_tests)
@unittest.skip('crbug.com/623047') @unittest.skip('crbug.com/623047')
...@@ -264,9 +268,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -264,9 +268,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
} }
] ]
o._GetTestsFromPickle = mock.MagicMock(return_value=raw_tests)
o._annotations = {'SmallTest': None}
expected_tests = [ expected_tests = [
{ {
'annotations': { 'annotations': {
...@@ -286,7 +287,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -286,7 +287,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
}, },
] ]
o._annotations = {'SmallTest': None}
with mock.patch(_INSTRUMENTATION_TEST_INSTANCE_PATH % '_GetTestsFromPickle',
return_value=raw_tests):
actual_tests = o.GetTests() actual_tests = o.GetTests()
self.assertEquals(actual_tests, expected_tests) self.assertEquals(actual_tests, expected_tests)
def testGetTests_excludedAnnotationFilter(self): def testGetTests_excludedAnnotationFilter(self):
...@@ -318,9 +323,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -318,9 +323,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
} }
] ]
o._GetTestsFromPickle = mock.MagicMock(return_value=raw_tests)
o._excluded_annotations = {'SmallTest': None}
expected_tests = [ expected_tests = [
{ {
'annotations': { 'annotations': {
...@@ -332,7 +334,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -332,7 +334,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
}, },
] ]
o._excluded_annotations = {'SmallTest': None}
with mock.patch(_INSTRUMENTATION_TEST_INSTANCE_PATH % '_GetTestsFromPickle',
return_value=raw_tests):
actual_tests = o.GetTests() actual_tests = o.GetTests()
self.assertEquals(actual_tests, expected_tests) self.assertEquals(actual_tests, expected_tests)
def testGetTests_annotationSimpleValueFilter(self): def testGetTests_annotationSimpleValueFilter(self):
...@@ -373,9 +379,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -373,9 +379,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
} }
] ]
o._GetTestsFromPickle = mock.MagicMock(return_value=raw_tests)
o._annotations = {'TestValue': '1'}
expected_tests = [ expected_tests = [
{ {
'annotations': { 'annotations': {
...@@ -388,7 +391,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -388,7 +391,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
}, },
] ]
o._annotations = {'TestValue': '1'}
with mock.patch(_INSTRUMENTATION_TEST_INSTANCE_PATH % '_GetTestsFromPickle',
return_value=raw_tests):
actual_tests = o.GetTests() actual_tests = o.GetTests()
self.assertEquals(actual_tests, expected_tests) self.assertEquals(actual_tests, expected_tests)
def testGetTests_annotationDictValueFilter(self): def testGetTests_annotationDictValueFilter(self):
...@@ -420,9 +427,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -420,9 +427,6 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
} }
] ]
o._GetTestsFromPickle = mock.MagicMock(return_value=raw_tests)
o._annotations = {'Feature': 'Bar'}
expected_tests = [ expected_tests = [
{ {
'annotations': { 'annotations': {
...@@ -434,7 +438,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase): ...@@ -434,7 +438,11 @@ class InstrumentationTestInstanceTest(unittest.TestCase):
}, },
] ]
o._annotations = {'Feature': 'Bar'}
with mock.patch(_INSTRUMENTATION_TEST_INSTANCE_PATH % '_GetTestsFromPickle',
return_value=raw_tests):
actual_tests = o.GetTests() actual_tests = o.GetTests()
self.assertEquals(actual_tests, expected_tests) self.assertEquals(actual_tests, expected_tests)
def testGenerateTestResults_noStatus(self): def testGenerateTestResults_noStatus(self):
......
#!/usr/bin/env python
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Finds all the annotated tests from proguard dump"""
import argparse
import datetime
import json
import linecache
import logging
import os
import pprint
import re
import sys
import time
_SRC_DIR = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..'))
sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'devil'))
from devil.utils import cmd_helper
sys.path.append(os.path.join(_SRC_DIR, 'build', 'android'))
from pylib import constants
from pylib.instrumentation import instrumentation_test_instance
_CRBUG_ID_PATTERN = re.compile(r'crbug(?:.com)?/(\d+)')
_EXPORT_TIME_FORMAT = '%Y%m%dT%H%M%S'
_GIT_LOG_TIME_PATTERN = re.compile(r'\d+')
_GIT_LOG_MESSAGE_PATTERN = r'Cr-Commit-Position: refs/heads/master@{#(\d+)}'
_GIT_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S'
def _GetBugId(test_annotations):
"""Find and return the test bug id from its annoation message elements"""
# TODO(yolandyan): currently the script only supports on bug id per method,
# add support for multiple bug id
for content in test_annotations.itervalues():
if content and content.get('message'):
search_result = re.search(_CRBUG_ID_PATTERN, content.get('message'))
if search_result is not None:
return int(search_result.group(1))
return None
def _GetTests(test_apks, apk_output_dir):
"""Return a list of all annotated tests and total test count"""
result = []
total_test_count = 0
for test_apk in test_apks:
logging.info('Current test apk: %s', test_apk)
test_jar = os.path.join(
apk_output_dir, constants.SDK_BUILD_TEST_JAVALIB_DIR,
'%s.jar' % test_apk)
all_tests = instrumentation_test_instance.GetAllTests(test_jar=test_jar)
for test_class in all_tests:
class_path = test_class['class']
class_name = test_class['class'].split('.')[-1]
class_annotations = test_class['annotations']
class_bug_id = _GetBugId(class_annotations)
for test_method in test_class['methods']:
total_test_count += 1
# getting annotation of each test case
test_annotations = test_method['annotations']
test_bug_id = _GetBugId(test_annotations)
test_bug_id = test_bug_id if test_bug_id else class_bug_id
test_annotations.update(class_annotations)
# getting test method name of each test
test_name = test_method['method']
test_dict = {
'bug_id': test_bug_id,
'annotations': test_annotations,
'test_name': test_name,
'test_apk_name': test_apk,
'class_name': class_name,
'class_path': class_path
}
result.append(test_dict)
logging.info('Total count of tests in all test apks: %d', total_test_count)
return result, total_test_count
def _GetReportMeta(utc_script_runtime_string, total_test_count):
"""Returns a dictionary of the report's metadata"""
revision = cmd_helper.GetCmdOutput(['git', 'rev-parse', 'HEAD']).strip()
raw_string = cmd_helper.GetCmdOutput(
['git', 'log', '--pretty=format:%at', '--max-count=1', 'HEAD'])
time_string_search = re.search(_GIT_LOG_TIME_PATTERN, raw_string)
if time_string_search is None:
raise Exception('Timestamp format incorrect, expected all digits, got %s'
% raw_string)
raw_string = cmd_helper.GetCmdOutput(
['git', 'log', '--pretty=format:%b', '--max-count=1', 'HEAD'])
commit_pos_search = re.search(_GIT_LOG_MESSAGE_PATTERN, raw_string)
if commit_pos_search is None:
raise Exception('Cr-Commit-Position is not found, potentially running with '
'uncommited HEAD')
commit_pos = int(commit_pos_search.group(1))
utc_revision_time = datetime.datetime.utcfromtimestamp(
int(time_string_search.group(0)))
utc_revision_time = utc_revision_time.strftime(_EXPORT_TIME_FORMAT)
logging.info(
'revision is %s, revision time is %s', revision, utc_revision_time)
return {
'revision': revision,
'commit_pos': commit_pos,
'script_runtime': utc_script_runtime_string,
'revision_time': utc_revision_time,
'platform': 'android',
'total_test_count': total_test_count
}
def _GetReport(test_apks, script_runtime_string, apk_output_dir):
"""Generate the dictionary of report data
Args:
test_apks: a list of apks for search for tests
script_runtime_string: the time when the script is run at
format: '%Y%m%dT%H%M%S'
"""
test_data, total_test_count = _GetTests(test_apks, apk_output_dir)
report_meta = _GetReportMeta(script_runtime_string, total_test_count)
report_data = {
'metadata': report_meta,
'tests': test_data
}
return report_data
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--test-apks', nargs='+', dest='test_apks',
required=True,
help='List all test apks file name that the script uses '
'to fetch tracked tests from')
parser.add_argument('--json-output-dir', required=True,
help='JSON file output dir')
parser.add_argument('--apk-output-dir', required=True,
help='The output directory of test apks')
parser.add_argument('--timestamp-string',
help='The time when this script is run, passed in by the '
'recipe that runs this script so both the recipe '
'and this script use it to format output json name')
parser.add_argument('-v', '--verbose', action='store_true', default=False,
help='INFO verbosity')
arguments = parser.parse_args(sys.argv[1:])
logging.basicConfig(
level=logging.INFO if arguments.verbose else logging.WARNING)
if arguments.timestamp_string is None:
script_runtime = datetime.datetime.utcnow()
script_runtime_string = script_runtime.strftime(_EXPORT_TIME_FORMAT)
else:
script_runtime = arguments.timestamp_string
logging.info('Build time is %s', script_runtime_string)
apk_output_dir = os.path.abspath(os.path.join(
constants.DIR_SOURCE_ROOT, arguments.apk_output_dir))
report_data = _GetReport(
arguments.test_apks, script_runtime_string, apk_output_dir)
json_output_path = os.path.join(
arguments.json_output_dir,
'%s-android-chrome.json' % script_runtime_string)
with open(json_output_path, 'w') as f:
json.dump(report_data, f, sort_keys=True, separators=(',',': '))
logging.info('Saved json output file to %s', json_output_path)
if __name__ == '__main__':
sys.exit(main())
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment