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

pantsbuild / pants / 22285099215

22 Feb 2026 08:52PM UTC coverage: 75.854% (-17.1%) from 92.936%
22285099215

Pull #23121

github

web-flow
Merge c7299df9c into ba8359840
Pull Request #23121: fix issue with optional fields in dependency validator

28 of 29 new or added lines in 2 files covered. (96.55%)

11174 existing lines in 400 files now uncovered.

53694 of 70786 relevant lines covered (75.85%)

1.88 hits per line

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

0.0
/src/python/pants/backend/helm/dependency_inference/unittest.py
1
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

UNCOV
4
import logging
×
UNCOV
5
import os
×
UNCOV
6
from collections.abc import Sequence
×
UNCOV
7
from dataclasses import dataclass
×
UNCOV
8
from pathlib import PurePath
×
9

UNCOV
10
from pants.backend.helm.goals.tailor import _SNAPSHOT_FOLDER_NAME, _TESTS_FOLDER_NAME
×
UNCOV
11
from pants.backend.helm.target_types import AllHelmChartTargets, HelmUnitTestDependenciesField
×
UNCOV
12
from pants.core.target_types import AllAssetTargetsByPath
×
UNCOV
13
from pants.engine.addresses import Address
×
UNCOV
14
from pants.engine.internals.graph import determine_explicitly_provided_dependencies
×
UNCOV
15
from pants.engine.rules import collect_rules, implicitly, rule
×
UNCOV
16
from pants.engine.target import (
×
17
    DependenciesRequest,
18
    FieldSet,
19
    InferDependenciesRequest,
20
    InferredDependencies,
21
    Target,
22
)
UNCOV
23
from pants.engine.unions import UnionRule
×
UNCOV
24
from pants.util.ordered_set import OrderedSet
×
UNCOV
25
from pants.util.strutil import bullet_list, pluralize
×
26

UNCOV
27
logger = logging.getLogger(__name__)
×
28

29

UNCOV
30
class InvalidUnitTestTestFolder(Exception):
×
UNCOV
31
    def __init__(self, address: Address, found_folder: str) -> None:
×
32
        super().__init__(
×
33
            f"`helm_unittest_test` target at {address.spec_path} is at the wrong folder, "
34
            f"it should be inside a `tests` folder under the Helm chart root sources, it was found at: {found_folder}"
35
        )
36

37

UNCOV
38
class AmbiguousHelmUnitTestChart(Exception):
×
UNCOV
39
    def __init__(self, *, target_addr: str, putative_addresses: Sequence[str]) -> None:
×
40
        super().__init__(
×
41
            f"The actual Helm chart for the target at '{target_addr}' is ambiguous and can not be inferred. "
42
            f"Found {pluralize(len(putative_addresses), 'candidate')}:\n{bullet_list(putative_addresses)}"
43
        )
44

45

UNCOV
46
@dataclass(frozen=True)
×
UNCOV
47
class HelmUnitTestChartDependencyInferenceFieldSet(FieldSet):
×
UNCOV
48
    required_fields = (HelmUnitTestDependenciesField,)
×
49

50
    dependencies: HelmUnitTestDependenciesField
51

52

UNCOV
53
class InferHelmUnitTestChartDependencyRequest(InferDependenciesRequest):
×
UNCOV
54
    infer_from = HelmUnitTestChartDependencyInferenceFieldSet
×
55

56

UNCOV
57
@rule
×
UNCOV
58
async def infer_chart_dependency_into_unittests(
×
59
    request: InferHelmUnitTestChartDependencyRequest,
60
    all_helm_charts: AllHelmChartTargets,
61
    all_asset_targets: AllAssetTargetsByPath,
62
) -> InferredDependencies:
UNCOV
63
    unittest_target_addr: Address = request.field_set.address
×
64

UNCOV
65
    putative_chart_path, unittest_target_dir = os.path.split(unittest_target_addr.spec_path)
×
UNCOV
66
    if unittest_target_dir != _TESTS_FOLDER_NAME:
