コード例 #1
0
ファイル: test_sdist.py プロジェクト: Car111/poetry-core
def test_find_packages():
    poetry = Factory().create_poetry(project("complete"))

    builder = SdistBuilder(poetry)

    base = project("complete")
    include = PackageInclude(base, "my_package")

    pkg_dir, packages, pkg_data = builder.find_packages(include)

    assert pkg_dir is None
    assert packages == [
        "my_package",
        "my_package.sub_pkg1",
        "my_package.sub_pkg2",
        "my_package.sub_pkg3",
    ]
    assert pkg_data == {
        "": ["*"],
        "my_package": ["data1/*"],
        "my_package.sub_pkg2": ["data2/*"],
    }

    poetry = Factory().create_poetry(project("source_package"))

    builder = SdistBuilder(poetry)

    base = project("source_package")
    include = PackageInclude(base, "package_src", source="src")

    pkg_dir, packages, pkg_data = builder.find_packages(include)

    assert pkg_dir == str(base / "src")
    assert packages == ["package_src"]
    assert pkg_data == {"": ["*"]}
コード例 #2
0
def test_package_include_with_nested_dir():
    pkg_include = PackageInclude(base=with_includes,
                                 include="extra_package/**/*.py")
    assert pkg_include.elements == [
        with_includes / "extra_package/some_dir/foo.py",
        with_includes / "extra_package/some_dir/quux.py",
    ]
コード例 #3
0
def test_package_include_with_non_existent_directory() -> None:
    with pytest.raises(ValueError) as e:
        PackageInclude(base=with_includes, include="not_a_dir")

    err_str = str(with_includes / "not_a_dir") + " does not contain any element"

    assert str(e.value) == err_str
コード例 #4
0
def test_pep_561_stub_only_package_good_name_suffix():
    pkg_include = PackageInclude(base=fixtures_dir / "pep_561_stub_only",
                                 include="good-stubs")
    assert pkg_include.elements == [
        fixtures_dir / "pep_561_stub_only/good-stubs/__init__.pyi",
        fixtures_dir / "pep_561_stub_only/good-stubs/module.pyi",
    ]
コード例 #5
0
def test_pep_561_stub_only_partial_namespace_package_good_name_suffix() -> None:
    pkg_include = PackageInclude(
        base=fixtures_dir / "pep_561_stub_only_partial_namespace", include="good-stubs"
    )
    assert pkg_include.elements == [
        fixtures_dir / "pep_561_stub_only_partial_namespace/good-stubs/module.pyi",
        fixtures_dir / "pep_561_stub_only_partial_namespace/good-stubs/subpkg/",
        fixtures_dir
        / "pep_561_stub_only_partial_namespace/good-stubs/subpkg/__init__.pyi",
        fixtures_dir / "pep_561_stub_only_partial_namespace/good-stubs/subpkg/py.typed",
    ]
コード例 #6
0
def test_package_include_with_multiple_dirs():
    pkg_include = PackageInclude(base=fixtures_dir, include="with_includes")
    assert pkg_include.elements == [
        with_includes / "__init__.py",
        with_includes / "bar",
        with_includes / "bar/baz.py",
        with_includes / "extra_package",
        with_includes / "extra_package/some_dir",
        with_includes / "extra_package/some_dir/foo.py",
        with_includes / "extra_package/some_dir/quux.py",
        with_includes / "not_a_python_pkg",
        with_includes / "not_a_python_pkg/baz.txt",
    ]
コード例 #7
0
def test_package_include_with_no_python_files_in_dir():
    with pytest.raises(ValueError) as e:
        PackageInclude(base=with_includes, include="not_a_python_pkg")

    assert str(e.value) == "not_a_python_pkg is not a package."
コード例 #8
0
def test_package_include_with_simple_dir():
    pkg_include = PackageInclude(base=with_includes, include="bar")
    assert pkg_include.elements == [with_includes / "bar/baz.py"]
コード例 #9
0
ファイル: module.py プロジェクト: python-poetry/poetry-core
    def __init__(
        self,
        name: str,
        directory: str = ".",
        packages: list[dict[str, Any]] | None = None,
        includes: list[dict[str, Any]] | None = None,
    ) -> None:
        from poetry.core.masonry.utils.include import Include
        from poetry.core.masonry.utils.package_include import PackageInclude
        from poetry.core.utils.helpers import module_name

        self._name = module_name(name)
        self._in_src = False
        self._is_package = False
        self._path = Path(directory)
        self._includes: list[Include] = []
        packages = packages or []
        includes = includes or []

        if not packages:
            # It must exist either as a .py file or a directory, but not both
            pkg_dir = Path(directory, self._name)
            py_file = Path(directory, self._name + ".py")
            if pkg_dir.is_dir() and py_file.is_file():
                raise ValueError(f"Both {pkg_dir} and {py_file} exist")
            elif pkg_dir.is_dir():
                packages = [{"include": str(pkg_dir.relative_to(self._path))}]
            elif py_file.is_file():
                packages = [{"include": str(py_file.relative_to(self._path))}]
            else:
                # Searching for a src module
                src = Path(directory, "src")
                src_pkg_dir = src / self._name
                src_py_file = src / (self._name + ".py")

                if src_pkg_dir.is_dir() and src_py_file.is_file():
                    raise ValueError(f"Both {pkg_dir} and {py_file} exist")
                elif src_pkg_dir.is_dir():
                    packages = [{
                        "include": str(src_pkg_dir.relative_to(src)),
                        "from": str(src.relative_to(self._path)),
                    }]
                elif src_py_file.is_file():
                    packages = [{
                        "include": str(src_py_file.relative_to(src)),
                        "from": str(src.relative_to(self._path)),
                    }]
                else:
                    raise ModuleOrPackageNotFound(
                        f"No file/folder found for package {name}")

        for package in packages:
            formats = package.get("format")
            if formats and not isinstance(formats, list):
                formats = [formats]

            self._includes.append(
                PackageInclude(
                    self._path,
                    package["include"],
                    formats=formats,
                    source=package.get("from"),
                ))

        for include in includes:
            self._includes.append(
                Include(self._path, include["path"],
                        formats=include["format"]))
コード例 #10
0
def test_pep_561_stub_only_package_bad_name_suffix() -> None:
    with pytest.raises(ValueError) as e:
        PackageInclude(base=fixtures_dir / "pep_561_stub_only", include="bad")

    assert str(e.value) == "bad is not a package."