|
| 1 | +"""Tests for the ``azpysdk changelog`` command group.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | +from unittest.mock import patch, MagicMock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from azpysdk.changelog import changelog |
| 9 | + |
| 10 | + |
| 11 | +# --------------------------------------------------------------------------- |
| 12 | +# Helper – build a minimal parser that includes the changelog subcommands |
| 13 | +# --------------------------------------------------------------------------- |
| 14 | + |
| 15 | +def _build_parser(): |
| 16 | + parser = argparse.ArgumentParser(prog="azpysdk") |
| 17 | + subparsers = parser.add_subparsers(title="commands", dest="command") |
| 18 | + changelog().register(subparsers) |
| 19 | + return parser |
| 20 | + |
| 21 | + |
| 22 | +# --------------------------------------------------------------------------- |
| 23 | +# Parser / registration tests |
| 24 | +# --------------------------------------------------------------------------- |
| 25 | + |
| 26 | + |
| 27 | +class TestChangelogRegistration: |
| 28 | + """Verify that the changelog command is registered correctly.""" |
| 29 | + |
| 30 | + def test_changelog_subcommand_exists(self): |
| 31 | + parser = _build_parser() |
| 32 | + args = parser.parse_args(["changelog", "verify"]) |
| 33 | + assert args.command == "changelog" |
| 34 | + assert args.changelog_command == "verify" |
| 35 | + |
| 36 | + def test_changelog_add_without_package(self): |
| 37 | + parser = _build_parser() |
| 38 | + args = parser.parse_args(["changelog", "add"]) |
| 39 | + assert args.changelog_command == "add" |
| 40 | + assert args.package is None |
| 41 | + |
| 42 | + def test_changelog_add_with_package(self): |
| 43 | + parser = _build_parser() |
| 44 | + args = parser.parse_args(["changelog", "add", "sdk/storage/azure-storage-blob"]) |
| 45 | + assert args.changelog_command == "add" |
| 46 | + assert args.package == "sdk/storage/azure-storage-blob" |
| 47 | + |
| 48 | + def test_changelog_create(self): |
| 49 | + parser = _build_parser() |
| 50 | + args = parser.parse_args(["changelog", "create"]) |
| 51 | + assert args.changelog_command == "create" |
| 52 | + |
| 53 | + def test_changelog_status(self): |
| 54 | + parser = _build_parser() |
| 55 | + args = parser.parse_args(["changelog", "status"]) |
| 56 | + assert args.changelog_command == "status" |
| 57 | + |
| 58 | + def test_changelog_no_subcommand_prints_help(self): |
| 59 | + """When no subcommand is given, the handler should print help and return 1.""" |
| 60 | + parser = _build_parser() |
| 61 | + args = parser.parse_args(["changelog"]) |
| 62 | + assert hasattr(args, "func") |
| 63 | + # func should be the _no_subcommand method |
| 64 | + result = args.func(args) |
| 65 | + assert result == 1 |
| 66 | + |
| 67 | + |
| 68 | +# --------------------------------------------------------------------------- |
| 69 | +# Execution tests (npx / chronus invocation) |
| 70 | +# --------------------------------------------------------------------------- |
| 71 | + |
| 72 | + |
| 73 | +class TestChangelogExecution: |
| 74 | + """Verify that each subcommand invokes chronus with the right arguments.""" |
| 75 | + |
| 76 | + @patch("azpysdk.changelog.subprocess.call", return_value=0) |
| 77 | + @patch("azpysdk.changelog.shutil.which", return_value="/usr/bin/npx") |
| 78 | + def test_add_calls_chronus_add(self, mock_which, mock_call): |
| 79 | + parser = _build_parser() |
| 80 | + args = parser.parse_args(["changelog", "add"]) |
| 81 | + result = args.func(args) |
| 82 | + assert result == 0 |
| 83 | + mock_call.assert_called_once() |
| 84 | + cmd = mock_call.call_args[0][0] |
| 85 | + assert cmd == ["/usr/bin/npx", "chronus", "add"] |
| 86 | + |
| 87 | + @patch("azpysdk.changelog.subprocess.call", return_value=0) |
| 88 | + @patch("azpysdk.changelog.shutil.which", return_value="/usr/bin/npx") |
| 89 | + def test_add_with_package_passes_package(self, mock_which, mock_call): |
| 90 | + parser = _build_parser() |
| 91 | + args = parser.parse_args(["changelog", "add", "sdk/core/azure-core"]) |
| 92 | + result = args.func(args) |
| 93 | + assert result == 0 |
| 94 | + cmd = mock_call.call_args[0][0] |
| 95 | + assert cmd == ["/usr/bin/npx", "chronus", "add", "sdk/core/azure-core"] |
| 96 | + |
| 97 | + @patch("azpysdk.changelog.subprocess.call", return_value=0) |
| 98 | + @patch("azpysdk.changelog.shutil.which", return_value="/usr/bin/npx") |
| 99 | + def test_verify_calls_chronus_verify(self, mock_which, mock_call): |
| 100 | + parser = _build_parser() |
| 101 | + args = parser.parse_args(["changelog", "verify"]) |
| 102 | + result = args.func(args) |
| 103 | + assert result == 0 |
| 104 | + cmd = mock_call.call_args[0][0] |
| 105 | + assert cmd == ["/usr/bin/npx", "chronus", "verify"] |
| 106 | + |
| 107 | + @patch("azpysdk.changelog.subprocess.call", return_value=0) |
| 108 | + @patch("azpysdk.changelog.shutil.which", return_value="/usr/bin/npx") |
| 109 | + def test_create_calls_chronus_changelog(self, mock_which, mock_call): |
| 110 | + parser = _build_parser() |
| 111 | + args = parser.parse_args(["changelog", "create"]) |
| 112 | + result = args.func(args) |
| 113 | + assert result == 0 |
| 114 | + cmd = mock_call.call_args[0][0] |
| 115 | + assert cmd == ["/usr/bin/npx", "chronus", "changelog"] |
| 116 | + |
| 117 | + @patch("azpysdk.changelog.subprocess.call", return_value=0) |
| 118 | + @patch("azpysdk.changelog.shutil.which", return_value="/usr/bin/npx") |
| 119 | + def test_status_calls_chronus_status(self, mock_which, mock_call): |
| 120 | + parser = _build_parser() |
| 121 | + args = parser.parse_args(["changelog", "status"]) |
| 122 | + result = args.func(args) |
| 123 | + assert result == 0 |
| 124 | + cmd = mock_call.call_args[0][0] |
| 125 | + assert cmd == ["/usr/bin/npx", "chronus", "status"] |
| 126 | + |
| 127 | + @patch("azpysdk.changelog.subprocess.call", return_value=0) |
| 128 | + @patch("azpysdk.changelog.shutil.which", return_value="/usr/bin/npx") |
| 129 | + def test_chronus_runs_from_repo_root(self, mock_which, mock_call): |
| 130 | + """Chronus must run from the repository root directory.""" |
| 131 | + parser = _build_parser() |
| 132 | + args = parser.parse_args(["changelog", "verify"]) |
| 133 | + args.func(args) |
| 134 | + _, kwargs = mock_call.call_args |
| 135 | + from azpysdk.changelog import REPO_ROOT |
| 136 | + assert kwargs["cwd"] == REPO_ROOT |
| 137 | + |
| 138 | + @patch("azpysdk.changelog.subprocess.call", return_value=1) |
| 139 | + @patch("azpysdk.changelog.shutil.which", return_value="/usr/bin/npx") |
| 140 | + def test_nonzero_exit_code_propagated(self, mock_which, mock_call): |
| 141 | + parser = _build_parser() |
| 142 | + args = parser.parse_args(["changelog", "verify"]) |
| 143 | + result = args.func(args) |
| 144 | + assert result == 1 |
| 145 | + |
| 146 | + |
| 147 | +# --------------------------------------------------------------------------- |
| 148 | +# Error handling tests |
| 149 | +# --------------------------------------------------------------------------- |
| 150 | + |
| 151 | + |
| 152 | +class TestChangelogErrors: |
| 153 | + """Verify error handling when prerequisites are missing.""" |
| 154 | + |
| 155 | + @patch("azpysdk.changelog.shutil.which", return_value=None) |
| 156 | + def test_npx_not_found_raises(self, mock_which): |
| 157 | + parser = _build_parser() |
| 158 | + args = parser.parse_args(["changelog", "verify"]) |
| 159 | + with pytest.raises(FileNotFoundError, match="npx not found"): |
| 160 | + args.func(args) |
0 commit comments