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

pantsbuild / pants / 20632486505

01 Jan 2026 04:21AM UTC coverage: 43.231% (-37.1%) from 80.281%
20632486505

Pull #22962

github

web-flow
Merge 08d5c63b0 into f52ab6675
Pull Request #22962: Bump the gha-deps group across 1 directory with 6 updates

26122 of 60424 relevant lines covered (43.23%)

0.86 hits per line

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

45.95
/src/python/pants/backend/go/util_rules/embedcfg.py
1
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3

4
from __future__ import annotations
2✔
5

6
import json
2✔
7
from collections.abc import Iterable, Mapping
2✔
8
from dataclasses import dataclass
2✔
9
from typing import Any
2✔
10

11
from pants.util.frozendict import FrozenDict
2✔
12
from pants.util.strutil import strip_prefix
2✔
13

14

15
@dataclass(frozen=True)
2✔
16
class EmbedConfig:
2✔
17
    patterns: FrozenDict[str, tuple[str, ...]]
2✔
18
    files: FrozenDict[str, str]
2✔
19

20
    def __init__(self, patterns: Mapping[str, Iterable[str]], files: Mapping[str, str]) -> None:
2✔
21
        """Configuration passed to the Go compiler to configure file embedding.
22

23
        The compiler relies entirely on the caller to map embed patterns to actual filesystem
24
        paths. All embed patterns
25
        contained in the package must be mapped. Consult
26
        `FirstPartyPkgAnalysis.embed_patterns` for the embed patterns obtained from analysis.
27

28
        :param patterns: Maps each pattern provided via a //go:embed directive to a list of file
29
          paths relative to the package directory for files to embed for that pattern. When the
30
          embedded variable is an `embed.FS`, those relative file paths define the virtual
31
          directory hierarchy exposed by the embed.FS filesystem abstraction. The relative file
32
          paths are resolved to actual filesystem paths for their content by consulting the
33
          `files` dictionary.
34
        :param files: Maps each virtual, relative file path used as a value in the `patterns`
35
          dictionary to the actual filesystem path with that file's content.
36
        """
37
        object.__setattr__(self, "patterns", FrozenDict({k: tuple(v) for k, v in patterns.items()}))
×
38
        object.__setattr__(self, "files", FrozenDict(files))
×
39

40
    @classmethod
2✔
41
    def from_json_dict(
2✔
42
        cls, d: dict[str, Any], prefix_to_strip: str | None = None
43
    ) -> EmbedConfig | None:
44
        patterns = d.get("Patterns", {})
×
45
        files = d.get("Files", {})
×
46
        if prefix_to_strip:
×
47
            files = {key: strip_prefix(value, prefix_to_strip) for key, value in files.items()}
×
48
        result = cls(
×
49
            patterns=FrozenDict({key: tuple(value) for key, value in patterns.items()}),
50
            files=FrozenDict(files),
51
        )
52
        return result if result else None
×
53

54
    def to_embedcfg(self) -> bytes:
2✔
55
        data = {
×
56
            "Patterns": dict(self.patterns),
57
            "Files": dict(self.files),
58
        }
59
        return json.dumps(data).encode("utf-8")
×
60

61
    def __bool__(self) -> bool:
2✔
62
        return bool(self.patterns) or bool(self.files)
×
63

64
    def merge(self, other: EmbedConfig) -> EmbedConfig:
2✔
65
        """Merge two EmbedConfig's into one.
66

67
        Overlapping keys must have the same values.
68
        """
69
        overlapping_patterns_keys = set(self.patterns.keys()) & set(other.patterns.keys())
×
70
        for key in overlapping_patterns_keys:
×
71
            if self.patterns[key] != other.patterns[key]:
×
72
                raise AssertionError(
×
73
                    "Unable to merge conflicting golang file embed configurations. This should not have occurred. "
74
                    "Please open an issue at https://github.com/pantsbuild/pants/issues/new/choose "
75
                    "with the following information: "
76
                    f"Patterns Key: {key}; Left: {self.patterns[key]}; Right: {other.patterns[key]} "
77
                )
78

79
        overlapping_files_keys = set(self.files.keys()) & set(other.files.keys())
×
80
        for key in overlapping_files_keys:
×
81
            if self.files[key] != other.files[key]:
×
82
                raise AssertionError(
×
83
                    "Unable to merge conflicting golang file embed configurations. This should not have occurred. "
84
                    "Please open an issue at https://github.com/pantsbuild/pants/issues/new/choose "
85
                    "with the following information: "
86
                    f"Files Key: {key}; Left: {self.patterns[key]}; Right: {other.patterns[key]} "
87
                )
88

89
        return EmbedConfig(
×
90
            patterns={**self.patterns, **other.patterns},
91
            files={**self.files, **other.files},
92
        )
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