Skip to content

Commit

Permalink
test(providers): factorize some version providers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre authored and Lee-W committed Feb 12, 2023
1 parent 015d7c0 commit 5489177
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 119 deletions.
7 changes: 6 additions & 1 deletion tests/commands/test_version_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ def test_version_use_version_provider(

commands.Version(
config,
{"report": False, "project": project, "commitizen": False, "verbose": True},
{
"report": False,
"project": project,
"commitizen": False,
"verbose": not project,
},
)()
captured = capsys.readouterr()

Expand Down
201 changes: 83 additions & 118 deletions tests/test_version_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING, Iterator, Optional
from typing import TYPE_CHECKING, Iterator, Optional, Type

import pytest

Expand All @@ -17,6 +17,7 @@
Pep621Provider,
PoetryProvider,
ScmProvider,
VersionProvider,
get_provider,
)
from tests.utils import create_file_and_commit, create_tag
Expand Down Expand Up @@ -56,137 +57,101 @@ def test_commitizen_provider(config: BaseConfig, mocker: MockerFixture):
mock.assert_called_once_with("version", "43.1")


def test_pep621_provider(config: BaseConfig, chdir: Path):
pyproject_toml = chdir / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[project]
version = "0.1.0"
"""
)
)

provider = Pep621Provider(config)

assert provider.get_version() == "0.1.0"

provider.set_version("43.1")

assert pyproject_toml.read_text() == dedent(
FILE_PROVIDERS = dict(
pep621=(
"pyproject.toml",
Pep621Provider,
"""\
[project]
version = "43.1"
"""
)


def test_poetry_provider(config: BaseConfig, chdir: Path):
pyproject_toml = chdir / "pyproject.toml"
pyproject_toml.write_text(
dedent(
"""\
[tool.poetry]
version = "0.1.0"
"""
)
)
config.settings["version_provider"] = "poetry"

provider = get_provider(config)
assert isinstance(provider, PoetryProvider)
assert provider.get_version() == "0.1.0"

provider.set_version("43.1")
assert pyproject_toml.read_text() == dedent(
version = "0.1.0"
""",
"""\
[project]
version = "42.1"
""",
),
poetry=(
"pyproject.toml",
PoetryProvider,
"""\
[tool.poetry]
version = "43.1"
"""
)


def test_cargo_provider(config: BaseConfig, chdir: Path):
cargo_toml = chdir / "Cargo.toml"
cargo_toml.write_text(
dedent(
"""\
[package]
version = "0.1.0"
"""
)
)
config.settings["version_provider"] = "cargo"

provider = get_provider(config)
assert isinstance(provider, CargoProvider)
assert provider.get_version() == "0.1.0"

provider.set_version("43.1")
assert cargo_toml.read_text() == dedent(
version = "0.1.0"
""",
"""\
[tool.poetry]
version = "42.1"
""",
),
cargo=(
"Cargo.toml",
CargoProvider,
"""\
[package]
version = "43.1"
"""
)


def test_npm_provider(config: BaseConfig, chdir: Path):
package_json = chdir / "package.json"
package_json.write_text(
dedent(
"""\
{
"name": "whatever",
"version": "0.1.0"
}
"""
)
)
config.settings["version_provider"] = "npm"

provider = get_provider(config)
assert isinstance(provider, NpmProvider)
assert provider.get_version() == "0.1.0"

provider.set_version("43.1")
assert package_json.read_text() == dedent(
version = "0.1.0"
""",
"""\
[package]
version = "42.1"
""",
),
npm=(
"package.json",
NpmProvider,
"""\
{
"name": "whatever",
"version": "43.1"
"version": "0.1.0"
}
"""
)


def test_composer_provider(config: BaseConfig, chdir: Path):
composer_json = chdir / "composer.json"
composer_json.write_text(
dedent(
"""\
{
"name": "whatever",
"version": "0.1.0"
}
"""
)
)
config.settings["version_provider"] = "composer"

provider = get_provider(config)
assert isinstance(provider, ComposerProvider)
assert provider.get_version() == "0.1.0"

provider.set_version("43.1")
assert composer_json.read_text() == dedent(
""",
"""\
{
"name": "whatever",
"version": "42.1"
}
""",
),
composer=(
"composer.json",
ComposerProvider,
"""\
{
"name": "whatever",
"version": "43.1"
"version": "0.1.0"
}
"""
)
""",
"""\
{
"name": "whatever",
"version": "42.1"
}
""",
),
)


@pytest.mark.parametrize(
"id,filename,cls,content,expected",
(pytest.param(id, *FILE_PROVIDERS[id], id=id) for id in FILE_PROVIDERS),
)
def test_file_providers(
config: BaseConfig,
chdir: Path,
id: str,
filename: str,
cls: Type[VersionProvider],
content: str,
expected: str,
):
file = chdir / filename
file.write_text(dedent(content))
config.settings["version_provider"] = id

provider = get_provider(config)
assert isinstance(provider, cls)
assert provider.get_version() == "0.1.0"

provider.set_version("42.1")
assert file.read_text() == dedent(expected)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 5489177

Please sign in to comment.