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

pantsbuild / pants / 20333307239

18 Dec 2025 10:07AM UTC coverage: 75.452% (-4.8%) from 80.295%
20333307239

Pull #22949

github

web-flow
Merge b07232683 into 407284c67
Pull Request #22949: Add experimental uv resolver for Python lockfiles

51 of 96 new or added lines in 5 files covered. (53.13%)

2857 existing lines in 120 files now uncovered.

66315 of 87890 relevant lines covered (75.45%)

2.78 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

UNCOV
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:
63
    unittest_target_addr: Address = request.field_set.address
×
64

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

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

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

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
    }
88
    found_snapshots = set()
×
89

90
    for candidate_snapshot in candidate_snapshot_resources:
×
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

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

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

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

111
    for candidate_chart in candidate_charts:
×
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
        )
118
        maybe_disambiguated = explicitly_provided_deps.disambiguated((candidate_chart,))
×
119
        if maybe_disambiguated:
×
120
            found_charts.add(maybe_disambiguated)
×
121

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

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

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

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

© 2025 Coveralls, Inc