2017-08-19 02:21:11 +02:00
|
|
|
"""Sanity test using shellcheck."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from xml.etree.ElementTree import (
|
|
|
|
fromstring,
|
|
|
|
Element,
|
|
|
|
)
|
|
|
|
|
2021-05-05 23:29:30 +02:00
|
|
|
from ... import types as t
|
2019-07-26 08:29:23 +02:00
|
|
|
|
2021-05-05 23:29:30 +02:00
|
|
|
from . import (
|
2019-07-27 01:46:52 +02:00
|
|
|
SanityVersionNeutral,
|
2017-08-19 02:21:11 +02:00
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
|
|
|
SanitySkipped,
|
2019-08-06 02:40:00 +02:00
|
|
|
SANITY_ROOT,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2021-05-05 23:29:30 +02:00
|
|
|
from ...target import (
|
2019-07-30 20:10:55 +02:00
|
|
|
TestTarget,
|
|
|
|
)
|
|
|
|
|
2021-05-05 23:29:30 +02:00
|
|
|
from ...util import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SubprocessError,
|
2018-09-20 08:20:27 +02:00
|
|
|
read_lines_without_comments,
|
2019-07-30 20:10:55 +02:00
|
|
|
find_executable,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2021-05-05 23:29:30 +02:00
|
|
|
from ...util_common import (
|
2019-07-11 07:00:34 +02:00
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2021-05-05 23:29:30 +02:00
|
|
|
from ...config import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-07-27 01:46:52 +02:00
|
|
|
class ShellcheckTest(SanityVersionNeutral):
|
2017-08-19 02:21:11 +02:00
|
|
|
"""Sanity test using shellcheck."""
|
2019-07-26 16:53:53 +02:00
|
|
|
@property
|
2019-07-26 08:29:23 +02:00
|
|
|
def error_code(self): # type: () -> t.Optional[str]
|
|
|
|
"""Error code for ansible-test matching the format used by the underlying test program, or None if the program does not use error codes."""
|
|
|
|
return 'AT1000'
|
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
def filter_targets(self, targets): # type: (t.List[TestTarget]) -> t.List[TestTarget]
|
|
|
|
"""Return the given list of test targets, filtered to include only those relevant for the test."""
|
|
|
|
return [target for target in targets if os.path.splitext(target.path)[1] == '.sh']
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
def test(self, args, targets):
|
|
|
|
"""
|
|
|
|
:type args: SanityConfig
|
|
|
|
:type targets: SanityTargets
|
2018-04-04 03:53:53 +02:00
|
|
|
:rtype: TestResult
|
2017-08-19 02:21:11 +02:00
|
|
|
"""
|
2019-08-06 02:40:00 +02:00
|
|
|
exclude_file = os.path.join(SANITY_ROOT, 'shellcheck', 'exclude.txt')
|
2019-07-10 02:31:04 +02:00
|
|
|
exclude = set(read_lines_without_comments(exclude_file, remove_blank_lines=True, optional=True))
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-26 08:29:23 +02:00
|
|
|
settings = self.load_processor(args)
|
2019-07-23 22:31:54 +02:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
paths = [target.path for target in targets.include]
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
if not find_executable('shellcheck', required='warning'):
|
2017-08-19 02:21:11 +02:00
|
|
|
return SanitySkipped(self.name)
|
|
|
|
|
|
|
|
cmd = [
|
|
|
|
'shellcheck',
|
|
|
|
'-e', ','.join(sorted(exclude)),
|
|
|
|
'--format', 'checkstyle',
|
|
|
|
] + paths
|
|
|
|
|
|
|
|
try:
|
|
|
|
stdout, stderr = run_command(args, cmd, capture=True)
|
|
|
|
status = 0
|
|
|
|
except SubprocessError as ex:
|
|
|
|
stdout = ex.stdout
|
|
|
|
stderr = ex.stderr
|
|
|
|
status = ex.status
|
|
|
|
|
|
|
|
if stderr or status > 1:
|
|
|
|
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
|
|
|
|
|
|
|
|
if args.explain:
|
|
|
|
return SanitySuccess(self.name)
|
|
|
|
|
|
|
|
# json output is missing file paths in older versions of shellcheck, so we'll use xml instead
|
|
|
|
root = fromstring(stdout) # type: Element
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
for item in root: # type: Element
|
|
|
|
for entry in item: # type: Element
|
|
|
|
results.append(SanityMessage(
|
|
|
|
message=entry.attrib['message'],
|
|
|
|
path=item.attrib['name'],
|
|
|
|
line=int(entry.attrib['line']),
|
|
|
|
column=int(entry.attrib['column']),
|
|
|
|
level=entry.attrib['severity'],
|
|
|
|
code=entry.attrib['source'].replace('ShellCheck.', ''),
|
|
|
|
))
|
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
results = settings.process_errors(results, paths)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
if results:
|
|
|
|
return SanityFailure(self.name, messages=results)
|
|
|
|
|
|
|
|
return SanitySuccess(self.name)
|