Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude nested items (#784) #1464

Merged
2 changes: 2 additions & 0 deletions poetry/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def find_excluded_files(self): # type: () -> Set[str]

explicitely_excluded = set()
for excluded_glob in self._package.exclude:
if os.path.isdir(os.path.join(self._path.as_posix(), str(excluded_glob))):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract the argument from os.path.isdir here and put it in a variable inside this for-loop since we are also using the same argument for glob in line 92?

excluded_glob = Path(str(excluded_glob), "**/*")
for excluded in glob(
os.path.join(self._path.as_posix(), str(excluded_glob)), recursive=True
):
Expand Down
26 changes: 26 additions & 0 deletions tests/masonry/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,32 @@ def test_default_with_excluded_data(mocker):
assert "my-package-1.2.3/PKG-INFO" in names


def test_src_excluded_nested_data():
module_path = fixtures_dir / "default_with_excluded_data_toml"
poetry = Factory().create_poetry(module_path)
poetry._package.exclude = ["my_package/data"]

builder = SdistBuilder(poetry, NullEnv(), NullIO())
builder.build()

sdist = module_path / "dist" / "my-package-1.2.3.tar.gz"

assert sdist.exists()

with tarfile.open(str(sdist), "r") as tar:
names = tar.getnames()
assert len(names) == len(set(names))
assert "my-package-1.2.3/LICENSE" in names
assert "my-package-1.2.3/README.rst" in names
assert "my-package-1.2.3/my_package/__init__.py" in names
assert "my-package-1.2.3/pyproject.toml" in names
assert "my-package-1.2.3/setup.py" in names
assert "my-package-1.2.3/PKG-INFO" in names
assert "my-package-1.2.3/my_package/data/data1.txt" not in names
assert "my-package-1.2.3/my_package/data/sub_data/data2.txt" not in names
assert "my-package-1.2.3/my_package/data/sub_data/data3.txt" not in names


def test_proper_python_requires_if_two_digits_precision_version_specified():
poetry = Factory().create_poetry(project("simple_version"))

Expand Down
17 changes: 17 additions & 0 deletions tests/masonry/builders/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ def test_wheel_excluded_data():
assert "my_package/data/data1.txt" not in z.namelist()


def test_wheel_excluded_nested_data():
module_path = fixtures_dir / "default_with_excluded_data_toml"
poetry = Factory().create_poetry(module_path)
poetry._package.exclude = ["my_package/data"]
WheelBuilder.make(poetry, NullEnv(), NullIO())

whl = module_path / "dist" / "my_package-1.2.3-py3-none-any.whl"

assert whl.exists()

with zipfile.ZipFile(str(whl)) as z:
assert "my_package/__init__.py" in z.namelist()
assert "my_package/data/sub_data/data2.txt" not in z.namelist()
assert "my_package/data/sub_data/data3.txt" not in z.namelist()
assert "my_package/data/data1.txt" not in z.namelist()


def test_wheel_localversionlabel():
module_path = fixtures_dir / "localversionlabel"
WheelBuilder.make(Factory().create_poetry(module_path), NullEnv(), NullIO())
Expand Down