Skip to content

Commit

Permalink
Ignore dev dependencies for PEP 517 builds
Browse files Browse the repository at this point in the history
This change allows for development dependencies to be ignored creating
`Poetry` instances. This is used when PEP 517 artifacts are built as 
they are not required in this scenario.

Relates-to: python-poetry/poetry#2174
  • Loading branch information
roxchkplusony authored Oct 22, 2020
1 parent fa52260 commit 727907d
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 5 deletions.
6 changes: 4 additions & 2 deletions poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class Factory(object):
Factory class to create various elements needed by Poetry.
"""

def create_poetry(self, cwd=None): # type: (Optional[Path]) -> Poetry
def create_poetry(
self, cwd=None, with_dev=True
): # type: (Optional[Path]. bool) -> Poetry
poetry_file = self.locate(cwd)
local_config = PyProjectTOML(path=poetry_file).poetry_config

Expand Down Expand Up @@ -91,7 +93,7 @@ def create_poetry(self, cwd=None): # type: (Optional[Path]) -> Poetry
self.create_dependency(name, constraint, root_dir=package.root_dir)
)

if "dev-dependencies" in local_config:
if with_dev and "dev-dependencies" in local_config:
for name, constraint in local_config["dev-dependencies"].items():
if isinstance(constraint, list):
for _constraint in constraint:
Expand Down
6 changes: 3 additions & 3 deletions poetry/core/masonry/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_requires_for_build_wheel(config_settings=None):


def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):
poetry = Factory().create_poetry(Path(".").resolve())
poetry = Factory().create_poetry(Path(".").resolve(), with_dev=False)
builder = WheelBuilder(poetry)

dist_info = Path(metadata_directory, builder.dist_info)
Expand All @@ -52,14 +52,14 @@ def prepare_metadata_for_build_wheel(metadata_directory, config_settings=None):

def build_wheel(wheel_directory, config_settings=None, metadata_directory=None):
"""Builds a wheel, places it in wheel_directory"""
poetry = Factory().create_poetry(Path(".").resolve())
poetry = Factory().create_poetry(Path(".").resolve(), with_dev=False)

return unicode(WheelBuilder.make_in(poetry, Path(wheel_directory)))


def build_sdist(sdist_directory, config_settings=None):
"""Builds an sdist, places it in sdist_directory"""
poetry = Factory().create_poetry(Path(".").resolve())
poetry = Factory().create_poetry(Path(".").resolve(), with_dev=False)

path = SdistBuilder(poetry).build(Path(sdist_directory))

Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/project_with_invalid_dev_deps/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = ["Awesome Hacker <[email protected]>"]
license = "MIT"

[tool.poetry.dependencies]

[tool.poetry.extras]

[tool.poetry.dev-dependencies]
mylib = { path = "../mylib", develop = true}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.poetry]
name = "with_bad_path_dep"
version = "1.2.3"
description = "Some description."
authors = ["Awesome Hacker <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.6"
bogus = { path = "../only/in/dev", develop = true }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tool.poetry]
name = "with_bad_path_dev_dep"
version = "1.2.3"
description = "Some description."
authors = ["Awesome Hacker <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]
bogus = { path = "../only/in/dev", develop = true }
Empty file.
45 changes: 45 additions & 0 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ def test_build_wheel_with_include():
)


def test_build_wheel_with_bad_path_dev_dep_succeeds():
with temporary_directory() as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dev_dep")
):
api.build_wheel(tmp_dir)


def test_build_wheel_with_bad_path_dep_fails():
with pytest.raises(ValueError) as err, temporary_directory() as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dep")
):
api.build_wheel(tmp_dir)
assert "does not exist" in str(err.value)


@pytest.mark.skipif(
sys.platform == "win32"
and sys.version_info <= (3, 6)
Expand Down Expand Up @@ -101,6 +116,21 @@ def test_build_sdist_with_include():
)


def test_build_sdist_with_bad_path_dev_dep_succeeds():
with temporary_directory() as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dev_dep")
):
api.build_sdist(tmp_dir)


def test_build_sdist_with_bad_path_dep_fails():
with pytest.raises(ValueError) as err, temporary_directory() as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dep")
):
api.build_sdist(tmp_dir)
assert "does not exist" in str(err.value)


def test_prepare_metadata_for_build_wheel():
entry_points = """\
[console_scripts]
Expand Down Expand Up @@ -170,3 +200,18 @@ def test_prepare_metadata_for_build_wheel():

with (dist_info / "METADATA").open(encoding="utf-8") as f:
assert metadata == decode(f.read())


def test_prepare_metadata_for_build_wheel_with_bad_path_dev_dep_succeeds():
with temporary_directory() as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dev_dep")
):
api.prepare_metadata_for_build_wheel(tmp_dir)


def test_prepare_metadata_for_build_wheel_with_bad_path_dep_succeeds():
with pytest.raises(ValueError) as err, temporary_directory() as tmp_dir, cwd(
os.path.join(fixtures, "with_bad_path_dep")
):
api.prepare_metadata_for_build_wheel(tmp_dir)
assert "does not exist" in str(err.value)
18 changes: 18 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,21 @@ def test_create_poetry_fails_on_invalid_configuration():
- 'description' is a required property
"""
assert expected == str(e.value)


def test_create_poetry_omits_dev_dependencies_iff_with_dev_is_false():
poetry = Factory().create_poetry(fixtures_dir / "sample_project", with_dev=False)
assert not any(r for r in poetry.package.dev_requires if "pytest" in str(r))

poetry = Factory().create_poetry(fixtures_dir / "sample_project")
assert any(r for r in poetry.package.dev_requires if "pytest" in str(r))


def test_create_poetry_fails_with_invalid_dev_dependencies_iff_with_dev_is_true():
with pytest.raises(ValueError) as err:
Factory().create_poetry(fixtures_dir / "project_with_invalid_dev_deps")
assert "does not exist" in str(err.value)

Factory().create_poetry(
fixtures_dir / "project_with_invalid_dev_deps", with_dev=False
)

0 comments on commit 727907d

Please sign in to comment.