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-07-26 08:29:23 +02:00
|
|
|
import lib.types as t
|
|
|
|
|
2018-01-25 02:22:14 +01:00
|
|
|
from lib.sanity import (
|
2019-07-27 01:46:52 +02:00
|
|
|
SanityVersionNeutral,
|
2018-01-25 02:22:14 +01:00
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
|
|
|
SanitySkipped,
|
|
|
|
)
|
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
from lib.target import (
|
|
|
|
TestTarget,
|
|
|
|
)
|
|
|
|
|
2018-01-25 02:22:14 +01:00
|
|
|
from lib.util import (
|
|
|
|
SubprocessError,
|
|
|
|
find_executable,
|
|
|
|
)
|
|
|
|
|
2019-07-11 07:00:34 +02:00
|
|
|
from lib.util_common import (
|
|
|
|
run_command,
|
2019-07-23 04:24:48 +02:00
|
|
|
ANSIBLE_ROOT,
|
2019-07-11 07:00:34 +02:00
|
|
|
)
|
|
|
|
|
2018-01-25 02:22:14 +01:00
|
|
|
from lib.config import (
|
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
from lib.data import (
|
|
|
|
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:
|
|
|
|
cmds.append([os.path.join(ANSIBLE_ROOT, 'test/runner/requirements/sanity.ps1')])
|
|
|
|
|
|
|
|
cmds.append([os.path.join(ANSIBLE_ROOT, 'test/sanity/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)
|