Skip to content

Commit

Permalink
fixup! improve assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed Dec 19, 2024
1 parent a6a6013 commit ae8af77
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import json
from textwrap import dedent

import pytest
Expand All @@ -10,7 +9,11 @@
from pants.backend.docker.rules import rules as docker_rules
from pants.backend.docker.target_types import DockerImageTarget
from pants.backend.tools.trivy.rules import rules as trivy_rules
from pants.backend.tools.trivy.testutil import assert_trivy_output, trivy_config, assert_trivy_success
from pants.backend.tools.trivy.testutil import (
assert_trivy_output,
assert_trivy_success,
trivy_config,
)
from pants.core.goals import package
from pants.core.goals.lint import LintResult
from pants.core.util_rules import source_files
Expand Down
24 changes: 15 additions & 9 deletions src/python/pants/backend/tools/trivy/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@


def assert_trivy_output(
result: LintResult, expected_exit_code: int, target: str, scanner_type: str, expected_error_count: int
result: LintResult,
expected_exit_code: int,
target: str,
scanner_type: str,
expected_error_count: int,
):
if result.exit_code != expected_exit_code:
raise AssertionError(f"Trivy process had incorrect exit code, expected={expected_exit_code}, actual={result.exit_code}, stdout={result.stdout}, stderr={result.stderr}")
raise AssertionError(
f"Trivy process had incorrect exit code, expected={expected_exit_code}, actual={result.exit_code}, stdout={result.stdout}, stderr={result.stderr}"
)

try:
report = json.loads(result.stdout)
except json.decoder.JSONDecodeError as e:
raise AssertionError(f"Trivy output could not be parsed as JSON, stdout={result.stdout=}, stderr={result.stderr}") from e
raise AssertionError(
f"Trivy output could not be parsed as JSON, stdout={result.stdout=}, stderr={result.stderr}"
) from e

findings_by_target = {res["Target"]: res for res in report["Results"]}
assert (
Expand All @@ -37,15 +45,13 @@ def assert_trivy_output(
), f"Did not find expected vulnerabilities found={found_count} expected={expected_error_count}"




def assert_trivy_success(result: LintResult):
if result.exit_code != 0:
raise AssertionError(
f"Trivy process was not successful, stdout={result.stdout}"
)
raise AssertionError(f"Trivy process was not successful, stdout={result.stdout}")

try:
json.loads(result.stdout)
except json.decoder.JSONDecodeError as e:
raise AssertionError(f"Trivy output could not be parsed as JSON, stdout={result.stdout}, stderr={result.stderr}") from e
raise AssertionError(
f"Trivy output could not be parsed as JSON, stdout={result.stdout}, stderr={result.stderr}"
) from e

0 comments on commit ae8af77

Please sign in to comment.