Files
devx/tests/unit/test_validate_deploy_ref.py
emil ddfbdec956
Post-merge / detect-and-configure (push) Successful in 12s
Post-merge / release-and-maintain (push) Successful in 1m0s
DEVX-136: feat: add fix_pr_title module and update_pr API method
2026-07-13 23:55:11 +00:00

74 lines
3.3 KiB
Python

"""Unit tests for devx.ci.validate_deploy_ref."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import MagicMock, patch
from click.testing import CliRunner
from devx.ci.validate_deploy_ref import main
class TestValidateDeployRef:
def test_valid_tag_prints_ref(self, tmp_path: Path) -> None:
runner = CliRunner()
with patch("devx.ci.validate_deploy_ref.subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="abcdef1234567890\n", stderr="")
result = runner.invoke(main, ["--tag", "v1.0.0"])
assert result.exit_code == 0
assert "v1.0.0" in result.output
def test_invalid_tag_exits_nonzero(self) -> None:
runner = CliRunner()
with patch("devx.ci.validate_deploy_ref.subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=1, stdout="", stderr="error")
result = runner.invoke(main, ["--tag", "nonexistent"])
assert result.exit_code == 1
assert "does not exist" in result.output
@patch("devx.ci.validate_deploy_ref.subprocess.run")
def test_no_tag_without_allow_empty_exits_nonzero(self, mock_subproc: MagicMock) -> None:
runner = CliRunner()
result = runner.invoke(main, [])
assert result.exit_code == 1
assert "No tag specified" in result.output
@patch("devx.ci.validate_deploy_ref.subprocess.run")
def test_allow_empty_prints_pr_mode(self, mock_subproc: MagicMock) -> None:
runner = CliRunner()
result = runner.invoke(main, ["--allow-empty"])
assert result.exit_code == 0
assert "PR mode" in result.output
def test_github_output_writes_ref(self, tmp_path: Path) -> None:
runner = CliRunner()
gh_output = tmp_path / "github_output"
gh_output.write_text("")
with patch("devx.ci.validate_deploy_ref.subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="abcdef12\n", stderr="")
with runner.isolation(env={"GITHUB_OUTPUT": str(gh_output)}):
result = runner.invoke(main, ["--tag", "v1.0.0", "--github-output"])
assert result.exit_code == 0
content = gh_output.read_text()
assert "deploy-ref=v1.0.0" in content
def test_github_output_without_env_var_exits_nonzero(self) -> None:
runner = CliRunner()
with patch("devx.ci.validate_deploy_ref.subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0, stdout="abcdef12\n", stderr="")
with runner.isolation(env={"GITHUB_OUTPUT": ""}):
result = runner.invoke(main, ["--tag", "v1.0.0", "--github-output"])
assert result.exit_code == 1
assert "GITHUB_OUTPUT" in result.output
@patch("devx.ci.validate_deploy_ref.subprocess.run")
def test_allow_empty_with_github_output(self, mock_subproc: MagicMock, tmp_path: Path) -> None:
runner = CliRunner()
gh_output = tmp_path / "github_output"
gh_output.write_text("")
with runner.isolation(env={"GITHUB_OUTPUT": str(gh_output)}):
result = runner.invoke(main, ["--allow-empty", "--github-output"])
assert result.exit_code == 0
assert "deploy-ref=" in gh_output.read_text()