×
67
        raise InvalidUnitTestTestFolder(unittest_target_addr, unittest_target_addr.spec_path)
×
68

UNCOV
69
    explicitly_provided_deps = await determine_explicitly_provided_dependencies(
×
70
        **implicitly(DependenciesRequest(request.field_set.dependencies))
71
    )
72

UNCOV
73
    def is_snapshot_resource(path: PurePath) -> bool:
×
UNCOV
74
        if not path.parent:
×
75
            return False
×
UNCOV
76
        if path.parent.name != _SNAPSHOT_FOLDER_NAME:
×
UNCOV
77
            return False
×
UNCOV
78
        if not path.parent.parent:
×
79
            return False
×
UNCOV
80
        return str(path.parent.parent) == unittest_target_addr.spec_path
×
81

UNCOV
82
    candidate_snapshot_resources = {
×
83
        tgt.address
84
        for path, targets in all_asset_targets.resources.items()
85
        if is_snapshot_resource(path)
86
        for tgt in targets
87
    }
UNCOV
88
    found_snapshots = set()
×
89

UNCOV
90
    for candidate_snapshot in candidate_snapshot_resources:
×
UNCOV
91
        explicitly_provided_deps.maybe_warn_of_ambiguous_dependency_inference(
×
92
            (candidate_snapshot,),
93
            unittest_target_addr,
94
            import_reference="snapshot",
95
            context=f"The target {candidate_snapshot} is nested under {unittest_target_addr}",
96
        )
97

UNCOV
98
        maybe_disambiguated = explicitly_provided_deps.disambiguated((candidate_snapshot,))
×
UNCOV
99
        if maybe_disambiguated:
×
UNCOV
100
            found_snapshots.add(maybe_disambiguated)
×
101

UNCOV
102
    def is_parent_chart(target: Target) -> bool:
×
UNCOV
103
        chart_folder = target.address.spec_path
×
UNCOV
104
        return chart_folder == putative_chart_path
×
105

UNCOV
106
    candidate_charts: OrderedSet[Address] = OrderedSet(
×
107
        [tgt.address for tgt in all_helm_charts if is_parent_chart(tgt)]
108
    )
UNCOV
109
    found_charts = set()
×
110

UNCOV
111
    for candidate_chart in candidate_charts:
×
UNCOV
112
        explicitly_provided_deps.maybe_warn_of_ambiguous_dependency_inference(
×
113
            (candidate_chart,),
114
            unittest_target_addr,
115
            import_reference="chart",
116
            context=f"The target {unittest_target_addr} is nested under the chart {candidate_chart}",
117
        )
UNCOV
118
        maybe_disambiguated = explicitly_provided_deps.disambiguated((candidate_chart,))
×
UNCOV
119
        if maybe_disambiguated:
×
UNCOV
120
            found_charts.add(maybe_disambiguated)
×
121

UNCOV
122
    if len(found_charts) > 1:
×
123
        raise AmbiguousHelmUnitTestChart(
×
124
            target_addr=unittest_target_addr.spec,
125
            putative_addresses=[addr.spec for addr in found_charts],
126
        )
127

UNCOV
128
    if len(found_charts) == 1:
×
UNCOV
129
        found_dep = list(found_charts)[0]
×
UNCOV
130
        logger.debug(
×
131
            f"Found Helm chart at '{found_dep.spec}' for unittest at: {unittest_target_addr.spec}"
132
        )
133

UNCOV
134
    dependencies: OrderedSet[Address] = OrderedSet()
×
UNCOV
135
    dependencies.update(found_snapshots)
×
UNCOV
136
    dependencies.update(found_charts)
×
137

UNCOV
138
    return InferredDependencies(dependencies)
×
139

140

UNCOV
141
def rules():
×
UNCOV
142
    return [
×
143
        *collect_rules(),
144
        UnionRule(InferDependenciesRequest, InferHelmUnitTestChartDependencyRequest),
145
    ]
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