| 1 |
"""Sanity test using PSScriptAnalyzer.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import json |
| 6 |
import os |
| 7 |
import re |
| 8 |
import typing as t |
| 9 |
|
| 10 |
from . import ( |
| 11 |
SanityVersionNeutral, |
| 12 |
SanityMessage, |
| 13 |
SanityFailure, |
| 14 |
SanitySuccess, |
| 15 |
SanitySkipped, |
| 16 |
SanityTargets, |
| 17 |
SANITY_ROOT, |
| 18 |
) |
| 19 |
|
| 20 |
from ...test import ( |
| 21 |
TestResult, |
| 22 |
) |
| 23 |
|
| 24 |
from ...target import ( |
| 25 |
TestTarget, |
| 26 |
) |
| 27 |
|
| 28 |
from ...util import ( |
| 29 |
SubprocessError, |
| 30 |
find_executable, |
| 31 |
ANSIBLE_TEST_DATA_ROOT, |
| 32 |
common_environment, |
| 33 |
) |
| 34 |
|
| 35 |
from ...util_common import ( |
| 36 |
run_command, |
| 37 |
get_powershell_injector_env, |
| 38 |
) |
| 39 |
|
| 40 |
from ...config import ( |
| 41 |
SanityConfig, |
| 42 |
) |
| 43 |
|
| 44 |
from ...data import ( |
| 45 |
data_context, |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
class PslintTest(SanityVersionNeutral): |
| 50 |
"""Sanity test using PSScriptAnalyzer.""" |
| 51 |
|
| 52 |
@property |
| 53 |
def error_code(self) -> t.Optional[str]: |
| 54 |
"""Error code for ansible-test matching the format used by the underlying test program, or None if the program does not use error codes.""" |
| 55 |
return 'AnsibleTest' |
| 56 |
|
| 57 |
def filter_targets(self, targets: list[TestTarget]) -> list[TestTarget]: |
| 58 |
"""Return the given list of test targets, filtered to include only those relevant for the test.""" |
| 59 |
return [target for target in targets if os.path.splitext(target.path)[1] in ('.ps1', '.psm1', '.psd1')] |
| 60 |
|
| 61 |
def test(self, args: SanityConfig, targets: SanityTargets) -> TestResult: |
| 62 |
env = common_environment() |
| 63 |
env.update(get_powershell_injector_env(args.controller_powershell, env)) |
| 64 |
|
| 65 |
settings = self.load_processor(args) |
| 66 |
|
| 67 |
paths = [target.path for target in targets.include] |
| 68 |
|
| 69 |
if not find_executable('pwsh', required='warning', path=env.get('PATH')): |
| 70 |
return SanitySkipped(self.name) |
| 71 |
|
| 72 |
cmds = [] |
| 73 |
|
| 74 |
if args.controller.is_managed or args.requirements: |
| 75 |
cmds.append(['pwsh', os.path.join(ANSIBLE_TEST_DATA_ROOT, 'requirements', 'sanity.pslint.ps1')]) |
| 76 |
|
| 77 |
cmds.append(['pwsh', os.path.join(SANITY_ROOT, 'pslint', 'pslint.ps1')] + paths) |
| 78 |
|
| 79 |
stdout = '' |
| 80 |
|
| 81 |
for cmd in cmds: |
| 82 |
try: |
| 83 |
stdout, stderr = run_command(args, cmd, env=env, capture=True) |
| 84 |
status = 0 |
| 85 |
except SubprocessError as ex: |
| 86 |
stdout = ex.stdout |
| 87 |
stderr = ex.stderr |
| 88 |
status = ex.status |
| 89 |
|
| 90 |
if stderr: |
| 91 |
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout) |
| 92 |
|
| 93 |
if args.explain: |
| 94 |
return SanitySuccess(self.name) |
| 95 |
|
| 96 |
severity = [ |
| 97 |
'Information', |
| 98 |
'Warning', |
| 99 |
'Error', |
| 100 |
'ParseError', |
| 101 |
] |
| 102 |
|
| 103 |
cwd = data_context().content.root + '/' |
| 104 |
|
| 105 |
# replace unicode smart quotes and ellipsis with ascii versions |
| 106 |
stdout = re.sub('[\u2018\u2019]', "'", stdout) |
| 107 |
stdout = re.sub('[\u201c\u201d]', '"', stdout) |
| 108 |
stdout = re.sub('[\u2026]', '...', stdout) |
| 109 |
|
| 110 |
messages = json.loads(stdout) |
| 111 |
|
| 112 |
errors = [SanityMessage( |
| 113 |
code=m['RuleName'], |
| 114 |
message=m['Message'], |
| 115 |
path=m['ScriptPath'].replace(cwd, ''), |
| 116 |
line=m['Line'] or 0, |
| 117 |
column=m['Column'] or 0, |
| 118 |
level=severity[m['Severity']], |
| 119 |
) for m in messages] |
| 120 |
|
| 121 |
errors = settings.process_errors(errors, paths) |
| 122 |
|
| 123 |
if errors: |
| 124 |
return SanityFailure(self.name, messages=errors) |
| 125 |
|
| 126 |
return SanitySuccess(self.name) |