forked from python-poetry/poetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_build.py
154 lines (114 loc) · 4.58 KB
/
test_build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from __future__ import annotations
import shutil
import tarfile
from typing import TYPE_CHECKING
import pytest
from poetry.factory import Factory
if TYPE_CHECKING:
from pathlib import Path
from cleo.testers.command_tester import CommandTester
from poetry.poetry import Poetry
from poetry.utils.env import VirtualEnv
from tests.types import CommandTesterFactory
from tests.types import FixtureDirGetter
@pytest.fixture
def tmp_project_path(tmp_path: Path) -> Path:
return tmp_path / "project"
@pytest.fixture
def tmp_poetry(tmp_project_path: Path, fixture_dir: FixtureDirGetter) -> Poetry:
# copy project so that we start with a clean directory
shutil.copytree(fixture_dir("simple_project"), tmp_project_path)
poetry = Factory().create_poetry(tmp_project_path)
return poetry
@pytest.fixture
def tmp_tester(
tmp_poetry: Poetry, command_tester_factory: CommandTesterFactory
) -> CommandTester:
return command_tester_factory("build", tmp_poetry)
def get_package_glob(poetry: Poetry, local_version: str | None = None) -> str:
version = poetry.package.version
if local_version:
version = version.replace(local=local_version)
return f"{poetry.package.name.replace('-', '_')}-{version}*"
def test_build_format_is_not_valid(tmp_tester: CommandTester) -> None:
with pytest.raises(ValueError, match=r"Invalid format.*"):
tmp_tester.execute("--format not_valid")
@pytest.mark.parametrize("format", ["sdist", "wheel", "all"])
def test_build_creates_packages_in_dist_directory_if_no_output_is_specified(
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry, format: str
) -> None:
tmp_tester.execute(f"--format {format}")
build_artifacts = tuple(
(tmp_project_path / "dist").glob(get_package_glob(tmp_poetry))
)
assert len(build_artifacts) > 0
assert all(archive.exists() for archive in build_artifacts)
def test_build_with_local_version_label(
tmp_tester: CommandTester, tmp_project_path: Path, tmp_poetry: Poetry
) -> None:
local_version_label = "local-version"
tmp_tester.execute(f"--local-version {local_version_label}")
build_artifacts = tuple(
(tmp_project_path / "dist").glob(
get_package_glob(tmp_poetry, local_version=local_version_label)
)
)
assert len(build_artifacts) > 0
assert all(archive.exists() for archive in build_artifacts)
def test_build_not_possible_in_non_package_mode(
fixture_dir: FixtureDirGetter,
command_tester_factory: CommandTesterFactory,
) -> None:
source_dir = fixture_dir("non_package_mode")
poetry = Factory().create_poetry(source_dir)
tester = command_tester_factory("build", poetry)
assert tester.execute() == 1
assert (
tester.io.fetch_error()
== "Building a package is not possible in non-package mode.\n"
)
def test_build_with_multiple_readme_files(
fixture_dir: FixtureDirGetter,
tmp_path: Path,
tmp_venv: VirtualEnv,
command_tester_factory: CommandTesterFactory,
) -> None:
source_dir = fixture_dir("with_multiple_readme_files")
target_dir = tmp_path / "project"
shutil.copytree(str(source_dir), str(target_dir))
poetry = Factory().create_poetry(target_dir)
tester = command_tester_factory("build", poetry, environment=tmp_venv)
tester.execute()
build_dir = target_dir / "dist"
assert build_dir.exists()
sdist_file = build_dir / "my_package-0.1.tar.gz"
assert sdist_file.exists()
assert sdist_file.stat().st_size > 0
(wheel_file,) = build_dir.glob("my_package-0.1-*.whl")
assert wheel_file.exists()
assert wheel_file.stat().st_size > 0
with tarfile.open(sdist_file) as tf:
sdist_content = tf.getnames()
assert "my_package-0.1/README-1.rst" in sdist_content
assert "my_package-0.1/README-2.rst" in sdist_content
@pytest.mark.parametrize(
"output_dir", [None, "dist", "test/dir", "../dist", "absolute"]
)
def test_build_output_option(
tmp_tester: CommandTester,
tmp_project_path: Path,
tmp_poetry: Poetry,
output_dir: str,
) -> None:
if output_dir is None:
tmp_tester.execute()
build_dir = tmp_project_path / "dist"
elif output_dir == "absolute":
tmp_tester.execute(f"--output {tmp_project_path / 'tmp/dist'}")
build_dir = tmp_project_path / "tmp/dist"
else:
tmp_tester.execute(f"--output {output_dir}")
build_dir = tmp_project_path / output_dir
build_artifacts = tuple(build_dir.glob(get_package_glob(tmp_poetry)))
assert len(build_artifacts) > 0
assert all(archive.exists() for archive in build_artifacts)