Support comments in ansible-test flat files.

(cherry picked from commit 5a3000af19)
This commit is contained in:
Matt Clay 2018-09-19 23:20:27 -07:00 committed by Toshio Kuratomi
parent 7842b99106
commit cfe86582f2
13 changed files with 135 additions and 96 deletions

View file

@ -12,6 +12,7 @@ from lib.util import (
raw_command, raw_command,
get_docker_completion, get_docker_completion,
generate_pip_command, generate_pip_command,
read_lines_without_comments,
) )
from lib.delegation import ( from lib.delegation import (
@ -687,8 +688,7 @@ def complete_remote(prefix, parsed_args, **_):
""" """
del parsed_args del parsed_args
with open('test/runner/completion/remote.txt', 'r') as completion_fd: images = read_lines_without_comments('test/runner/completion/remote.txt', remove_blank_lines=True)
images = completion_fd.read().splitlines()
return [i for i in images if i.startswith(prefix)] return [i for i in images if i.startswith(prefix)]
@ -701,12 +701,10 @@ def complete_remote_shell(prefix, parsed_args, **_):
""" """
del parsed_args del parsed_args
with open('test/runner/completion/remote.txt', 'r') as completion_fd: images = read_lines_without_comments('test/runner/completion/remote.txt', remove_blank_lines=True)
images = completion_fd.read().splitlines()
# 2008 doesn't support SSH so we do not add to the list of valid images # 2008 doesn't support SSH so we do not add to the list of valid images
with open('test/runner/completion/windows.txt', 'r') as completion_fd: images.extend(["windows/%s" % i for i in read_lines_without_comments('test/runner/completion/windows.txt', remove_blank_lines=True) if i != '2008'])
images.extend(["windows/%s" % i for i in completion_fd.read().splitlines() if i != '2008'])
return [i for i in images if i.startswith(prefix)] return [i for i in images if i.startswith(prefix)]
@ -730,8 +728,7 @@ def complete_windows(prefix, parsed_args, **_):
:type parsed_args: any :type parsed_args: any
:rtype: list[str] :rtype: list[str]
""" """
with open('test/runner/completion/windows.txt', 'r') as completion_fd: images = read_lines_without_comments('test/runner/completion/windows.txt', remove_blank_lines=True)
images = completion_fd.read().splitlines()
return [i for i in images if i.startswith(prefix) and (not parsed_args.windows or i not in parsed_args.windows)] return [i for i in images if i.startswith(prefix) and (not parsed_args.windows or i not in parsed_args.windows)]
@ -742,8 +739,7 @@ def complete_network_platform(prefix, parsed_args, **_):
:type parsed_args: any :type parsed_args: any
:rtype: list[str] :rtype: list[str]
""" """
with open('test/runner/completion/network.txt', 'r') as completion_fd: images = read_lines_without_comments('test/runner/completion/network.txt', remove_blank_lines=True)
images = completion_fd.read().splitlines()
return [i for i in images if i.startswith(prefix) and (not parsed_args.platform or i not in parsed_args.platform)] return [i for i in images if i.startswith(prefix) and (not parsed_args.platform or i not in parsed_args.platform)]

View file

@ -18,6 +18,7 @@ from lib.util import (
parse_to_dict, parse_to_dict,
ABC, ABC,
is_binary_file, is_binary_file,
read_lines_without_comments,
) )
from lib.ansible_util import ( from lib.ansible_util import (
@ -134,8 +135,8 @@ def collect_code_smell_tests():
""" """
:rtype: tuple[SanityCodeSmellTest] :rtype: tuple[SanityCodeSmellTest]
""" """
with open('test/sanity/code-smell/skip.txt', 'r') as skip_fd: skip_file = 'test/sanity/code-smell/skip.txt'
skip_tests = skip_fd.read().splitlines() skip_tests = read_lines_without_comments(skip_file, remove_blank_lines=True)
paths = glob.glob('test/sanity/code-smell/*') paths = glob.glob('test/sanity/code-smell/*')
paths = sorted(p for p in paths if os.access(p, os.X_OK) and os.path.isfile(p) and os.path.basename(p) not in skip_tests) paths = sorted(p for p in paths if os.access(p, os.X_OK) and os.path.isfile(p) and os.path.basename(p) not in skip_tests)

View file

@ -17,6 +17,7 @@ from lib.util import (
SubprocessError, SubprocessError,
display, display,
intercept_command, intercept_command,
read_lines_without_comments,
) )
from lib.ansible_util import ( from lib.ansible_util import (
@ -37,8 +38,8 @@ class AnsibleDocTest(SanityMultipleVersion):
:type python_version: str :type python_version: str
:rtype: TestResult :rtype: TestResult
""" """
with open('test/sanity/ansible-doc/skip.txt', 'r') as skip_fd: skip_file = 'test/sanity/ansible-doc/skip.txt'
skip_modules = set(skip_fd.read().splitlines()) skip_modules = set(read_lines_without_comments(skip_file, remove_blank_lines=True))
plugin_type_blacklist = set([ plugin_type_blacklist = set([
# not supported by ansible-doc # not supported by ansible-doc

View file

@ -17,6 +17,7 @@ from lib.util import (
run_command, run_command,
display, display,
find_python, find_python,
read_lines_without_comments,
) )
from lib.config import ( from lib.config import (
@ -37,12 +38,10 @@ class CompileTest(SanityMultipleVersion):
:type python_version: str :type python_version: str
:rtype: TestResult :rtype: TestResult
""" """
# optional list of regex patterns to exclude from tests
skip_file = 'test/sanity/compile/python%s-skip.txt' % python_version skip_file = 'test/sanity/compile/python%s-skip.txt' % python_version
if os.path.exists(skip_file): if os.path.exists(skip_file):
with open(skip_file, 'r') as skip_fd: skip_paths = read_lines_without_comments(skip_file)
skip_paths = skip_fd.read().splitlines()
else: else:
skip_paths = [] skip_paths = []
@ -87,6 +86,9 @@ class CompileTest(SanityMultipleVersion):
for path in skip_paths: for path in skip_paths:
line += 1 line += 1
if not path:
continue
if not os.path.exists(path): if not os.path.exists(path):
# Keep files out of the list which no longer exist in the repo. # Keep files out of the list which no longer exist in the repo.
results.append(SanityMessage( results.append(SanityMessage(

View file

@ -19,6 +19,7 @@ from lib.util import (
remove_tree, remove_tree,
display, display,
find_python, find_python,
read_lines_without_comments,
) )
from lib.ansible_util import ( from lib.ansible_util import (
@ -43,9 +44,8 @@ class ImportTest(SanityMultipleVersion):
:type python_version: str :type python_version: str
:rtype: TestResult :rtype: TestResult
""" """
with open('test/sanity/import/skip.txt', 'r') as skip_fd: skip_file = 'test/sanity/import/skip.txt'
skip_paths = skip_fd.read().splitlines() skip_paths = read_lines_without_comments(skip_file, remove_blank_lines=True)
skip_paths_set = set(skip_paths) skip_paths_set = set(skip_paths)
paths = sorted( paths = sorted(
@ -121,7 +121,7 @@ class ImportTest(SanityMultipleVersion):
column=int(r['column']), column=int(r['column']),
) for r in results] ) for r in results]
results = [result for result in results if result.path not in skip_paths] results = [result for result in results if result.path not in skip_paths_set]
if results: if results:
return SanityFailure(self.name, messages=results, python_version=python_version) return SanityFailure(self.name, messages=results, python_version=python_version)

View file

@ -15,6 +15,7 @@ from lib.util import (
SubprocessError, SubprocessError,
display, display,
run_command, run_command,
read_lines_without_comments,
) )
from lib.config import ( from lib.config import (
@ -37,17 +38,14 @@ class Pep8Test(SanitySingleVersion):
:type targets: SanityTargets :type targets: SanityTargets
:rtype: TestResult :rtype: TestResult
""" """
with open(PEP8_SKIP_PATH, 'r') as skip_fd: skip_paths = read_lines_without_comments(PEP8_SKIP_PATH)
skip_paths = skip_fd.read().splitlines() legacy_paths = read_lines_without_comments(PEP8_LEGACY_PATH)
with open(PEP8_LEGACY_PATH, 'r') as legacy_fd: legacy_ignore_file = 'test/sanity/pep8/legacy-ignore.txt'
legacy_paths = legacy_fd.read().splitlines() legacy_ignore = set(read_lines_without_comments(legacy_ignore_file, remove_blank_lines=True))
with open('test/sanity/pep8/legacy-ignore.txt', 'r') as ignore_fd: current_ignore_file = 'test/sanity/pep8/current-ignore.txt'
legacy_ignore = set(ignore_fd.read().splitlines()) current_ignore = sorted(read_lines_without_comments(current_ignore_file, remove_blank_lines=True))
with open('test/sanity/pep8/current-ignore.txt', 'r') as ignore_fd:
current_ignore = sorted(ignore_fd.read().splitlines())
skip_paths_set = set(skip_paths) skip_paths_set = set(skip_paths)
legacy_paths_set = set(legacy_paths) legacy_paths_set = set(legacy_paths)
@ -106,6 +104,9 @@ class Pep8Test(SanitySingleVersion):
for path in legacy_paths: for path in legacy_paths:
line += 1 line += 1
if not path:
continue
if not os.path.exists(path): if not os.path.exists(path):
# Keep files out of the list which no longer exist in the repo. # Keep files out of the list which no longer exist in the repo.
errors.append(SanityMessage( errors.append(SanityMessage(
@ -133,6 +134,9 @@ class Pep8Test(SanitySingleVersion):
for path in skip_paths: for path in skip_paths:
line += 1 line += 1
if not path:
continue
if not os.path.exists(path): if not os.path.exists(path):
# Keep files out of the list which no longer exist in the repo. # Keep files out of the list which no longer exist in the repo.
errors.append(SanityMessage( errors.append(SanityMessage(

View file

@ -18,6 +18,7 @@ from lib.util import (
SubprocessError, SubprocessError,
run_command, run_command,
find_executable, find_executable,
read_lines_without_comments,
) )
from lib.config import ( from lib.config import (
@ -41,19 +42,20 @@ class PslintTest(SanitySingleVersion):
:type targets: SanityTargets :type targets: SanityTargets
:rtype: TestResult :rtype: TestResult
""" """
with open(PSLINT_SKIP_PATH, 'r') as skip_fd: skip_paths = read_lines_without_comments(PSLINT_SKIP_PATH)
skip_paths = skip_fd.read().splitlines()
invalid_ignores = [] invalid_ignores = []
with open(PSLINT_IGNORE_PATH, 'r') as ignore_fd: ignore_entries = read_lines_without_comments(PSLINT_IGNORE_PATH)
ignore_entries = ignore_fd.read().splitlines()
ignore = collections.defaultdict(dict) ignore = collections.defaultdict(dict)
line = 0 line = 0
for ignore_entry in ignore_entries: for ignore_entry in ignore_entries:
line += 1 line += 1
if not ignore_entry:
continue
if ' ' not in ignore_entry: if ' ' not in ignore_entry:
invalid_ignores.append((line, 'Invalid syntax')) invalid_ignores.append((line, 'Invalid syntax'))
continue continue
@ -138,6 +140,9 @@ class PslintTest(SanitySingleVersion):
for path in skip_paths: for path in skip_paths:
line += 1 line += 1
if not path:
continue
if not os.path.exists(path): if not os.path.exists(path):
# Keep files out of the list which no longer exist in the repo. # Keep files out of the list which no longer exist in the repo.
errors.append(SanityMessage( errors.append(SanityMessage(

View file

@ -24,6 +24,7 @@ from lib.util import (
run_command, run_command,
display, display,
find_executable, find_executable,
read_lines_without_comments,
) )
from lib.executor import ( from lib.executor import (
@ -69,22 +70,23 @@ class PylintTest(SanitySingleVersion):
display.warning('Skipping pylint on unsupported Python version %s.' % args.python_version) display.warning('Skipping pylint on unsupported Python version %s.' % args.python_version)
return SanitySkipped(self.name) return SanitySkipped(self.name)
with open(PYLINT_SKIP_PATH, 'r') as skip_fd: skip_paths = read_lines_without_comments(PYLINT_SKIP_PATH)
skip_paths = skip_fd.read().splitlines()
invalid_ignores = [] invalid_ignores = []
supported_versions = set(SUPPORTED_PYTHON_VERSIONS) - set(UNSUPPORTED_PYTHON_VERSIONS) supported_versions = set(SUPPORTED_PYTHON_VERSIONS) - set(UNSUPPORTED_PYTHON_VERSIONS)
supported_versions = set([v.split('.')[0] for v in supported_versions]) | supported_versions supported_versions = set([v.split('.')[0] for v in supported_versions]) | supported_versions
with open(PYLINT_IGNORE_PATH, 'r') as ignore_fd: ignore_entries = read_lines_without_comments(PYLINT_IGNORE_PATH)
ignore_entries = ignore_fd.read().splitlines()
ignore = collections.defaultdict(dict) ignore = collections.defaultdict(dict)
line = 0 line = 0
for ignore_entry in ignore_entries: for ignore_entry in ignore_entries:
line += 1 line += 1
if not ignore_entry:
continue
if ' ' not in ignore_entry: if ' ' not in ignore_entry:
invalid_ignores.append((line, 'Invalid syntax')) invalid_ignores.append((line, 'Invalid syntax'))
continue continue
@ -193,6 +195,9 @@ class PylintTest(SanitySingleVersion):
for path in skip_paths: for path in skip_paths:
line += 1 line += 1
if not path:
continue
if not os.path.exists(path): if not os.path.exists(path):
# Keep files out of the list which no longer exist in the repo. # Keep files out of the list which no longer exist in the repo.
errors.append(SanityMessage( errors.append(SanityMessage(

View file

@ -17,6 +17,7 @@ from lib.util import (
parse_to_dict, parse_to_dict,
display, display,
find_executable, find_executable,
read_lines_without_comments,
) )
from lib.config import ( from lib.config import (
@ -40,8 +41,8 @@ class RstcheckTest(SanitySingleVersion):
display.warning('Skipping rstcheck on unsupported Python version %s.' % args.python_version) display.warning('Skipping rstcheck on unsupported Python version %s.' % args.python_version)
return SanitySkipped(self.name) return SanitySkipped(self.name)
with open('test/sanity/rstcheck/ignore-substitutions.txt', 'r') as ignore_fd: ignore_file = 'test/sanity/rstcheck/ignore-substitutions.txt'
ignore_substitutions = sorted(set(ignore_fd.read().splitlines())) ignore_substitutions = sorted(set(read_lines_without_comments(ignore_file, remove_blank_lines=True)))
paths = sorted(i.path for i in targets.include if os.path.splitext(i.path)[1] in ('.rst',)) paths = sorted(i.path for i in targets.include if os.path.splitext(i.path)[1] in ('.rst',))

View file

@ -19,6 +19,7 @@ from lib.sanity import (
from lib.util import ( from lib.util import (
SubprocessError, SubprocessError,
run_command, run_command,
read_lines_without_comments,
) )
from lib.config import ( from lib.config import (
@ -34,11 +35,11 @@ class ShellcheckTest(SanitySingleVersion):
:type targets: SanityTargets :type targets: SanityTargets
:rtype: TestResult :rtype: TestResult
""" """
with open('test/sanity/shellcheck/skip.txt', 'r') as skip_fd: skip_file = 'test/sanity/shellcheck/skip.txt'
skip_paths = set(skip_fd.read().splitlines()) skip_paths = set(read_lines_without_comments(skip_file, remove_blank_lines=True))
with open('test/sanity/shellcheck/exclude.txt', 'r') as exclude_fd: exclude_file = 'test/sanity/shellcheck/exclude.txt'
exclude = set(exclude_fd.read().splitlines()) exclude = set(read_lines_without_comments(exclude_file, remove_blank_lines=True))
paths = sorted(i.path for i in targets.include if os.path.splitext(i.path)[1] == '.sh' and i.path not in skip_paths) paths = sorted(i.path for i in targets.include if os.path.splitext(i.path)[1] == '.sh' and i.path not in skip_paths)

View file

@ -17,6 +17,7 @@ from lib.util import (
SubprocessError, SubprocessError,
display, display,
run_command, run_command,
read_lines_without_comments,
) )
from lib.ansible_util import ( from lib.ansible_util import (
@ -44,9 +45,7 @@ class ValidateModulesTest(SanitySingleVersion):
:type targets: SanityTargets :type targets: SanityTargets
:rtype: TestResult :rtype: TestResult
""" """
with open(VALIDATE_SKIP_PATH, 'r') as skip_fd: skip_paths = read_lines_without_comments(VALIDATE_SKIP_PATH)
skip_paths = skip_fd.read().splitlines()
skip_paths_set = set(skip_paths) skip_paths_set = set(skip_paths)
env = ansible_environment(args, color=False) env = ansible_environment(args, color=False)
@ -65,14 +64,16 @@ class ValidateModulesTest(SanitySingleVersion):
invalid_ignores = [] invalid_ignores = []
with open(VALIDATE_IGNORE_PATH, 'r') as ignore_fd: ignore_entries = read_lines_without_comments(VALIDATE_IGNORE_PATH)
ignore_entries = ignore_fd.read().splitlines()
ignore = collections.defaultdict(dict) ignore = collections.defaultdict(dict)
line = 0 line = 0
for ignore_entry in ignore_entries: for ignore_entry in ignore_entries:
line += 1 line += 1
if not ignore_entry:
continue
if ' ' not in ignore_entry: if ' ' not in ignore_entry:
invalid_ignores.append((line, 'Invalid syntax')) invalid_ignores.append((line, 'Invalid syntax'))
continue continue
@ -139,9 +140,14 @@ class ValidateModulesTest(SanitySingleVersion):
confidence=calculate_confidence(VALIDATE_IGNORE_PATH, line, args.metadata) if args.metadata.changes else None, confidence=calculate_confidence(VALIDATE_IGNORE_PATH, line, args.metadata) if args.metadata.changes else None,
)) ))
line = 0
for path in skip_paths: for path in skip_paths:
line += 1 line += 1
if not path:
continue
if not os.path.exists(path): if not os.path.exists(path):
# Keep files out of the list which no longer exist in the repo. # Keep files out of the list which no longer exist in the repo.
errors.append(SanityMessage( errors.append(SanityMessage(

View file

@ -12,6 +12,7 @@ import sys
from lib.util import ( from lib.util import (
ApplicationError, ApplicationError,
read_lines_without_comments,
) )
MODULE_EXTENSIONS = '.py', '.ps1' MODULE_EXTENSIONS = '.py', '.ps1'
@ -511,8 +512,8 @@ class IntegrationTarget(CompletionTarget):
# static_aliases # static_aliases
try: try:
with open(os.path.join(path, 'aliases'), 'r') as aliases_file: aliases_path = os.path.join(path, 'aliases')
static_aliases = tuple(aliases_file.read().splitlines()) static_aliases = tuple(read_lines_without_comments(aliases_path, remove_blank_lines=True))
except IOError as ex: except IOError as ex:
if ex.errno != errno.ENOENT: if ex.errno != errno.ENOENT:
raise raise

View file

@ -42,8 +42,7 @@ def get_docker_completion():
:rtype: dict[str, str] :rtype: dict[str, str]
""" """
if not DOCKER_COMPLETION: if not DOCKER_COMPLETION:
with open('test/runner/completion/docker.txt', 'r') as completion_fd: images = read_lines_without_comments('test/runner/completion/docker.txt', remove_blank_lines=True)
images = completion_fd.read().splitlines()
DOCKER_COMPLETION.update(dict(kvp for kvp in [parse_docker_completion(i) for i in images] if kvp)) DOCKER_COMPLETION.update(dict(kvp for kvp in [parse_docker_completion(i) for i in images] if kvp))
@ -81,6 +80,23 @@ def remove_file(path):
os.remove(path) os.remove(path)
def read_lines_without_comments(path, remove_blank_lines=False):
"""
:type path: str
:type remove_blank_lines: bool
:rtype: list[str]
"""
with open(path, 'r') as path_fd:
lines = path_fd.read().splitlines()
lines = [re.sub(r' *#.*$', '', line) for line in lines]
if remove_blank_lines:
lines = [line for line in lines if line]
return lines
def find_executable(executable, cwd=None, path=None, required=True): def find_executable(executable, cwd=None, path=None, required=True):
""" """
:type executable: str :type executable: str