Ejemplo n.º 1
0
def test_nested_alias(alias, definitions: dict | ContextManager) -> None:
    expect: ContextManager = no_exception() if isinstance(
        definitions, dict) else definitions
    with expect:
        cli_alias = CliAlias.from_dict(alias)
        if isinstance(definitions, dict):
            assert cli_alias.definitions == FrozenDict(definitions)
Ejemplo n.º 2
0
def test_validate_build_file_name(include: str, file_name: str,
                                  raises: bool) -> None:
    tailor_subsystem = create_goal_subsystem(TailorSubsystem,
                                             build_file_name=file_name)
    if raises:
        with pytest.raises(ValueError):
            tailor_subsystem.validate_build_file_name((include, ))
    else:
        with no_exception():
            tailor_subsystem.validate_build_file_name((include, ))
Ejemplo n.º 3
0
def test_get_context_root(context_root: str | None, default_context_root: str,
                          expected_context_root: str | ContextManager) -> None:
    if isinstance(expected_context_root, str):
        raises = cast("ContextManager", no_exception())
    else:
        raises = expected_context_root
        expected_context_root = ""

    with raises:
        docker_options = create_subsystem(
            DockerOptions,
            default_context_root=default_context_root,
        )
        address = Address("src/docker", target_name="image")
        tgt = DockerImageTarget({"context_root": context_root}, address)
        fs = DockerFieldSet.create(tgt)
        actual_context_root = fs.get_context_root(
            docker_options.default_context_root)
        if expected_context_root:
            assert actual_context_root == expected_context_root
Ejemplo n.º 4
0
            "tags": {
                "baseimage": "${base_version}",
                "stage0": "${base_version}",
            },
            "build_args": {
                # `base_name` is not listed here, as it was not an explicitly defined build arg.
                "base_version": "3.9",
            },
        },
    )


@pytest.mark.parametrize(
    "dockerfile_arg_value, extra_build_arg_value, expect",
    [
        pytest.param(None, None, no_exception(), id="No args defined"),
        pytest.param(
            None,
            "",
            pytest.raises(ExecutionError,
                          match=r"variable 'MY_ARG' is undefined"),
            id="No default value for build arg",
        ),
        pytest.param(None,
                     "some default value",
                     no_exception(),
                     id="Default value for build arg"),
        pytest.param("",
                     None,
                     no_exception(),
                     id="No build arg defined, and ARG without default"),
Ejemplo n.º 5
0
     ),
     None,
 ),
 (
     "v3.2.2",
     UnsupportedVersionUsage.RaiseError,
     pytest.raises(
         UnknownVersion,
         match=
         "No known version of foobar v3.2.2 for macos_x86_64 found in"),
     None,
 ),
 (
     "v3.4.7",
     UnsupportedVersionUsage.RaiseError,
     no_exception(),
     None,
 ),
 (
     "v3.8.0",
     UnsupportedVersionUsage.RaiseError,
     pytest.raises(
         UnsupportedVersion,
         match=re.escape(
             "The option [foobar].version is set to v3.8.0, which is not compatible with what this release of Pants expects: foobar<3.8,>3.2.1. "
             "Please update the version to a supported value, or consider using a different Pants release if you cannot change the version. "
             "Alternatively, update [foobar].use_unsupported_version to be 'warning'."
         ),
     ),
     None,
 ),
