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

pantsbuild / pants / 18252174847

05 Oct 2025 01:36AM UTC coverage: 43.382% (-36.9%) from 80.261%
18252174847

push

github

web-flow
run tests on mac arm (#22717)

Just doing the minimal to pull forward the x86_64 pattern.

ref #20993

25776 of 59416 relevant lines covered (43.38%)

1.3 hits per line

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

63.27
/src/python/pants/backend/tools/trufflehog/rules_integration_test.py
1
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3
from __future__ import annotations
3✔
4

5
from typing import Any
3✔
6

7
import pytest
3✔
8

9
from pants.backend.tools.trufflehog.rules import TrufflehogRequest
3✔
10
from pants.backend.tools.trufflehog.rules import rules as trufflehog_rules
3✔
11
from pants.core.goals.fmt import Partitions
3✔
12
from pants.core.goals.lint import LintResult
3✔
13
from pants.core.util_rules import config_files, external_tool
3✔
14
from pants.engine.fs import PathGlobs
3✔
15
from pants.engine.internals.native_engine import Snapshot
3✔
16
from pants.testutil.rule_runner import QueryRule, RuleRunner
3✔
17

18

19
@pytest.fixture
3✔
20
def rule_runner() -> RuleRunner:
3✔
21
    return RuleRunner(
3✔
22
        rules=[
23
            *trufflehog_rules(),
24
            *config_files.rules(),
25
            *external_tool.rules(),
26
            QueryRule(Partitions, [TrufflehogRequest.PartitionRequest]),
27
            QueryRule(LintResult, [TrufflehogRequest.Batch]),
28
        ],
29
    )
30

31

32
PANTS_TOML = """[GLOBAL]\nbackend_packages = ["pants.backend.tools.trufflehog"]\n"""
3✔
33

34
# This configuration file specifies a detector that looks for custom regex patterns
35
TRUFFLEHOG_CONFIG = r"""
3✔
36
# config.yaml
37
detectors:
38
  - name: HogTokenDetector
39
    keywords:
40
      - hog
41
    regex:
42
      hogID: '\b(HOG[0-9A-Z]{17})\b'
43
      hogToken: '[^A-Za-z0-9+\/]{0,1}([A-Za-z0-9+\/]{40})[^A-Za-z0-9+\/]{0,1}'
44
    verify:
45
      - endpoint: http://localhost:8000/
46
        # unsafe must be set if the endpoint is HTTP
47
        unsafe: true
48
        headers:
49
          - "Authorization: super secret authorization header"
50
"""
51

52
# Example file contents with mock secrets in place for detection. These are not real secrets.
53
TRUFFLEHOG_PAYLOAD_WITH_SECRETS = """
3✔
54
{
55
    "HogTokenDetector": {
56
        "HogID": ["HOGAAIUNNWHAHJJWUQYR"],
57
        "HogSecret": ["sD9vzqdSsAOxntjAJ/qZ9sw+8PvEYg0r7D1Hhh0C"],
58
    }
59
}
60
"""
61

62
# The count of detectors loaded by the current version of Trufflehog.
63
# This may change in future versions, depending on whether or not new detectors are added.
64
TOTAL_DETECTORS = 738
3✔
65

66

67
def run_trufflehog(
3✔
68
    rule_runner: RuleRunner,
69
    *,
70
    extra_args: list[str] | None = None,
71
) -> LintResult:
72
    rule_runner.set_options(
3✔
73
        ["--backend-packages=pants.backend.tools.trufflehog", *(extra_args or ())],
74
    )
75
    snapshot = rule_runner.request(Snapshot, [PathGlobs(["**"])])
3✔
76
    partition = rule_runner.request(
3✔
77
        Partitions[Any], [TrufflehogRequest.PartitionRequest(snapshot.files)]
78
    )[0]
79
    fmt_result = rule_runner.request(
3✔
80
        LintResult,
81
        [
82
            TrufflehogRequest.Batch("", partition.elements, partition_metadata=partition.metadata),
83
        ],
84
    )
85
    return fmt_result
3✔
86

87

88
def extract_total_detector_count(input_string: str) -> int | None:
3✔
89
    """This function extracts the total number of detectors loaded by Trufflehog.
90

91
    Trufflehog prints to stderr in a format that can't be parsed as json.
92
    """
93
    # Find the index of the substring "total"
94
    total_index = input_string.find('"total":')
×
95
    if total_index == -1:
×
96
        return None  # "total" key not found
×
97

98
    # Extract the value after "total"
99
    total_value = ""
×
100
    for char in input_string[total_index + len('"total":') :]:
×
101
        if char.isdigit():
×
102
            total_value += char
×
103
        else:
104
            break
×
105

106
    return int(total_value) if total_value else None
×
107

108

109
def test_detectors_loaded(rule_runner: RuleRunner) -> None:
3✔
110
    rule_runner.write_files({"pants-enable-trufflehog.toml": PANTS_TOML})
×
111
    fmt_result = run_trufflehog(rule_runner)
×
112
    assert not fmt_result.stdout
×
113
    # Trufflehog prints details on how many active detectors are running to stderr
114
    assert "loaded detectors" in fmt_result.stderr
×
115
    # This number is expected to change with upgrades to trufflehog
116
    assert TOTAL_DETECTORS == extract_total_detector_count(fmt_result.stderr)
×
117
    rule_runner.write_files(
×
118
        {
119
            "pants-enable-trufflehog.toml.toml": PANTS_TOML,
120
            "trufflehog-config.yaml": TRUFFLEHOG_CONFIG,
121
        }
122
    )
123
    fmt_result = run_trufflehog(rule_runner)
×
124
    assert not fmt_result.stdout
×
125
    # Adding the config file has added one additional detector
126
    assert TOTAL_DETECTORS + 1 == extract_total_detector_count(fmt_result.stderr)
×
127

128

129
@pytest.mark.platform_specific_behavior
3✔
130
def test_secret_detected(rule_runner: RuleRunner) -> None:
3✔
131
    # Write the configuration file
132
    rule_runner.write_files(
3✔
133
        {
134
            "pants-enable-trufflehog.toml.toml": PANTS_TOML,
135
            "trufflehog-config.yaml": TRUFFLEHOG_CONFIG,
136
            "secret.json": TRUFFLEHOG_PAYLOAD_WITH_SECRETS,
137
        }
138
    )
139
    fmt_result = run_trufflehog(rule_runner)
3✔
140

141
    # Trufflehog returns exit code 183 upon finding secrets
142
    assert fmt_result.exit_code == 183
3✔
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

© 2026 Coveralls, Inc