|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from textwrap import dedent |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from commitizen.providers import get_provider |
| 9 | +from commitizen.providers.pep751_provider import Pep751Provider |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from pathlib import Path |
| 13 | + |
| 14 | + from commitizen.config.base_config import BaseConfig |
| 15 | + |
| 16 | +PYPROJECT_TOML = """\ |
| 17 | +[project] |
| 18 | +name = "my-package" |
| 19 | +version = "0.1.0" |
| 20 | +""" |
| 21 | + |
| 22 | +PYPROJECT_TOML_EXPECTED = """\ |
| 23 | +[project] |
| 24 | +name = "my-package" |
| 25 | +version = "42.1" |
| 26 | +""" |
| 27 | + |
| 28 | +PYLOCK_TOML_WITH_DIRECTORY = """\ |
| 29 | +lock-version = "1.0" |
| 30 | +created-by = "test" |
| 31 | +
|
| 32 | +[[packages]] |
| 33 | +name = "my-package" |
| 34 | +version = "0.1.0" |
| 35 | +
|
| 36 | +[packages.directory] |
| 37 | +path = "." |
| 38 | +editable = true |
| 39 | +
|
| 40 | +[[packages]] |
| 41 | +name = "some-dep" |
| 42 | +version = "1.2.3" |
| 43 | +""" |
| 44 | + |
| 45 | +PYLOCK_TOML_WITH_DIRECTORY_EXPECTED = """\ |
| 46 | +lock-version = "1.0" |
| 47 | +created-by = "test" |
| 48 | +
|
| 49 | +[[packages]] |
| 50 | +name = "my-package" |
| 51 | +version = "42.1" |
| 52 | +
|
| 53 | +[packages.directory] |
| 54 | +path = "." |
| 55 | +editable = true |
| 56 | +
|
| 57 | +[[packages]] |
| 58 | +name = "some-dep" |
| 59 | +version = "1.2.3" |
| 60 | +""" |
| 61 | + |
| 62 | +PYLOCK_TOML_NON_MATCHING_NAME = """\ |
| 63 | +lock-version = "1.0" |
| 64 | +created-by = "test" |
| 65 | +
|
| 66 | +[[packages]] |
| 67 | +name = "other-package" |
| 68 | +version = "0.1.0" |
| 69 | +
|
| 70 | +[packages.directory] |
| 71 | +path = "." |
| 72 | +editable = true |
| 73 | +""" |
| 74 | + |
| 75 | +PYLOCK_TOML_NON_DIRECTORY = """\ |
| 76 | +lock-version = "1.0" |
| 77 | +created-by = "test" |
| 78 | +
|
| 79 | +[[packages]] |
| 80 | +name = "my-package" |
| 81 | +version = "0.1.0" |
| 82 | +""" |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture |
| 86 | +def pyproject(chdir: Path) -> Path: |
| 87 | + file = chdir / "pyproject.toml" |
| 88 | + file.write_text(dedent(PYPROJECT_TOML)) |
| 89 | + return file |
| 90 | + |
| 91 | + |
| 92 | +def test_get_version(config: BaseConfig, pyproject: Path): |
| 93 | + config.settings["version_provider"] = "pep751" |
| 94 | + provider = get_provider(config) |
| 95 | + assert isinstance(provider, Pep751Provider) |
| 96 | + assert provider.get_version() == "0.1.0" |
| 97 | + |
| 98 | + |
| 99 | +def test_set_version_without_lock_files( |
| 100 | + config: BaseConfig, pyproject: Path, chdir: Path |
| 101 | +): |
| 102 | + config.settings["version_provider"] = "pep751" |
| 103 | + provider = get_provider(config) |
| 104 | + |
| 105 | + provider.set_version("42.1") |
| 106 | + |
| 107 | + assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED) |
| 108 | + # No pylock*.toml files should exist |
| 109 | + assert list(chdir.glob("pylock*.toml")) == [] |
| 110 | + |
| 111 | + |
| 112 | +def test_set_version_with_pylock_toml(config: BaseConfig, pyproject: Path, chdir: Path): |
| 113 | + lock_file = chdir / "pylock.toml" |
| 114 | + lock_file.write_text(dedent(PYLOCK_TOML_WITH_DIRECTORY)) |
| 115 | + config.settings["version_provider"] = "pep751" |
| 116 | + provider = get_provider(config) |
| 117 | + |
| 118 | + provider.set_version("42.1") |
| 119 | + |
| 120 | + assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED) |
| 121 | + assert lock_file.read_text() == dedent(PYLOCK_TOML_WITH_DIRECTORY_EXPECTED) |
| 122 | + |
| 123 | + |
| 124 | +def test_set_version_with_named_lock_file( |
| 125 | + config: BaseConfig, pyproject: Path, chdir: Path |
| 126 | +): |
| 127 | + lock_file = chdir / "pylock.dev.toml" |
| 128 | + lock_file.write_text(dedent(PYLOCK_TOML_WITH_DIRECTORY)) |
| 129 | + config.settings["version_provider"] = "pep751" |
| 130 | + provider = get_provider(config) |
| 131 | + |
| 132 | + provider.set_version("42.1") |
| 133 | + |
| 134 | + assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED) |
| 135 | + assert lock_file.read_text() == dedent(PYLOCK_TOML_WITH_DIRECTORY_EXPECTED) |
| 136 | + |
| 137 | + |
| 138 | +def test_set_version_non_matching_package_not_updated( |
| 139 | + config: BaseConfig, pyproject: Path, chdir: Path |
| 140 | +): |
| 141 | + lock_file = chdir / "pylock.toml" |
| 142 | + lock_file.write_text(dedent(PYLOCK_TOML_NON_MATCHING_NAME)) |
| 143 | + original_lock_content = lock_file.read_text() |
| 144 | + config.settings["version_provider"] = "pep751" |
| 145 | + provider = get_provider(config) |
| 146 | + |
| 147 | + provider.set_version("42.1") |
| 148 | + |
| 149 | + assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED) |
| 150 | + # Lock file should be unchanged since no matching package name |
| 151 | + assert lock_file.read_text() == original_lock_content |
| 152 | + |
| 153 | + |
| 154 | +def test_set_version_non_directory_source_not_updated( |
| 155 | + config: BaseConfig, pyproject: Path, chdir: Path |
| 156 | +): |
| 157 | + lock_file = chdir / "pylock.toml" |
| 158 | + lock_file.write_text(dedent(PYLOCK_TOML_NON_DIRECTORY)) |
| 159 | + original_lock_content = lock_file.read_text() |
| 160 | + config.settings["version_provider"] = "pep751" |
| 161 | + provider = get_provider(config) |
| 162 | + |
| 163 | + provider.set_version("42.1") |
| 164 | + |
| 165 | + assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED) |
| 166 | + # Lock file should be unchanged since package has no directory source |
| 167 | + assert lock_file.read_text() == original_lock_content |
| 168 | + |
| 169 | + |
| 170 | +def test_set_version_multiple_lock_files( |
| 171 | + config: BaseConfig, pyproject: Path, chdir: Path |
| 172 | +): |
| 173 | + lock_file1 = chdir / "pylock.toml" |
| 174 | + lock_file1.write_text(dedent(PYLOCK_TOML_WITH_DIRECTORY)) |
| 175 | + lock_file2 = chdir / "pylock.dev.toml" |
| 176 | + lock_file2.write_text(dedent(PYLOCK_TOML_WITH_DIRECTORY)) |
| 177 | + config.settings["version_provider"] = "pep751" |
| 178 | + provider = get_provider(config) |
| 179 | + |
| 180 | + provider.set_version("42.1") |
| 181 | + |
| 182 | + assert pyproject.read_text() == dedent(PYPROJECT_TOML_EXPECTED) |
| 183 | + assert lock_file1.read_text() == dedent(PYLOCK_TOML_WITH_DIRECTORY_EXPECTED) |
| 184 | + assert lock_file2.read_text() == dedent(PYLOCK_TOML_WITH_DIRECTORY_EXPECTED) |
| 185 | + |
| 186 | + |
| 187 | +def test_provider_registration(config: BaseConfig, pyproject: Path): |
| 188 | + config.settings["version_provider"] = "pep751" |
| 189 | + provider = get_provider(config) |
| 190 | + assert isinstance(provider, Pep751Provider) |
0 commit comments