diff options
author | 2017-12-06 15:57:31 -0800 | |
---|---|---|
committer | 2017-12-20 15:05:17 -0800 | |
commit | efb892d858c6bd5513205747ab710db830a1c5bb (patch) | |
tree | 240a4c28351736bf6b108371c3c03c7ec0122d09 /tools/auto_gen_test_config.py | |
parent | a53865d108097a95a6635ad26c35d6426e48655d (diff) |
Auto generate test config files
This change allows one to run atest <module name>. The TradeFed test
config file will be automatically created in testcases directory based
on the settings.
Bug: 69929803, 69932044
Test: m -j NotificationStressTests libminijail_test
add following lines in notificationtests/Android.mk,
before "include $(BUILD_PACKAGE)"
LOCAL_COMPATIBILITY_SUITE := general-test
Change-Id: Ib5e9cd152d957b5d5beb6ed23a899a021a7e06d6
Diffstat (limited to 'tools/auto_gen_test_config.py')
-rwxr-xr-x | tools/auto_gen_test_config.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tools/auto_gen_test_config.py b/tools/auto_gen_test_config.py new file mode 100755 index 0000000000..fa018f4579 --- /dev/null +++ b/tools/auto_gen_test_config.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +# +# Copyright (C) 2017 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""A tool to generate TradeFed test config file. +""" + +import os +import shutil +import sys +from xml.dom.minidom import parse + +ATTRIBUTE_LABEL = 'android:label' +ATTRIBUTE_RUNNER = 'android:name' +ATTRIBUTE_TARGET_PACKAGE = 'android:targetPackage' + +PLACEHOLDER_LABEL = '{LABEL}' +PLACEHOLDER_MODULE = '{MODULE}' +PLACEHOLDER_PACKAGE = '{PACKAGE}' +PLACEHOLDER_RUNNER = '{RUNNER}' +PLACEHOLDER_TEST_TYPE = '{TEST_TYPE}' + + +def main(argv): + """Entry point of auto_gen_test_config. + + Args: + argv: A list of arguments. + Returns: + 0 if no error, otherwise 1. + """ + if len(argv) != 4: + sys.stderr.write( + 'Invalid arguements. The script requires 4 arguments for file paths: ' + 'target_config android_manifest empty_config ' + 'instrumentation_test_config_template.\n') + return 1 + target_config = argv[0] + android_manifest = argv[1] + empty_config = argv[2] + instrumentation_test_config_template = argv[3] + + manifest = parse(android_manifest) + instrumentation_elements = manifest.getElementsByTagName('instrumentation') + if len(instrumentation_elements) != 1: + # Failed to locate instrumentation element in AndroidManifest file. + # Empty test config file will be created. + shutil.copyfile(empty_config, target_config) + return 0 + + module = os.path.splitext(os.path.basename(target_config))[0] + instrumentation = instrumentation_elements[0] + if instrumentation.attributes.has_key(ATTRIBUTE_LABEL): + label = instrumentation.attributes[ATTRIBUTE_LABEL].value + else: + label = module + runner = instrumentation.attributes[ATTRIBUTE_RUNNER].value + package = instrumentation.attributes[ATTRIBUTE_TARGET_PACKAGE].value + test_type = ('AndroidJUnitTest' if runner.endswith('.AndroidJUnitRunner') + else 'InstrumentationTest') + + with open(instrumentation_test_config_template) as template: + config = template.read() + config = config.replace(PLACEHOLDER_LABEL, label) + config = config.replace(PLACEHOLDER_MODULE, module) + config = config.replace(PLACEHOLDER_PACKAGE, package) + config = config.replace(PLACEHOLDER_TEST_TYPE, test_type) + config = config.replace(PLACEHOLDER_RUNNER, runner) + with open(target_config, 'w') as config_file: + config_file.write(config) + return 0 + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) |