• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 18517631058

15 Oct 2025 04:18AM UTC coverage: 69.207% (-11.1%) from 80.267%
18517631058

Pull #22745

github

web-flow
Merge 642a76ca1 into 99919310e
Pull Request #22745: [windows] Add windows support in the stdio crate.

53815 of 77759 relevant lines covered (69.21%)

2.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.0
/src/python/pants/backend/tools/trivy/testutil.py
1
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3
import json
1✔
4

5
from pants.core.goals.lint import LintResult
1✔
6

7
trivy_config = """
1✔
8
format: json
9
"""
10

11

12
def assert_trivy_output(
1✔
13
    result: LintResult,
14
    expected_exit_code: int,
15
    target: str,
16
    scanner_type: str,
17
    expected_error_count: int,
18
):
19
    """Assert that the output of Trivy matches our expectations.
20

21
    For checking the error count, we assert that there are _at least_ as many errors as specified,
22
    since new ones are found as time goes on and we don't want the tests to suddenly break
23
    """
24
    if result.exit_code != expected_exit_code:
1✔
25
        raise AssertionError(
×
26
            f"Trivy process had incorrect exit code, expected={expected_exit_code}, actual={result.exit_code}, stdout={result.stdout}, stderr={result.stderr}"
27
        )
28

29
    try:
1✔
30
        report = json.loads(result.stdout)
1✔
31
    except json.decoder.JSONDecodeError as e:
×
32
        raise AssertionError(
×
33
            f"Trivy output could not be parsed as JSON, stdout={result.stdout=}, stderr={result.stderr}"
34
        ) from e
35

36
    findings_by_target = {res["Target"]: res for res in report["Results"]}
1✔
37
    assert target in findings_by_target, (
1✔
38
        f"Did not find expected file in results, target={target} files={list(findings_by_target.keys())}"
39
    )
40

41
    if scanner_type == "config":
1✔
42
        found_count = findings_by_target[target]["MisconfSummary"]["Failures"]
1✔
43
        assert found_count >= expected_error_count, (
1✔
44
            f"Did not find expected failure count actual={found_count} expected={expected_error_count}"
45
        )
46
    elif scanner_type == "image":
×
47
        found_count = len(findings_by_target[target]["Vulnerabilities"])
×
48
        assert found_count >= expected_error_count, (
×
49
            f"Did not find expected vulnerabilities found={found_count} expected={expected_error_count}"
50
        )
51

52

53
def assert_trivy_success(result: LintResult):
1✔
54
    if result.exit_code != 0:
×
55
        raise AssertionError(f"Trivy process was not successful, stdout={result.stdout}")
×
56

57
    try:
×
58
        json.loads(result.stdout)
×
59
    except json.decoder.JSONDecodeError as e:
×
60
        raise AssertionError(
×
61
            f"Trivy output could not be parsed as JSON, stdout={result.stdout}, stderr={result.stderr}"
62
        ) from e
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc