"""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()