Skip to content

Commit c76988b

Browse files
branchvabn
authored andcommitted
fix: replace custom temporary_directory
1 parent 8d3e67c commit c76988b

File tree

3 files changed

+39
-48
lines changed

3 files changed

+39
-48
lines changed

src/poetry_plugin_bundle/bundlers/venv_bundler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def set_remove(self, remove: bool = True) -> VenvBundler:
4444

4545
def bundle(self, poetry: Poetry, io: IO) -> bool:
4646
from pathlib import Path
47+
from tempfile import TemporaryDirectory
4748

4849
from cleo.io.null_io import NullIO
4950
from poetry.core.masonry.builders.wheel import WheelBuilder
@@ -55,7 +56,6 @@ def bundle(self, poetry: Poetry, io: IO) -> bool:
5556
from poetry.utils.env import EnvManager
5657
from poetry.utils.env import SystemEnv
5758
from poetry.utils.env import VirtualEnv
58-
from poetry.utils.helpers import temporary_directory
5959

6060
warnings = []
6161

@@ -145,7 +145,7 @@ def bundle(self, poetry: Poetry, io: IO) -> bool:
145145

146146
# Build a wheel of the project in a temporary directory
147147
# and install it in the newly create virtual environment
148-
with temporary_directory() as directory:
148+
with TemporaryDirectory() as directory:
149149
try:
150150
wheel_name = WheelBuilder.make_in(poetry, directory=Path(directory))
151151
wheel = Path(directory).joinpath(wheel_name)

tests/bundlers/test_venv_bundler.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,68 +52,68 @@ def poetry(config: Config) -> Poetry:
5252

5353

5454
def test_bundler_should_build_a_new_venv_with_existing_python(
55-
io: BufferedIO, tmp_dir: str, poetry: Poetry, mocker: MockerFixture
55+
io: BufferedIO, tmpdir: str, poetry: Poetry, mocker: MockerFixture
5656
):
57-
shutil.rmtree(tmp_dir)
57+
shutil.rmtree(tmpdir)
5858
mocker.patch("poetry.installation.executor.Executor._execute_operation")
5959

6060
bundler = VenvBundler()
61-
bundler.set_path(Path(tmp_dir))
61+
bundler.set_path(Path(tmpdir))
6262

6363
assert bundler.bundle(poetry, io)
6464

6565
python_version = ".".join(str(v) for v in sys.version_info[:3])
6666
expected = f"""\
67-
• Bundling simple-project (1.2.3) into {tmp_dir}
68-
• Bundling simple-project (1.2.3) into {tmp_dir}: Creating a virtual environment using Python {python_version}
69-
• Bundling simple-project (1.2.3) into {tmp_dir}: Installing dependencies
70-
• Bundling simple-project (1.2.3) into {tmp_dir}: Installing simple-project (1.2.3)
71-
• Bundled simple-project (1.2.3) into {tmp_dir}
67+
• Bundling simple-project (1.2.3) into {tmpdir}
68+
• Bundling simple-project (1.2.3) into {tmpdir}: Creating a virtual environment using Python {python_version}
69+
• Bundling simple-project (1.2.3) into {tmpdir}: Installing dependencies
70+
• Bundling simple-project (1.2.3) into {tmpdir}: Installing simple-project (1.2.3)
71+
• Bundled simple-project (1.2.3) into {tmpdir}
7272
""" # noqa: E501
7373
assert expected == io.fetch_output()
7474

7575

7676
def test_bundler_should_build_a_new_venv_with_given_executable(
77-
io: BufferedIO, tmp_dir: str, poetry: Poetry, mocker: MockerFixture
77+
io: BufferedIO, tmpdir: str, poetry: Poetry, mocker: MockerFixture
7878
):
79-
shutil.rmtree(tmp_dir)
79+
shutil.rmtree(tmpdir)
8080
mocker.patch("poetry.installation.executor.Executor._execute_operation")
8181

8282
bundler = VenvBundler()
83-
bundler.set_path(Path(tmp_dir))
83+
bundler.set_path(Path(tmpdir))
8484
bundler.set_executable(sys.executable)
8585

8686
assert bundler.bundle(poetry, io)
8787

8888
python_version = ".".join(str(v) for v in sys.version_info[:3])
8989
expected = f"""\
90-
• Bundling simple-project (1.2.3) into {tmp_dir}
91-
• Bundling simple-project (1.2.3) into {tmp_dir}: Creating a virtual environment using Python {python_version}
92-
• Bundling simple-project (1.2.3) into {tmp_dir}: Installing dependencies
93-
• Bundling simple-project (1.2.3) into {tmp_dir}: Installing simple-project (1.2.3)
94-
• Bundled simple-project (1.2.3) into {tmp_dir}
90+
• Bundling simple-project (1.2.3) into {tmpdir}
91+
• Bundling simple-project (1.2.3) into {tmpdir}: Creating a virtual environment using Python {python_version}
92+
• Bundling simple-project (1.2.3) into {tmpdir}: Installing dependencies
93+
• Bundling simple-project (1.2.3) into {tmpdir}: Installing simple-project (1.2.3)
94+
• Bundled simple-project (1.2.3) into {tmpdir}
9595
""" # noqa: E501
9696
assert expected == io.fetch_output()
9797

9898

