|
| 1 | +"""Tests for environment generation for hooks.""" |
| 2 | + |
| 3 | +import datetime |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from bumpversion.hooks import scm_env, PREFIX, base_env, current_version_env |
| 9 | +from tests.conftest import inside_dir, get_config_data |
| 10 | + |
| 11 | + |
| 12 | +def test_scm_env_returns_correct_info(git_repo: Path): |
| 13 | + """Should return information about the latest tag.""" |
| 14 | + readme = git_repo.joinpath("readme.md") |
| 15 | + readme.touch() |
| 16 | + tag_prefix = "v" |
| 17 | + overrides = {"current_version": "0.1.0", "commit": True, "tag": True, "tag_name": f"{tag_prefix}{{new_version}}"} |
| 18 | + |
| 19 | + with inside_dir(git_repo): |
| 20 | + # Add a file and tag |
| 21 | + subprocess.run(["git", "add", "readme.md"]) |
| 22 | + subprocess.run(["git", "commit", "-m", "first"]) |
| 23 | + subprocess.run(["git", "tag", f"{tag_prefix}0.1.0"]) |
| 24 | + conf, _, _ = get_config_data(overrides) |
| 25 | + |
| 26 | + result = scm_env(conf) |
| 27 | + assert result[f"{PREFIX}BRANCH_NAME"] == "master" |
| 28 | + assert len(result[f"{PREFIX}COMMIT_SHA"]) == 40 |
| 29 | + assert result[f"{PREFIX}CURRENT_TAG"] == "v0.1.0" |
| 30 | + assert result[f"{PREFIX}CURRENT_VERSION"] == "0.1.0" |
| 31 | + assert result[f"{PREFIX}DISTANCE_TO_LATEST_TAG"] == "0" |
| 32 | + assert result[f"{PREFIX}IS_DIRTY"] == "False" |
| 33 | + assert result[f"{PREFIX}SHORT_BRANCH_NAME"] == "master" |
| 34 | + |
| 35 | + |
| 36 | +class MockDatetime(datetime.datetime): |
| 37 | + @classmethod |
| 38 | + def now(cls, tz=None): |
| 39 | + return cls(2022, 2, 1, 17) if tz else cls(2022, 2, 1, 12) |
| 40 | + |
| 41 | + |
| 42 | +class TestBaseEnv: |
| 43 | + """Tests for base_env function.""" |
| 44 | + |
| 45 | + def test_includes_now_and_utcnow(self, mocker): |
| 46 | + """The output includes NOW and UTCNOW.""" |
| 47 | + mocker.patch("datetime.datetime", new=MockDatetime) |
| 48 | + config, _, _ = get_config_data({"current_version": "0.1.0"}) |
| 49 | + result_env = base_env(config) |
| 50 | + |
| 51 | + assert f"{PREFIX}NOW" in result_env |
| 52 | + assert f"{PREFIX}UTCNOW" in result_env |
| 53 | + assert result_env[f"{PREFIX}NOW"] == "2022-02-01T12:00:00" |
| 54 | + assert result_env[f"{PREFIX}UTCNOW"] == "2022-02-01T17:00:00" |
| 55 | + |
| 56 | + def test_includes_os_environ(self): |
| 57 | + """The output includes the current process' environment.""" |
| 58 | + config, _, _ = get_config_data({"current_version": "0.1.0"}) |
| 59 | + result_env = base_env(config) |
| 60 | + |
| 61 | + for var, value in os.environ.items(): |
| 62 | + assert var in result_env |
| 63 | + assert result_env[var] == value |
| 64 | + |
| 65 | + def test_includes_scm_info(self): |
| 66 | + """The output includes SCM information.""" |
| 67 | + config, _, _ = get_config_data({"current_version": "0.1.0"}) |
| 68 | + result_env = base_env(config) |
| 69 | + |
| 70 | + assert f"{PREFIX}COMMIT_SHA" in result_env |
| 71 | + assert f"{PREFIX}DISTANCE_TO_LATEST_TAG" in result_env |
| 72 | + assert f"{PREFIX}IS_DIRTY" in result_env |
| 73 | + assert f"{PREFIX}BRANCH_NAME" in result_env |
| 74 | + assert f"{PREFIX}SHORT_BRANCH_NAME" in result_env |
| 75 | + assert f"{PREFIX}CURRENT_VERSION" in result_env |
| 76 | + assert f"{PREFIX}CURRENT_TAG" in result_env |
| 77 | + |
| 78 | + |
| 79 | +def test_current_version_env_includes_correct_info(): |
| 80 | + """pass""" |
| 81 | + config, _, _ = get_config_data( |
| 82 | + {"current_version": "0.1.0", "parse": r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)"} |
| 83 | + ) |
| 84 | + result = current_version_env(config) |
| 85 | + |
| 86 | + assert result[f"{PREFIX}CURRENT_MAJOR"] == "0" |
| 87 | + assert result[f"{PREFIX}CURRENT_MINOR"] == "1" |
| 88 | + assert result[f"{PREFIX}CURRENT_PATCH"] == "0" |
0 commit comments