Skip to content

Commit

Permalink
Docker: fix base image dependency inference for parametrized targets. (
Browse files Browse the repository at this point in the history
  • Loading branch information
kaos committed Mar 8, 2024
1 parent 908d245 commit b8b3c72
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,39 @@ def test_generate_lockfile_without_python_backend() -> None:
"--resolve=dockerfile-parser",
]
).assert_success()


def test_baseimage_dep_inference(rule_runner: RuleRunner) -> None:
# We use a single run to grab all information, rather than parametrizing the test to save on
# rule invocations.
base_image_tags = dict(
BASE_IMAGE_1=":sibling",
BASE_IMAGE_2=":sibling@a=42,b=c",
BASE_IMAGE_3="else/where:weird#name@with=param",
BASE_IMAGE_4="//src/common:name@parametrized=foo-bar.1",
BASE_IMAGE_5="should/allow/default-target-name",
)

rule_runner.write_files(
{
"test/BUILD": "docker_image()",
"test/Dockerfile": "\n".join(
dedent(
f"""\
ARG {arg}="{tag}"
FROM ${arg}
"""
)
for arg, tag in base_image_tags.items()
)
+ dedent(
"""\
ARG DECOY="this is not a target address"
FROM $DECOY
"""
),
}
)
addr = Address("test")
info = rule_runner.request(DockerfileInfo, [DockerfileInfoRequest(addr)])
assert info.from_image_build_args.to_dict() == base_image_tags
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,22 @@ class ParsedDockerfileInfo:

_address_regexp = re.compile(
r"""
(?://)?[^:# ]*:[^:#!@?/\= ]+(?:\#[^:#!@?= ]+)?$
# Optionally root:ed.
(?://)?
# Optional path.
[^:# ]*
# Optional target name.
(?::[^:#!@?/\= ]+)?
# Optional generated name.
(?:\#[^:#!@?= ]+)?
# Optional parametrizations.
(?:@
# key=value
[^=: ]+=[^,: ]*
# Optional additional `,key=value`s
(?:,[^=: ]+=[^,: ]*)*
)?
$
""",
re.VERBOSE,
)
Expand Down

0 comments on commit b8b3c72

Please sign in to comment.