def assert_git(extra_opts: PyprojectAttr, suffix: str) -> None:
     attr = PyprojectAttr(
         {"git": "https://github.com/requests/requests.git"})
     attr.update(extra_opts)
     assert (
         handle_dict_attr("requests", attr, empty_pyproject_toml) ==
         f"requests @ git+https://github.com/requests/requests.git{suffix}")
def test_handle_url_arg(empty_pyproject_toml: PyProjectToml) -> None:
    attr = PyprojectAttr({"url": "https://my-site.com/mydep.whl"})
    assert (handle_dict_attr(
        "my_py_proj", attr,
        empty_pyproject_toml) == "my_py_proj @ https://my-site.com/mydep.whl")

    attr_with_extra = PyprojectAttr({"extras": ["extra1"]})
    attr_with_extra.update(attr)
    assert (handle_dict_attr("my_py_proj", attr_with_extra,
                             empty_pyproject_toml) ==
            "my_py_proj[extra1] @ https://my-site.com/mydep.whl")

    attr_with_mark = PyprojectAttr({"markers": "os_name=='darwin'"})
    attr_with_mark.update(attr)
    assert (handle_dict_attr("my_py_proj", attr_with_mark,
                             empty_pyproject_toml) ==
            "my_py_proj @ https://my-site.com/mydep.whl;(os_name=='darwin')")
def test_handle_path_arg(tmp_path: Path) -> None:
    build_root = tmp_path / "build_root"

    one_level = Path("one")
    one_pyproject_toml = create_pyproject_toml(build_root=build_root,
                                               toml_relpath=one_level /
                                               "pyproject.toml")

    two_level = one_level / "two"
    two_pyproject_toml = create_pyproject_toml(build_root=build_root,
                                               toml_relpath=two_level /
                                               "pyproject.toml")

    (build_root / two_level).mkdir(parents=True)

    external_file = tmp_path / "my_py_proj.whl"
    external_file.touch()

    external_project = tmp_path / "my_py_proj"
    external_project.mkdir()

    internal_file = build_root / "my_py_proj.whl"
    internal_file.touch()

    internal_project = build_root / "my_py_proj"
    internal_project.mkdir()

    file_attr = PyprojectAttr({"path": "../../my_py_proj.whl"})
    file_attr_mark = PyprojectAttr({
        "path": "../../my_py_proj.whl",
        "markers": "os_name=='darwin'"
    })
    file_attr_extras = PyprojectAttr({
        "path": "../../my_py_proj.whl",
        "extras": ["extra1"]
    })
    dir_attr = PyprojectAttr({"path": "../../my_py_proj"})

    assert (handle_dict_attr(
        "my_py_proj", file_attr,
        one_pyproject_toml) == f"my_py_proj @ file://{external_file}")

    assert (handle_dict_attr(
        "my_py_proj", file_attr_extras,
        one_pyproject_toml) == f"my_py_proj[extra1] @ file://{external_file}")

    assert (handle_dict_attr("my_py_proj", file_attr_mark, one_pyproject_toml)
            == f"my_py_proj @ file://{external_file};(os_name=='darwin')")

    assert (handle_dict_attr(
        "my_py_proj", file_attr,
        two_pyproject_toml) == f"my_py_proj @ file://{internal_file}")

    assert (handle_dict_attr(
        "my_py_proj", dir_attr,
        one_pyproject_toml) == f"my_py_proj @ file://{external_project}")

    assert handle_dict_attr("my_py_proj", dir_attr, two_pyproject_toml) is None
def test_handle_extras(empty_pyproject_toml: PyProjectToml) -> None:
    # The case where we have both extras and path/url are tested in
    # test_handle_path/url respectively.
    attr = PyprojectAttr({"version": "1.0.0", "extras": ["extra1"]})
    assert handle_dict_attr("requests", attr,
                            empty_pyproject_toml) == "requests[extra1] ==1.0.0"

    attr_git = PyprojectAttr({
        "git": "https://github.com/requests/requests.git",
        "extras": ["extra1"]
    })
    assert (handle_dict_attr("requests", attr_git, empty_pyproject_toml) ==
            "requests[extra1] @ git+https://github.com/requests/requests.git")

    assert handle_dict_attr("requests", attr,
                            empty_pyproject_toml) == "requests[extra1] ==1.0.0"
    attr_multi = PyprojectAttr({
        "version": "1.0.0",
        "extras": ["extra1", "extra2", "extra3"]
    })
    assert (handle_dict_attr(
        "requests", attr_multi,
        empty_pyproject_toml) == "requests[extra1,extra2,extra3] ==1.0.0")
 def assert_py_constraints(py_req: str, suffix: str) -> None:
     attr = PyprojectAttr({"version": "1.2.3", "python": py_req})
     assert handle_dict_attr(
         "foo", attr, empty_pyproject_toml) == f"foo ==1.2.3;{suffix}"
def test_version_only(empty_pyproject_toml: PyProjectToml) -> None:
    attr = PyprojectAttr({"version": "1.2.3"})
    assert handle_dict_attr("foo", attr, empty_pyproject_toml) == "foo ==1.2.3"
def test_handle_git_ssh(empty_pyproject_toml: PyProjectToml) -> None:
    attr = PyprojectAttr({"git": "[email protected]:requests/requests.git"})
    assert (handle_dict_attr("requests", attr, empty_pyproject_toml) ==
            "requests @ git+ssh://[email protected]/requests/requests.git")