diff options
author | Matt Clay <matt@mystile.com> | 2019-08-27 23:40:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-27 23:40:06 -0700 |
commit | f5d829392a17e20d0ba02c30e5a377a43cf70cfd (patch) | |
tree | 6dba1bdf24e10806d8713bfc18b1c7357b4a8ed7 /test/lib/ansible_test/_internal/util_common.py | |
parent | bf108ee7bfc0270715fe26d143bc5885eb398fb5 (diff) | |
download | ansible-f5d829392a17e20d0ba02c30e5a377a43cf70cfd.tar.gz |
Overhaul ansible-test test path handling. (#61416)
* Remove .keep files from test/results/ dirs.
* Remove classification of test/results/ dir.
* Add results_relative to data context.
* Use variables in delegation paths.
* Standardize file writing and results paths.
* Fix issues reported by PyCharm.
* Clean up invocation of coverage command.
It now runs through the injector.
* Hack to allow intercept_command in cover.py.
* Simplify git ignore for test results.
* Use test result tmp dir instead of cache dir.
* Remove old .pytest_cache reference.
* Fix unit test docker delegation.
* Show HTML report link.
* Clean up more results references.
* Move import sanity test output to .tmp dir.
* Exclude test results dir from coverage.
* Fix import sanity test lib paths.
* Fix hard-coded import test paths.
* Fix most hard-coded integration test paths.
* Fix PyCharm warnings.
* Fix import placement.
* Fix integration test dir path.
* Fix Shippable scripts.
* Fix Shippable matrix check.
* Overhaul key pair management.
Diffstat (limited to 'test/lib/ansible_test/_internal/util_common.py')
-rw-r--r-- | test/lib/ansible_test/_internal/util_common.py | 78 |
1 files changed, 74 insertions, 4 deletions
diff --git a/test/lib/ansible_test/_internal/util_common.py b/test/lib/ansible_test/_internal/util_common.py index 43081fb279..d41343bf5a 100644 --- a/test/lib/ansible_test/_internal/util_common.py +++ b/test/lib/ansible_test/_internal/util_common.py @@ -4,15 +4,17 @@ __metaclass__ = type import atexit import contextlib +import json import os import shutil import tempfile import textwrap +from . import types as t + from .util import ( common_environment, COVERAGE_CONFIG_NAME, - COVERAGE_OUTPUT_NAME, display, find_python, is_shippable, @@ -22,6 +24,7 @@ from .util import ( raw_command, to_bytes, ANSIBLE_TEST_DATA_ROOT, + make_dirs, ) from .data import ( @@ -29,6 +32,47 @@ from .data import ( ) +class ResultType: + """Test result type.""" + BOT = None # type: ResultType + COVERAGE = None # type: ResultType + DATA = None # type: ResultType + JUNIT = None # type: ResultType + LOGS = None # type: ResultType + REPORTS = None # type: ResultType + TMP = None # type: ResultType + + @staticmethod + def _populate(): + ResultType.BOT = ResultType('bot') + ResultType.COVERAGE = ResultType('coverage') + ResultType.DATA = ResultType('data') + ResultType.JUNIT = ResultType('junit') + ResultType.LOGS = ResultType('logs') + ResultType.REPORTS = ResultType('reports') + ResultType.TMP = ResultType('.tmp') + + def __init__(self, name): # type: (str) -> None + self.name = name + + @property + def relative_path(self): # type: () -> str + """The content relative path to the results.""" + return os.path.join(data_context().results_relative, self.name) + + @property + def path(self): # type: () -> str + """The absolute path to the results.""" + return os.path.join(data_context().results, self.name) + + def __str__(self): # type: () -> str + return self.name + + +# noinspection PyProtectedMember +ResultType._populate() # pylint: disable=protected-access + + class CommonConfig: """Configuration common to all commands.""" def __init__(self, args, command): @@ -75,6 +119,33 @@ def named_temporary_file(args, prefix, suffix, directory, content): yield tempfile_fd.name +def write_json_test_results(category, name, content): # type: (ResultType, str, t.Union[t.List[t.Any], t.Dict[str, t.Any]]) -> None + """Write the given json content to the specified test results path, creating directories as needed.""" + path = os.path.join(category.path, name) + write_json_file(path, content, create_directories=True) + + +def write_text_test_results(category, name, content): # type: (ResultType, str, str) -> None + """Write the given text content to the specified test results path, creating directories as needed.""" + path = os.path.join(category.path, name) + write_text_file(path, content, create_directories=True) + + +def write_json_file(path, content, create_directories=False): # type: (str, t.Union[t.List[t.Any], t.Dict[str, t.Any]], bool) -> None + """Write the given json content to the specified path, optionally creating missing directories.""" + text_content = json.dumps(content, sort_keys=True, indent=4, ensure_ascii=False) + '\n' + write_text_file(path, text_content, create_directories=create_directories) + + +def write_text_file(path, content, create_directories=False): # type: (str, str, bool) -> None + """Write the given text content to the specified path, optionally creating missing directories.""" + if create_directories: + make_dirs(os.path.dirname(path)) + + with open(to_bytes(path), 'wb') as file: + file.write(to_bytes(content)) + + def get_python_path(args, interpreter): """ :type args: TestConfig @@ -126,8 +197,7 @@ def get_python_path(args, interpreter): execv(python, [python] + argv[1:]) ''' % (interpreter, interpreter)).lstrip() - with open(injected_interpreter, 'w') as python_fd: - python_fd.write(code) + write_text_file(injected_interpreter, code) os.chmod(injected_interpreter, MODE_FILE_EXECUTE) @@ -173,7 +243,7 @@ def get_coverage_environment(args, target_name, version, temp_path, module_cover raise Exception('No temp path and no coverage config base path. Check for missing coverage_context usage.') config_file = os.path.join(coverage_config_base_path, COVERAGE_CONFIG_NAME) - coverage_file = os.path.join(coverage_output_base_path, COVERAGE_OUTPUT_NAME, '%s=%s=%s=%s=coverage' % ( + coverage_file = os.path.join(coverage_output_base_path, ResultType.COVERAGE.name, '%s=%s=%s=%s=coverage' % ( args.command, target_name, args.coverage_label or 'local-%s' % version, 'python-%s' % version)) if not args.explain and not os.path.exists(config_file): |