Ejemplo n.º 6
0
                """),
            "",
        ),
    ],
)
def test_parse_image_id_from_docker_build_output(expected: str, stdout: str,
                                                 stderr: str) -> None:
    assert expected == parse_image_id_from_docker_build_output(
        stdout.encode(), stderr.encode())


@pytest.mark.parametrize(
    "raw_values, expect_raises, image_refs",
    [
        (dict(name="lowercase"), no_exception(), ("lowercase:latest", )),
        (dict(name="CamelCase"), no_exception(), ("camelcase:latest", )),
        (dict(image_tags=["CamelCase"]), no_exception(),
         ("image:CamelCase", )),
        (dict(registries=["REG1.example.net"]), no_exception(),
         ("REG1.example.net/image:latest", )),
    ],
)
def test_image_ref_formatting(raw_values: dict, expect_raises: ContextManager,
                              image_refs: tuple[str, ...]) -> None:
    address = Address("test", target_name=raw_values.pop("name", "image"))
    tgt = DockerImageTarget(raw_values, address)
    field_set = DockerFieldSet.create(tgt)
    default_repository = "{name}"
    registries = DockerRegistries.from_dict({})
    interpolation_context = DockerInterpolationContext.from_dict({})
Ejemplo n.º 7
0
            "tags": {
                "baseimage": "${base_version}",
                "stage0": "${base_version}",
            },
            "build_args": {
                # `base_name` is not listed here, as it was not an explicitly defined build arg.
                "base_version": "3.9",
            },
        },
    )


@pytest.mark.parametrize(
    "dockerfile_arg_value, extra_build_arg_value, expect",
    [
        pytest.param(None, None, no_exception(), id="No args defined"),
        pytest.param(
            None,
            "",
            pytest.raises(ExecutionError, match=r"variable 'MY_ARG' is undefined"),
            id="No default value for build arg",
        ),
        pytest.param(None, "some default value", no_exception(), id="Default value for build arg"),
        pytest.param("", None, no_exception(), id="No build arg defined, and ARG without default"),
        pytest.param(
            "",
            "",
            pytest.raises(ExecutionError, match=r"variable 'MY_ARG' is undefined"),
            id="No default value from ARG",
        ),
        pytest.param(
Ejemplo n.º 8
0
def test_overrides_field_normalization() -> None:
    addr = Address("", target_name="example")

    assert OverridesField(None, addr).value is None
    assert OverridesField({}, addr).value == {}

    # Note that `list_field` is not hashable. We have to override `__hash__` for this to work.
    tgt1_override = {"str_field": "value", "list_field": [0, 1, 3]}
    tgt2_override = {"int_field": 0, "dict_field": {"a": 0}}

    # Convert a `str` key to `tuple[str, ...]`.
    field = OverridesField(
        {
            "tgt1": tgt1_override,
            ("tgt1", "tgt2"): tgt2_override
        }, addr)
    assert field.value == {
        ("tgt1", ): tgt1_override,
        ("tgt1", "tgt2"): tgt2_override
    }
    with no_exception():
        hash(field)

    path_field = OverridesField(
        {
            "foo.ext": tgt1_override,
            ("foo.ext", "bar*.ext"): tgt2_override
        }, Address("dir"))
    globs = OverridesField.to_path_globs(Address("dir"), path_field.flatten(),
                                         FilesNotFoundBehavior.error)
    assert [path_globs.globs for path_globs in globs] == [
        ("dir/foo.ext", ),
        ("dir/bar*.ext", ),
    ]
    assert OverridesField.flatten_paths(
        addr,
        [(paths, globs, overrides) for (paths, overrides), globs in zip(
            [
                (Paths(("dir/foo.ext", ), ()), tgt1_override),
                (Paths(("dir/bar1.ext", "dir/bar2.ext"), ()), tgt2_override),
            ],
            globs,
        )],
    ) == {
        "dir/foo.ext": tgt1_override,
        "dir/bar1.ext": tgt2_override,
        "dir/bar2.ext": tgt2_override,
    }
    assert path_field.flatten() == {
        "foo.ext": {
            **tgt1_override,
            **tgt2_override
        },
        "bar*.ext": tgt2_override,
    }
    with pytest.raises(InvalidFieldException):
        # Same field is overridden for the same file multiple times, which is an error.
        OverridesField.flatten_paths(
            addr,
            [
                (Paths(("dir/foo.ext", ), ()), PathGlobs([]), tgt1_override),
                (Paths(("dir/foo.ext", "dir/bar.ext"),
                       ()), PathGlobs([]), tgt1_override),
            ],
        )