9999
def test_bundler_should_build_a_new_venv_if_existing_venv_is_incompatible(
100-
io: BufferedIO, tmp_dir: str, poetry: Poetry, mocker: MockerFixture
100+
io: BufferedIO, tmpdir: str, poetry: Poetry, mocker: MockerFixture
101101
):
102102
mocker.patch("poetry.installation.executor.Executor._execute_operation")
103103

104104
bundler = VenvBundler()
105-
bundler.set_path(Path(tmp_dir))
105+
bundler.set_path(Path(tmpdir))
106106

107107
assert bundler.bundle(poetry, io)
108108

109109
python_version = ".".join(str(v) for v in sys.version_info[:3])
110110
expected = f"""\
111-
• Bundling simple-project (1.2.3) into {tmp_dir}
112-
• Bundling simple-project (1.2.3) into {tmp_dir}: Removing existing virtual environment
113-
• Bundling simple-project (1.2.3) into {tmp_dir}: Creating a virtual environment using Python {python_version}
114-
• Bundling simple-project (1.2.3) into {tmp_dir}: Installing dependencies
115-
• Bundling simple-project (1.2.3) into {tmp_dir}: Installing simple-project (1.2.3)
116-
• Bundled simple-project (1.2.3) into {tmp_dir}
111+
• Bundling simple-project (1.2.3) into {tmpdir}
112+
• Bundling simple-project (1.2.3) into {tmpdir}: Removing existing virtual environment
113+
• Bundling simple-project (1.2.3) into {tmpdir}: Creating a virtual environment using Python {python_version}
114+
• Bundling simple-project (1.2.3) into {tmpdir}: Installing dependencies
115+
• Bundling simple-project (1.2.3) into {tmpdir}: Installing simple-project (1.2.3)
116+
• Bundled simple-project (1.2.3) into {tmpdir}
117117
""" # noqa: E501
118118
assert expected == io.fetch_output()
119119

@@ -164,25 +164,25 @@ def test_bundler_should_remove_an_existing_venv_if_forced(
164164

165165

166166
def test_bundler_should_fail_when_installation_fails(
167-
io: BufferedIO, tmp_dir: str, poetry: Poetry, mocker: MockerFixture
167+
io: BufferedIO, tmpdir: str, poetry: Poetry, mocker: MockerFixture
168168
):
169169
mocker.patch(
170170
"poetry.installation.executor.Executor._do_execute_operation",
171171
side_effect=Exception(),
172172
)
173173

174174
bundler = VenvBundler()
175-
bundler.set_path(Path(tmp_dir))
175+
bundler.set_path(Path(tmpdir))
176176

177177
assert not bundler.bundle(poetry, io)
178178

179179
python_version = ".".join(str(v) for v in sys.version_info[:3])
180180
expected = f"""\
181-
• Bundling simple-project (1.2.3) into {tmp_dir}
182-
• Bundling simple-project (1.2.3) into {tmp_dir}: Removing existing virtual environment
183-
• Bundling simple-project (1.2.3) into {tmp_dir}: Creating a virtual environment using Python {python_version}
184-
• Bundling simple-project (1.2.3) into {tmp_dir}: Installing dependencies
185-
• Bundling simple-project (1.2.3) into {tmp_dir}: Failed at step Installing dependencies
181+
• Bundling simple-project (1.2.3) into {tmpdir}
182+
• Bundling simple-project (1.2.3) into {tmpdir}: Removing existing virtual environment
183+
• Bundling simple-project (1.2.3) into {tmpdir}: Creating a virtual environment using Python {python_version}
184+
• Bundling simple-project (1.2.3) into {tmpdir}: Installing dependencies
185+
• Bundling simple-project (1.2.3) into {tmpdir}: Failed at step Installing dependencies
186186
""" # noqa: E501
187187
assert expected == io.fetch_output()
188188

tests/conftest.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from __future__ import annotations
22

33
import shutil
4-
import tempfile
54

6-
from pathlib import Path
75
from typing import TYPE_CHECKING
86
from typing import Iterator
97

@@ -16,12 +14,14 @@
1614

1715

1816
if TYPE_CHECKING:
17+
from pathlib import Path
18+
1919
from pytest_mock import MockerFixture
2020

2121

2222
@pytest.fixture
23-
def config_cache_dir(tmp_dir: str) -> Path:
24-
path = Path(tmp_dir) / ".cache" / "pypoetry"
23+
def config_cache_dir(tmp_path: Path) -> Path:
24+
path = tmp_path / ".cache" / "pypoetry"
2525
path.mkdir(parents=True)
2626
return path
2727

@@ -70,17 +70,8 @@ def config(
7070

7171

7272
@pytest.fixture
73-
def tmp_dir() -> Iterator[str]:
74-
dir_ = tempfile.mkdtemp(prefix="poetry_plugin_bundle_")
75-
76-
yield dir_
77-
78-
shutil.rmtree(dir_)
79-
80-
81-
@pytest.fixture
82-
def tmp_venv(tmp_dir: str) -> Iterator[VirtualEnv]:
83-
venv_path = Path(tmp_dir) / "venv"
73+
def tmp_venv(tmp_path: Path) -> Iterator[VirtualEnv]:
74+
venv_path = tmp_path / "venv"
8475

8576
EnvManager.build_venv(str(venv_path))
8677

0 commit comments

Comments
 (0)