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_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 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_git(empty_pyproject_toml: PyProjectToml) -> None:
    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}")

    assert_git({}, "")
    assert_git(PyprojectAttr({"branch": "main"}), "@main")
    assert_git(PyprojectAttr({"tag": "v1.1.1"}), "@v1.1.1")
    assert_git(PyprojectAttr({"rev": "1a2b3c4d"}), "#1a2b3c4d")
    assert_git(
        PyprojectAttr({
            "branch": "main",
            "markers": "platform_python_implementation == 'CPython'",
            "python": "3.6",
        }),
        "@main;(platform_python_implementation == 'CPython') and (python_version == '3.6')",
    )
Example #6
0
def test_add_markers() -> None:

    attr_mark = PyprojectAttr(
        {"markers": "platform_python_implementation == 'CPython'"})
    assert (add_markers("foo==1.0.0", attr_mark, "somepath") ==
            "foo==1.0.0;(platform_python_implementation == 'CPython')")

    attr_mark_adv = PyprojectAttr({
        "markers":
        "platform_python_implementation == 'CPython' or sys_platform == 'win32'"
    })
    assert (
        add_markers("foo==1.0.0", attr_mark_adv, "somepath") ==
        "foo==1.0.0;(platform_python_implementation == 'CPython' or sys_platform == 'win32')"
    )
    attr_basic_both = PyprojectAttr({"python": "3.6"})
    attr_basic_both.update(attr_mark)

    assert (
        add_markers("foo==1.0.0", attr_basic_both, "somepath") ==
        "foo==1.0.0;(platform_python_implementation == 'CPython') and (python_version == '3.6')"
    )
    attr_adv_py_both = PyprojectAttr({
        "python":
        "^3.6",
        "markers":
        "platform_python_implementation == 'CPython'"
    })
    assert add_markers("foo==1.0.0", attr_adv_py_both,
                       "somepath") == softwrap("""
        foo==1.0.0;(platform_python_implementation == 'CPython') and
        (python_version >= '3.6' and python_version< '4.0')
        """)

    attr_adv_both = PyprojectAttr({
        "python":
        "^3.6",
        "markers":
        "platform_python_implementation == 'CPython' or sys_platform == 'win32'",
    })
    assert add_markers("foo==1.0.0", attr_adv_both, "somepath") == softwrap("""
        foo==1.0.0;(platform_python_implementation == 'CPython' or
        sys_platform == 'win32') and (python_version >= '3.6' and python_version< '4.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")