-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"""Tests for environment generation for hooks.""" | ||
|
||
import datetime | ||
import os | ||
import subprocess | ||
from pathlib import Path | ||
|
||
from bumpversion.hooks import scm_env, PREFIX, base_env, current_version_env | ||
from tests.conftest import inside_dir, get_config_data | ||
|
||
|
||
def test_scm_env_returns_correct_info(git_repo: Path): | ||
"""Should return information about the latest tag.""" | ||
readme = git_repo.joinpath("readme.md") | ||
readme.touch() | ||
tag_prefix = "v" | ||
overrides = {"current_version": "0.1.0", "commit": True, "tag": True, "tag_name": f"{tag_prefix}{{new_version}}"} | ||
|
||
with inside_dir(git_repo): | ||
# Add a file and tag | ||
subprocess.run(["git", "add", "readme.md"]) | ||
subprocess.run(["git", "commit", "-m", "first"]) | ||
subprocess.run(["git", "tag", f"{tag_prefix}0.1.0"]) | ||
conf, _, _ = get_config_data(overrides) | ||
|
||
result = scm_env(conf) | ||
assert result[f"{PREFIX}BRANCH_NAME"] == "master" | ||
assert len(result[f"{PREFIX}COMMIT_SHA"]) == 40 | ||
assert result[f"{PREFIX}CURRENT_TAG"] == "v0.1.0" | ||
assert result[f"{PREFIX}CURRENT_VERSION"] == "0.1.0" | ||
assert result[f"{PREFIX}DISTANCE_TO_LATEST_TAG"] == "0" | ||
assert result[f"{PREFIX}IS_DIRTY"] == "False" | ||
assert result[f"{PREFIX}SHORT_BRANCH_NAME"] == "master" | ||
|
||
|
||
class MockDatetime(datetime.datetime): | ||
@classmethod | ||
def now(cls, tz=None): | ||
return cls(2022, 2, 1, 17) if tz else cls(2022, 2, 1, 12) | ||
|
||
|
||
class TestBaseEnv: | ||
"""Tests for base_env function.""" | ||
|
||
def test_includes_now_and_utcnow(self, mocker): | ||
"""The output includes NOW and UTCNOW.""" | ||
mocker.patch("datetime.datetime", new=MockDatetime) | ||
config, _, _ = get_config_data({"current_version": "0.1.0"}) | ||
result_env = base_env(config) | ||
|
||
assert f"{PREFIX}NOW" in result_env | ||
assert f"{PREFIX}UTCNOW" in result_env | ||
assert result_env[f"{PREFIX}NOW"] == "2022-02-01T12:00:00" | ||
assert result_env[f"{PREFIX}UTCNOW"] == "2022-02-01T17:00:00" | ||
|
||
def test_includes_os_environ(self): | ||
"""The output includes the current process' environment.""" | ||
config, _, _ = get_config_data({"current_version": "0.1.0"}) | ||
result_env = base_env(config) | ||
|
||
for var, value in os.environ.items(): | ||
assert var in result_env | ||
assert result_env[var] == value | ||
|
||
def test_includes_scm_info(self): | ||
"""The output includes SCM information.""" | ||
config, _, _ = get_config_data({"current_version": "0.1.0"}) | ||
result_env = base_env(config) | ||
|
||
assert f"{PREFIX}COMMIT_SHA" in result_env | ||
assert f"{PREFIX}DISTANCE_TO_LATEST_TAG" in result_env | ||
assert f"{PREFIX}IS_DIRTY" in result_env | ||
assert f"{PREFIX}BRANCH_NAME" in result_env | ||
assert f"{PREFIX}SHORT_BRANCH_NAME" in result_env | ||
assert f"{PREFIX}CURRENT_VERSION" in result_env | ||
assert f"{PREFIX}CURRENT_TAG" in result_env | ||
|
||
|
||
def test_current_version_env_includes_correct_info(): | ||
"""pass""" | ||
config, _, _ = get_config_data( | ||
{"current_version": "0.1.0", "parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"} | ||
) | ||
result = current_version_env(config) | ||
|
||
assert result[f"{PREFIX}CURRENT_MAJOR"] == "0" | ||
assert result[f"{PREFIX}CURRENT_MINOR"] == "1" | ||
assert result[f"{PREFIX}CURRENT_PATCH"] == "0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import subprocess | ||
|
||
import pytest | ||
|
||
from bumpversion.hooks import run_command | ||
|
||
|
||
class TestRunCommand: | ||
"""Test the run_command function.""" | ||
|
||
def test_runs_a_str_command(self): | ||
"""Runs the command formatted as a string.""" | ||
result = run_command("echo Hello") | ||
assert isinstance(result, subprocess.CompletedProcess) | ||
assert result.stdout == "Hello\n" | ||
|
||
def test_can_access_env(self): | ||
"""The command can access custom environment variables.""" | ||
result = run_command("echo $TEST_ENV", environment={"TEST_ENV": "Hello"}) | ||
assert isinstance(result, subprocess.CompletedProcess) | ||
assert result.stdout == "Hello\n" | ||
|
||
def test_non_zero_exit(self): | ||
"""The result shows a non-zero result code.""" | ||
result = run_command("exit 1") | ||
assert result.returncode == 1 | ||
|
||
@pytest.mark.parametrize( | ||
"invalid_script", | ||
[(123,), (None,), (["exit", "1"])], | ||
) | ||
def test_an_invalid_script_raises_type_error(self, invalid_script): | ||
with pytest.raises(TypeError): | ||
run_command(invalid_script) | ||
|
||
@pytest.mark.parametrize( | ||
"invalid_env", | ||
[("string",), (123,), (None,)], | ||
) | ||
def test_an_invalid_env_raises_type_error(self, invalid_env): | ||
with pytest.raises(TypeError): | ||
run_command("echo Hello", environment=invalid_env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import subprocess | ||
|
||
from bumpversion.config import Config | ||
from bumpversion.hooks import run_setup_hooks | ||
from tests.conftest import get_config_data | ||
|
||
|
||
def setup_hook_env(config: Config) -> dict: | ||
"""Mocked function for the environment setup""" | ||
return {} | ||
|
||
|
||
def run_command(script: str, env: dict) -> subprocess.CompletedProcess: | ||
"""Mocked function for command execution""" | ||
return subprocess.CompletedProcess(args=script, returncode=0) | ||
|
||
|
||
def test_run_setup_hooks_calls_each_hook(mocker): | ||
"""The run_setup_hooks function runs each hook.""" | ||
# Assemble | ||
setup_hook_env_mock = mocker.patch("bumpversion.hooks.setup_hook_env", side_effect=setup_hook_env) | ||
run_command_mock = mocker.patch("bumpversion.hooks.run_command", side_effect=run_command) | ||
|
||
config, _, _ = get_config_data({"current_version": "0.1.0", "setup_hooks": ["script1", "script2"]}) | ||
|
||
# Act | ||
result = run_setup_hooks(config) | ||
|
||
# Asserts for function's behavior | ||
setup_hook_env_mock.assert_called_once_with(config) | ||
assert run_command_mock.call_count == len(config.setup_hooks) | ||
run_command_mock.assert_any_call("script1", {}) | ||
run_command_mock.assert_any_call("script2", {}) |