-
-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathtest_pep751_provider.py
More file actions
190 lines (140 loc) · 4.89 KB
/
test_pep751_provider.py
File metadata and controls
190 lines (140 loc) · 4.89 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from __future__ import annotations
from textwrap import dedent
from typing import TYPE_CHECKING
import pytest
from commitizen.providers import get_provider
from commitizen.providers.pep751_provider import Pep751Provider
if TYPE_CHECKING:
from pathlib import Path
from commitizen.config.base_config import BaseConfig
PYPROJECT_TOML = """\
[project]
name = "my-package"
version = "0.1.0"
"""
PYPROJECT_TOML_EXPECTED = """\
[project]
name = "my-package"
version = "42.1"
"""
PYLOCK_TOML_WITH_DIRECTORY = """\
lock-version = "1.0"
created-by = "test"
[[packages]]
name = "my-package"
version = "0.1.0"
[packages.directory]
path = "."
editable = true
[[packages]]
name = "some-dep"
version = "1.2.3"
"""
PYLOCK_TOML_WITH_DIRECTORY_EXPECTED = """\
lock-version = "1.0"
created-by = "test"
[[packages]]
name = "my-package"
version = "42.1"
[packages.directory]
path = "."
editable = true
[[packages]]
name = "some-dep"
version = "1.2.3"
"""
PYLOCK_TOML_NON_MATCHING_NAME = """\
lock-version = "1.0"
created-by = "test"
[[packages]]
name = "other-package"
version = "0.1.0"
[packages.directory]
path = "."
editable = true
"""
PYLOCK_TOML_NON_DIRECTORY = """\
lock-version = "1.0"
created-by = "test"
[[packages]]
name = "my-package"
version = "0.1.0"
"""
@pytest.fixture
def pyproject(chdir: Path) -> Path:
file = chdir / "pyproject.toml"
file.write_text(dedent(PYPROJECT_TOML))
return file
def test_get_version(config: BaseConfig, pyproject: Path):
config.settings["version_provider"] = "pep751"
provider = get_provider(config)
assert isinstance(provider, Pep751Provider)
assert provider.get_version() == "0.1.0"
def test_set_version_without_lock_files(
config: BaseConfig, pyproject: Path, chdir: Path
):
config.settings["version_provider"] = "pep751"
provider = get_provider(config)
provider.set_version("42.1")
assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED)
# No pylock*.toml files should exist
assert list(chdir.glob("pylock*.toml")) == []
def test_set_version_with_pylock_toml(config: BaseConfig, pyproject: Path, chdir: Path):
lock_file = chdir / "pylock.toml"
lock_file.write_text(dedent(PYLOCK_TOML_WITH_DIRECTORY))
config.settings["version_provider"] = "pep751"
provider = get_provider(config)
provider.set_version("42.1")
assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED)
assert lock_file.read_text() == dedent(PYLOCK_TOML_WITH_DIRECTORY_EXPECTED)
def test_set_version_with_named_lock_file(
config: BaseConfig, pyproject: Path, chdir: Path
):
lock_file = chdir / "pylock.dev.toml"
lock_file.write_text(dedent(PYLOCK_TOML_WITH_DIRECTORY))
config.settings["version_provider"] = "pep751"
provider = get_provider(config)
provider.set_version("42.1")
assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED)
assert lock_file.read_text() == dedent(PYLOCK_TOML_WITH_DIRECTORY_EXPECTED)
def test_set_version_non_matching_package_not_updated(
config: BaseConfig, pyproject: Path, chdir: Path
):
lock_file = chdir / "pylock.toml"
lock_file.write_text(dedent(PYLOCK_TOML_NON_MATCHING_NAME))
original_lock_content = lock_file.read_text()
config.settings["version_provider"] = "pep751"
provider = get_provider(config)
provider.set_version("42.1")
assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED)
# Lock file should be unchanged since no matching package name
assert lock_file.read_text() == original_lock_content
def test_set_version_non_directory_source_not_updated(
config: BaseConfig, pyproject: Path, chdir: Path
):
lock_file = chdir / "pylock.toml"
lock_file.write_text(dedent(PYLOCK_TOML_NON_DIRECTORY))
original_lock_content = lock_file.read_text()
config.settings["version_provider"] = "pep751"
provider = get_provider(config)
provider.set_version("42.1")
assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED)
# Lock file should be unchanged since package has no directory source
assert lock_file.read_text() == original_lock_content
def test_set_version_multiple_lock_files(
config: BaseConfig, pyproject: Path, chdir: Path
):
lock_file1 = chdir / "pylock.toml"
lock_file1.write_text(dedent(PYLOCK_TOML_WITH_DIRECTORY))
lock_file2 = chdir / "pylock.dev.toml"
lock_file2.write_text(dedent(PYLOCK_TOML_WITH_DIRECTORY))
config.settings["version_provider"] = "pep751"
provider = get_provider(config)
provider.set_version("42.1")
assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED)
assert lock_file1.read_text() == dedent(PYLOCK_TOML_WITH_DIRECTORY_EXPECTED)
assert lock_file2.read_text() == dedent(PYLOCK_TOML_WITH_DIRECTORY_EXPECTED)
def test_provider_registration(config: BaseConfig, pyproject: Path):
config.settings["version_provider"] = "pep751"
provider = get_provider(config)
assert isinstance(provider, Pep751Provider)