ansible-test - Clean up future boilerplate. (#76874)

* ansible-test - Clarify need for empty __init__.py
* ansible-test - Update code-smell boilerplate.
* Update code-smell boilerplate for core.
* Update future boilerplate test for ansible-test.

All ansible-test code (except for targets) and core-specific sanity tests now use the same boilerplate.

The test also checks for unwanted `__future__` and `metaclass` boilerplate.

* Relocate target tools to the correct directory.

Several tools used on target Python versions were incorrectly placed in the controller directory.
pull/76875/head
Matt Clay 3 years ago committed by GitHub
parent 3816815db0
commit d19b506ce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
# Empty __init__.py to allow importing of `ansible_test._util.target.common` under Python 2.x.
# This allows the ansible-test entry point to report supported Python versions before exiting.

@ -83,6 +83,7 @@ ANSIBLE_TEST_CONTROLLER_ROOT = os.path.join(ANSIBLE_TEST_UTIL_ROOT, 'controller'
ANSIBLE_TEST_TARGET_ROOT = os.path.join(ANSIBLE_TEST_UTIL_ROOT, 'target') ANSIBLE_TEST_TARGET_ROOT = os.path.join(ANSIBLE_TEST_UTIL_ROOT, 'target')
ANSIBLE_TEST_TOOLS_ROOT = os.path.join(ANSIBLE_TEST_CONTROLLER_ROOT, 'tools') ANSIBLE_TEST_TOOLS_ROOT = os.path.join(ANSIBLE_TEST_CONTROLLER_ROOT, 'tools')
ANSIBLE_TEST_TARGET_TOOLS_ROOT = os.path.join(ANSIBLE_TEST_TARGET_ROOT, 'tools')
# Modes are set to allow all users the same level of access. # Modes are set to allow all users the same level of access.
# This permits files to be used in tests that change users. # This permits files to be used in tests that change users.

@ -31,7 +31,7 @@ from .util import (
raw_command, raw_command,
ANSIBLE_TEST_DATA_ROOT, ANSIBLE_TEST_DATA_ROOT,
ANSIBLE_TEST_TARGET_ROOT, ANSIBLE_TEST_TARGET_ROOT,
ANSIBLE_TEST_TOOLS_ROOT, ANSIBLE_TEST_TARGET_TOOLS_ROOT,
ApplicationError, ApplicationError,
SubprocessError, SubprocessError,
generate_name, generate_name,
@ -425,7 +425,7 @@ def run_command(
def yamlcheck(python): def yamlcheck(python):
"""Return True if PyYAML has libyaml support, False if it does not and None if it was not found.""" """Return True if PyYAML has libyaml support, False if it does not and None if it was not found."""
result = json.loads(raw_command([python.path, os.path.join(ANSIBLE_TEST_TOOLS_ROOT, 'yamlcheck.py')], capture=True)[0]) result = json.loads(raw_command([python.path, os.path.join(ANSIBLE_TEST_TARGET_TOOLS_ROOT, 'yamlcheck.py')], capture=True)[0])
if not result['yaml']: if not result['yaml']:
return None return None

@ -14,7 +14,7 @@ from .util import (
find_python, find_python,
SubprocessError, SubprocessError,
get_available_python_versions, get_available_python_versions,
ANSIBLE_TEST_TOOLS_ROOT, ANSIBLE_TEST_TARGET_TOOLS_ROOT,
display, display,
remove_tree, remove_tree,
ApplicationError, ApplicationError,
@ -150,7 +150,7 @@ def get_python_real_prefix(args, python_path): # type: (EnvironmentConfig, str)
""" """
Return the real prefix of the specified interpreter or None if the interpreter is not a virtual environment created by 'virtualenv'. Return the real prefix of the specified interpreter or None if the interpreter is not a virtual environment created by 'virtualenv'.
""" """
cmd = [python_path, os.path.join(os.path.join(ANSIBLE_TEST_TOOLS_ROOT, 'virtualenvcheck.py'))] cmd = [python_path, os.path.join(os.path.join(ANSIBLE_TEST_TARGET_TOOLS_ROOT, 'virtualenvcheck.py'))]
check_result = json.loads(run_command(args, cmd, capture=True, always=True)[0]) check_result = json.loads(run_command(args, cmd, capture=True, always=True)[0])
real_prefix = check_result['real_prefix'] real_prefix = check_result['real_prefix']
return real_prefix return real_prefix

@ -1,3 +1,2 @@
"""Nearly empty __init__.py to allow importing under Python 2.x.""" # Empty __init__.py to allow importing of `ansible_test._util.target.common` under Python 2.x.
from __future__ import (absolute_import, division, print_function) # This allows the ansible-test entry point to report supported Python versions before exiting.
__metaclass__ = type

@ -1,6 +1,5 @@
"""Test to verify action plugins have an associated module to provide documentation.""" """Test to verify action plugins have an associated module to provide documentation."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import sys import sys

@ -1,5 +1,4 @@
"""Block the sphinx module from being loaded.""" """Block the sphinx module from being loaded."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
raise ImportError('The sphinx module has been prevented from loading to maintain consistent test results.') raise ImportError('The sphinx module has been prevented from loading to maintain consistent test results.')

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import ast import ast
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import ast import ast
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,8 +1,7 @@
# a script to check for illegal filenames on various Operating Systems. The # a script to check for illegal filenames on various Operating Systems. The
# main rules are derived from restrictions on Windows # main rules are derived from restrictions on Windows
# https://msdn.microsoft.com/en-us/library/aa365247#naming_conventions # https://msdn.microsoft.com/en-us/library/aa365247#naming_conventions
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import struct import struct

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import sys import sys

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,6 +1,5 @@
"""Schema validation of ansible-core's ansible_builtin_runtime.yml and collection's meta/runtime.yml""" """Schema validation of ansible-core's ansible_builtin_runtime.yml and collection's meta/runtime.yml"""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import datetime import datetime
import os import os

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import re import re

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,6 +1,5 @@
"""Read YAML from stdin and write JSON to stdout.""" """Read YAML from stdin and write JSON to stdout."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import json import json
import sys import sys

@ -2,8 +2,7 @@
# (c) 2018, Matt Martz <matt@sivel.net> # (c) 2018, Matt Martz <matt@sivel.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import datetime import datetime
import re import re

@ -2,8 +2,7 @@
# (c) 2018, Matt Martz <matt@sivel.net> # (c) 2018, Matt Martz <matt@sivel.net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import astroid import astroid
from pylint.interfaces import IAstroidChecker from pylint.interfaces import IAstroidChecker

@ -1,7 +1,5 @@
"""A plugin for pylint to identify imports and functions which should not be used.""" """A plugin for pylint to identify imports and functions which should not be used."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import typing as t import typing as t

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
from validate_modules.main import main from validate_modules.main import main

@ -15,6 +15,5 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
__version__ = '0.0.1b' __version__ = '0.0.1b'

@ -15,8 +15,7 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import abc import abc
import argparse import argparse

@ -15,8 +15,7 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import runpy import runpy
import inspect import inspect

@ -3,8 +3,7 @@
# Copyright: (c) 2015, Matt Martz <matt@sivel.net> # Copyright: (c) 2015, Matt Martz <matt@sivel.net>
# Copyright: (c) 2015, Rackspace US, Inc. # Copyright: (c) 2015, Rackspace US, Inc.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re

@ -15,8 +15,7 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import ast import ast
import datetime import datetime

@ -1,6 +1,5 @@
"""Wrapper around yamllint that supports YAML embedded in Ansible modules.""" """Wrapper around yamllint that supports YAML embedded in Ansible modules."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import ast import ast
import json import json

@ -1,6 +1,5 @@
"""Retrieve collection detail.""" """Retrieve collection detail."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import json import json
import os import os

@ -1,6 +1,5 @@
"""Show openssl version.""" """Show openssl version."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import json import json

@ -1,6 +1,5 @@
"""Read YAML from stdin and write JSON to stdout.""" """Read YAML from stdin and write JSON to stdout."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import datetime import datetime
import json import json

@ -1,3 +1,2 @@
"""Nearly empty __init__.py to allow importing under Python 2.x.""" # Empty __init__.py to allow importing of `ansible_test._util.target.common` under Python 2.x.
from __future__ import (absolute_import, division, print_function) # This allows the ansible-test entry point to report supported Python versions before exiting.
__metaclass__ = type

@ -1,3 +1,2 @@
"""Nearly empty __init__.py to allow importing under Python 2.x.""" # Empty __init__.py to allow importing of `ansible_test._util.target.common` under Python 2.x.
from __future__ import (absolute_import, division, print_function) # This allows the ansible-test entry point to report supported Python versions before exiting.
__metaclass__ = type

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -3,7 +3,8 @@
".py" ".py"
], ],
"prefixes": [ "prefixes": [
"test/lib/ansible_test/_internal/" "test/sanity/",
"test/lib/ansible_test/"
], ],
"output": "path-message" "output": "path-message"
} }

@ -1,12 +1,19 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import ast import ast
import sys import sys
def main(): def main():
# The following directories contain code which must work under Python 2.x.
py2_compat = (
'test/lib/ansible_test/_util/target/',
)
for path in sys.argv[1:] or sys.stdin.read().splitlines(): for path in sys.argv[1:] or sys.stdin.read().splitlines():
if any(path.startswith(prefix) for prefix in py2_compat):
continue
with open(path, 'rb') as path_fd: with open(path, 'rb') as path_fd:
lines = path_fd.read().splitlines() lines = path_fd.read().splitlines()
@ -15,11 +22,16 @@ def main():
# Files are allowed to be empty of everything including boilerplate # Files are allowed to be empty of everything including boilerplate
missing = False missing = False
invalid_future = []
for text in lines: for text in lines:
if text == b'from __future__ import annotations': if text == b'from __future__ import annotations':
missing = False missing = False
break break
if text.startswith(b'from __future__ ') or text == b'__metaclass__ = type':
invalid_future.append(text.decode())
if missing: if missing:
with open(path) as file: with open(path) as file:
contents = file.read() contents = file.read()
@ -39,6 +51,9 @@ def main():
if missing: if missing:
print('%s: missing: from __future__ import annotations' % path) print('%s: missing: from __future__ import annotations' % path)
for text in invalid_future:
print('%s: invalid: %s' % (path, text))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

@ -1,6 +1,5 @@
"""Make sure the data in BOTMETA.yml is valid""" """Make sure the data in BOTMETA.yml is valid"""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import glob import glob
import os import os

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os

@ -16,8 +16,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import mmap import mmap
import os import os

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import re import re

@ -1,6 +1,5 @@
"""Prevent unwanted files from being added to the source tree.""" """Prevent unwanted files from being added to the source tree."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import sys import sys

@ -1,6 +1,5 @@
"""Prevent files from being added to directories that are now obsolete.""" """Prevent files from being added to directories that are now obsolete."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import sys import sys

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import contextlib import contextlib
import fnmatch import fnmatch

@ -20,8 +20,7 @@ Test that the release name is present in the list of used up release names
""" """
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
from yaml import safe_load from yaml import safe_load

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import sys import sys

@ -1,6 +1,5 @@
"""Sanity test using rstcheck and sphinx.""" """Sanity test using rstcheck and sphinx."""
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import re import re
import subprocess import subprocess

@ -1,5 +1,4 @@
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import os import os
import re import re

@ -21,8 +21,7 @@ a newer upstream release.
""" """
from __future__ import (absolute_import, division, print_function) from __future__ import annotations
__metaclass__ = type
import fnmatch import fnmatch
import json import json

Loading…
Cancel
Save