2018-01-25 02:22:14 +01:00
|
|
|
"""Sanity test using PSScriptAnalyzer."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2018-01-25 02:22:14 +01:00
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .. import types as t
|
2019-07-26 08:29:23 +02:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..sanity import (
|
2019-07-27 01:46:52 +02:00
|
|
|
SanityVersionNeutral,
|
2018-01-25 02:22:14 +01:00
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
|
|
|
SanitySkipped,
|
2019-08-06 02:40:00 +02:00
|
|
|
SANITY_ROOT,
|
2018-01-25 02:22:14 +01:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..target import (
|
2019-07-30 20:10:55 +02:00
|
|
|
TestTarget,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util import (
|
2018-01-25 02:22:14 +01:00
|
|
|
SubprocessError,
|
|
|
|
find_executable,
|
2019-08-06 02:40:00 +02:00
|
|
|
ANSIBLE_TEST_DATA_ROOT,
|
2018-01-25 02:22:14 +01:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util_common import (
|
2019-07-11 07:00:34 +02:00
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..config import (
|
2018-01-25 02:22:14 +01:00
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..data import (
|
2019-07-23 04:24:48 +02:00
|
|
|
data_context,
|
|
|
|
)
|
|
|
|
|
2018-01-25 02:22:14 +01:00
|
|
|
|
2019-07-27 01:46:52 +02:00
|
|
|
class PslintTest(SanityVersionNeutral):
|
2018-01-25 02:22:14 +01:00
|
|
|
"""Sanity test using PSScriptAnalyzer."""
|
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 'AnsibleTest'
|
|
|
|
|
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] in ('.ps1', '.psm1', '.psd1')]
|
|
|
|
|
2018-01-25 02:22:14 +01:00
|
|
|
def test(self, args, targets):
|
|
|
|
"""
|
|
|
|
:type args: SanityConfig
|
|
|
|
:type targets: SanityTargets
|
2018-04-04 03:53:53 +02:00
|
|
|
:rtype: TestResult
|
2018-01-25 02:22:14 +01:00
|
|
|
"""
|
2019-07-26 08:29:23 +02:00
|
|
|
settings = self.load_processor(args)
|
2018-01-25 02:22:14 +01:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
paths = [target.path for target in targets.include]
|
2018-01-25 02:22:14 +01:00
|
|
|
|
|
|
|
if not find_executable('pwsh', required='warning'):
|
|
|
|
return SanitySkipped(self.name)
|
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
cmds = []
|
|
|
|
|
|
|
|
if args.requirements:
|
2019-08-06 02:40:00 +02:00
|
|
|
cmds.append([os.path.join(ANSIBLE_TEST_DATA_ROOT, 'requirements', 'sanity.ps1')])
|
2019-07-23 22:31:54 +02:00
|
|
|
|
2019-08-06 02:40:00 +02:00
|
|
|
cmds.append([os.path.join(SANITY_ROOT, 'pslint', 'pslint.ps1')] + paths)
|
2019-03-20 07:26:12 +01:00
|
|
|
|
2019-07-11 22:03:49 +02:00
|
|
|
stdout = ''
|
|
|
|
|
2019-03-20 07:26:12 +01:00
|
|
|
for cmd in cmds:
|
|
|
|
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:
|
|
|
|
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
|
2018-01-25 02:22:14 +01:00
|
|
|
|
|
|
|
if args.explain:
|
|
|
|
return SanitySuccess(self.name)
|
|
|
|
|
|
|
|
severity = [
|
|
|
|
'Information',
|
|
|
|
'Warning',
|
|
|
|
'Error',
|
2019-04-15 22:43:41 +02:00
|
|
|
'ParseError',
|
2018-01-25 02:22:14 +01:00
|
|
|
]
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
cwd = data_context().content.root + '/'
|
2018-01-25 02:22:14 +01:00
|
|
|
|
2019-04-15 22:43:41 +02:00
|
|
|
# replace unicode smart quotes and ellipsis with ascii versions
|
2018-01-25 02:22:14 +01:00
|
|
|
stdout = re.sub(u'[\u2018\u2019]', "'", stdout)
|
|
|
|
stdout = re.sub(u'[\u201c\u201d]', '"', stdout)
|
2019-04-15 22:43:41 +02:00
|
|
|
stdout = re.sub(u'[\u2026]', '...', stdout)
|
2018-01-25 02:22:14 +01:00
|
|
|
|
|
|
|
messages = json.loads(stdout)
|
|
|
|
|
|
|
|
errors = [SanityMessage(
|
|
|
|
code=m['RuleName'],
|
|
|
|
message=m['Message'],
|
|
|
|
path=m['ScriptPath'].replace(cwd, ''),
|
|
|
|
line=m['Line'] or 0,
|
|
|
|
column=m['Column'] or 0,
|
|
|
|
level=severity[m['Severity']],
|
|
|
|
) for m in messages]
|
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
errors = settings.process_errors(errors, paths)
|
2018-01-25 02:22:14 +01:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
return SanityFailure(self.name, messages=errors)
|
|
|
|
|
|
|
|
return SanitySuccess(self.name)
|