|
2 | 2 | Unit tests for metaspec.cli.contribute module. |
3 | 3 | """ |
4 | 4 |
|
5 | | -from unittest.mock import MagicMock, patch |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +from unittest.mock import MagicMock, mock_open, patch |
6 | 8 |
|
7 | 9 | from typer.testing import CliRunner |
8 | 10 |
|
@@ -54,3 +56,196 @@ def test_contribute_command_description(self) -> None: |
54 | 56 | assert result.exit_code == 0 |
55 | 57 | assert "community" in result.stdout.lower() or "registry" in result.stdout.lower() |
56 | 58 |
|
| 59 | + @patch("metaspec.cli.contribute.shutil.which") |
| 60 | + @patch("metaspec.cli.contribute.CommunityRegistry") |
| 61 | + @patch("metaspec.cli.contribute.Prompt.ask") |
| 62 | + @patch("metaspec.cli.contribute.Confirm.ask") |
| 63 | + @patch("builtins.open", new_callable=mock_open) |
| 64 | + def test_contribute_interactive_full_flow( |
| 65 | + self, |
| 66 | + mock_file: MagicMock, |
| 67 | + mock_confirm: MagicMock, |
| 68 | + mock_prompt: MagicMock, |
| 69 | + mock_registry_class: MagicMock, |
| 70 | + mock_which: MagicMock, |
| 71 | + ) -> None: |
| 72 | + """Test complete interactive contribution flow.""" |
| 73 | + # Mock command exists |
| 74 | + mock_which.return_value = "/usr/local/bin/my-spec-kit" |
| 75 | + |
| 76 | + # Mock registry detection |
| 77 | + mock_registry = MagicMock() |
| 78 | + mock_registry.detect_speckit_info.return_value = { |
| 79 | + "version": "1.0.0", |
| 80 | + "cli_commands": ["init", "validate"], |
| 81 | + } |
| 82 | + mock_registry_class.return_value = mock_registry |
| 83 | + |
| 84 | + # Mock user inputs |
| 85 | + mock_prompt.side_effect = [ |
| 86 | + "my-spec-kit", # name |
| 87 | + "A testing toolkit", # description |
| 88 | + "my-spec-kit", # pypi package |
| 89 | + "https://github.com/user/repo", # repository |
| 90 | + "John Doe", # author |
| 91 | + "1.0.0", # version |
| 92 | + "testing,validation", # tags |
| 93 | + ] |
| 94 | + mock_confirm.side_effect = [ |
| 95 | + True, # use detected commands |
| 96 | + ] |
| 97 | + |
| 98 | + result = runner.invoke( |
| 99 | + app, |
| 100 | + ["contribute", "--command", "my-spec-kit"], |
| 101 | + ) |
| 102 | + |
| 103 | + # Should complete successfully |
| 104 | + assert result.exit_code == 0 |
| 105 | + assert "Generated metadata" in result.stdout or mock_file.called |
| 106 | + |
| 107 | + @patch("metaspec.cli.contribute.shutil.which") |
| 108 | + @patch("metaspec.cli.contribute.CommunityRegistry") |
| 109 | + @patch("metaspec.cli.contribute.Prompt.ask") |
| 110 | + @patch("metaspec.cli.contribute.Confirm.ask") |
| 111 | + @patch("builtins.open", new_callable=mock_open) |
| 112 | + def test_contribute_with_detected_commands( |
| 113 | + self, |
| 114 | + mock_file: MagicMock, |
| 115 | + mock_confirm: MagicMock, |
| 116 | + mock_prompt: MagicMock, |
| 117 | + mock_registry_class: MagicMock, |
| 118 | + mock_which: MagicMock, |
| 119 | + ) -> None: |
| 120 | + """Test contribution uses detected commands.""" |
| 121 | + # Mock command exists |
| 122 | + mock_which.return_value = "/usr/local/bin/test-kit" |
| 123 | + |
| 124 | + # Mock registry with detected commands |
| 125 | + mock_registry = MagicMock() |
| 126 | + mock_registry.detect_speckit_info.return_value = { |
| 127 | + "version": "0.5.0", |
| 128 | + "cli_commands": ["init", "validate", "generate"], |
| 129 | + } |
| 130 | + mock_registry_class.return_value = mock_registry |
| 131 | + |
| 132 | + # Mock user inputs |
| 133 | + mock_prompt.side_effect = [ |
| 134 | + "test-kit", # name |
| 135 | + "Testing toolkit", # description |
| 136 | + "test-kit", # pypi package |
| 137 | + "", # repository (optional) |
| 138 | + "", # author (optional) |
| 139 | + "0.5.0", # version |
| 140 | + "testing", # tags |
| 141 | + ] |
| 142 | + mock_confirm.side_effect = [ |
| 143 | + True, # use detected commands |
| 144 | + ] |
| 145 | + |
| 146 | + result = runner.invoke( |
| 147 | + app, |
| 148 | + ["contribute", "--command", "test-kit"], |
| 149 | + ) |
| 150 | + |
| 151 | + # Should complete successfully |
| 152 | + assert result.exit_code == 0 |
| 153 | + assert "Detected commands" in result.stdout or result.exit_code == 0 |
| 154 | + |
| 155 | + @patch("metaspec.cli.contribute.shutil.which") |
| 156 | + @patch("metaspec.cli.contribute.Confirm.ask") |
| 157 | + def test_contribute_command_not_in_path_continue( |
| 158 | + self, |
| 159 | + mock_confirm: MagicMock, |
| 160 | + mock_which: MagicMock, |
| 161 | + ) -> None: |
| 162 | + """Test contribution continues when command not in PATH but user confirms.""" |
| 163 | + # Mock command not found |
| 164 | + mock_which.return_value = None |
| 165 | + |
| 166 | + # User chooses not to continue |
| 167 | + mock_confirm.return_value = False |
| 168 | + |
| 169 | + result = runner.invoke( |
| 170 | + app, |
| 171 | + ["contribute", "--command", "missing-cmd"], |
| 172 | + ) |
| 173 | + |
| 174 | + # Should exit |
| 175 | + assert result.exit_code == 1 |
| 176 | + |
| 177 | + @patch("metaspec.cli.contribute.shutil.which") |
| 178 | + @patch("metaspec.cli.contribute.CommunityRegistry") |
| 179 | + @patch("metaspec.cli.contribute.Prompt.ask") |
| 180 | + @patch("metaspec.cli.contribute.Confirm.ask") |
| 181 | + @patch("builtins.open", new_callable=mock_open) |
| 182 | + def test_contribute_custom_commands( |
| 183 | + self, |
| 184 | + mock_file: MagicMock, |
| 185 | + mock_confirm: MagicMock, |
| 186 | + mock_prompt: MagicMock, |
| 187 | + mock_registry_class: MagicMock, |
| 188 | + mock_which: MagicMock, |
| 189 | + ) -> None: |
| 190 | + """Test contribution with custom commands instead of detected ones.""" |
| 191 | + # Mock command exists |
| 192 | + mock_which.return_value = "/usr/bin/custom-kit" |
| 193 | + |
| 194 | + # Mock registry |
| 195 | + mock_registry = MagicMock() |
| 196 | + mock_registry.detect_speckit_info.return_value = { |
| 197 | + "version": "1.0.0", |
| 198 | + "cli_commands": ["default1", "default2"], |
| 199 | + } |
| 200 | + mock_registry_class.return_value = mock_registry |
| 201 | + |
| 202 | + # Mock user inputs |
| 203 | + mock_prompt.side_effect = [ |
| 204 | + "custom-kit", # name |
| 205 | + "Custom toolkit", # description |
| 206 | + "custom-kit", # pypi package |
| 207 | + "", # repository |
| 208 | + "", # author |
| 209 | + "1.0.0", # version |
| 210 | + "custom,tools", # tags |
| 211 | + "custom1,custom2,custom3", # custom commands |
| 212 | + ] |
| 213 | + mock_confirm.side_effect = [ |
| 214 | + False, # don't use detected commands |
| 215 | + ] |
| 216 | + |
| 217 | + result = runner.invoke( |
| 218 | + app, |
| 219 | + ["contribute", "--command", "custom-kit"], |
| 220 | + ) |
| 221 | + |
| 222 | + # Should complete successfully |
| 223 | + assert result.exit_code == 0 |
| 224 | + |
| 225 | + def test_contribute_non_interactive_fails(self) -> None: |
| 226 | + """Test that non-interactive mode shows error.""" |
| 227 | + result = runner.invoke( |
| 228 | + app, |
| 229 | + ["contribute", "--command", "test", "--no-interactive"], |
| 230 | + ) |
| 231 | + |
| 232 | + # Should fail with error message |
| 233 | + assert result.exit_code == 1 |
| 234 | + assert "Interactive mode is required" in result.stdout |
| 235 | + |
| 236 | + @patch("metaspec.cli.contribute.Prompt.ask") |
| 237 | + def test_contribute_no_command_interactive_prompt( |
| 238 | + self, mock_prompt: MagicMock |
| 239 | + ) -> None: |
| 240 | + """Test that missing command triggers interactive prompt.""" |
| 241 | + # Mock command input, then other prompts will fail |
| 242 | + mock_prompt.side_effect = [ |
| 243 | + "test-kit", # command name |
| 244 | + Exception("Stop here for test"), # Stop execution |
| 245 | + ] |
| 246 | + |
| 247 | + result = runner.invoke(app, ["contribute"]) |
| 248 | + |
| 249 | + # Should attempt to prompt for command |
| 250 | + assert mock_prompt.called |
| 251 | + |
0 commit comments