diff options
author | Matt Clay <mclay@redhat.com> | 2020-02-04 20:21:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-04 20:21:53 +0100 |
commit | f4a80bb600510669801c5d5c0a250952748e99fd (patch) | |
tree | 83ce363b557a66453a9f2c9d0b84c589e09fc57a /test/lib/ansible_test/_internal/executor.py | |
parent | fixes ANSIBLE_DUPLICATE_YAML_DICT_KEY=error crashes (#66786) (diff) | |
download | ansible-f4a80bb600510669801c5d5c0a250952748e99fd.tar.xz ansible-f4a80bb600510669801c5d5c0a250952748e99fd.zip |
Code cleanup and refactoring in ansible-test. (#67063)
* Code cleanup in ansible-test.
* Split out encoding functions.
* Consoldate loading of JSON files.
* Split out disk IO functions.
* Simplify file access.
* Add functions for opening files.
* Replace open calls with appropriate functions.
* Expose more types from typing module.
* Support writing compact JSON.
* Add verbosity argument to display.warning.
* Add changelog entry.
* Update files overlooked during rebase.
* Use `io.open` instead of `open`.
* Fix file opening for imp.load_module.
* Remove use of `r+` mode to access files.
* Add missing import.
* Fix httptester on Python 2.x.
* Clarify changelog fragment.
* Consolidate imports. Remove extra newlines.
* Fix indirect imports.
Diffstat (limited to 'test/lib/ansible_test/_internal/executor.py')
-rw-r--r-- | test/lib/ansible_test/_internal/executor.py | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/test/lib/ansible_test/_internal/executor.py b/test/lib/ansible_test/_internal/executor.py index ef54d6f6d1..b1e18f328f 100644 --- a/test/lib/ansible_test/_internal/executor.py +++ b/test/lib/ansible_test/_internal/executor.py @@ -40,13 +40,20 @@ from .cloud import ( CloudEnvironmentConfig, ) +from .io import ( + make_dirs, + open_text_file, + read_binary_file, + read_text_file, + write_text_file, +) + from .util import ( ApplicationWarning, ApplicationError, SubprocessError, display, remove_tree, - make_dirs, is_shippable, is_binary_file, find_executable, @@ -71,7 +78,6 @@ from .util_common import ( intercept_command, named_temporary_file, run_command, - write_text_file, write_json_test_results, ResultType, handle_layout_messages, @@ -1200,12 +1206,12 @@ def inject_httptester(args): """ comment = ' # ansible-test httptester\n' append_lines = ['127.0.0.1 %s%s' % (host, comment) for host in HTTPTESTER_HOSTS] + hosts_path = '/etc/hosts' - with open('/etc/hosts', 'r+') as hosts_fd: - original_lines = hosts_fd.readlines() + original_lines = read_text_file(hosts_path).splitlines(True) - if not any(line.endswith(comment) for line in original_lines): - hosts_fd.writelines(append_lines) + if not any(line.endswith(comment) for line in original_lines): + write_text_file(hosts_path, ''.join(original_lines + append_lines)) # determine which forwarding mechanism to use pfctl = find_executable('pfctl', required=False) @@ -1510,8 +1516,7 @@ def detect_changes(args): elif args.changed_from or args.changed_path: paths = args.changed_path or [] if args.changed_from: - with open(args.changed_from, 'r') as changes_fd: - paths += changes_fd.read().splitlines() + paths += read_text_file(args.changed_from).splitlines() elif args.changed: paths = detect_changes_local(args) else: @@ -1599,8 +1604,7 @@ def detect_changes_local(args): args.metadata.changes[path] = ((0, 0),) continue - with open(path, 'r') as source_fd: - line_count = len(source_fd.read().splitlines()) + line_count = len(read_text_file(path).splitlines()) args.metadata.changes[path] = ((1, line_count),) @@ -2056,7 +2060,7 @@ class EnvironmentDescription: :type path: str :rtype: str """ - with open(path) as script_fd: + with open_text_file(path) as script_fd: return script_fd.readline().strip() @staticmethod @@ -2070,8 +2074,7 @@ class EnvironmentDescription: file_hash = hashlib.md5() - with open(path, 'rb') as file_fd: - file_hash.update(file_fd.read()) + file_hash.update(read_binary_file(path)) return file_hash.hexdigest() |