|
| 1 | +"""Tests for CLI module.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +from unittest.mock import Mock, patch, MagicMock |
| 5 | +import pytest |
| 6 | +from gitspaces.cli import create_parser, main |
| 7 | + |
| 8 | + |
| 9 | +def test_create_parser(): |
| 10 | + """Test parser creation.""" |
| 11 | + parser = create_parser() |
| 12 | + assert parser.prog == "gitspaces" |
| 13 | + |
| 14 | + |
| 15 | +def test_parser_version(capsys): |
| 16 | + """Test --version argument.""" |
| 17 | + parser = create_parser() |
| 18 | + with pytest.raises(SystemExit): |
| 19 | + parser.parse_args(["--version"]) |
| 20 | + |
| 21 | + |
| 22 | +def test_parser_debug(): |
| 23 | + """Test --debug argument.""" |
| 24 | + parser = create_parser() |
| 25 | + args = parser.parse_args(["--debug"]) |
| 26 | + assert args.debug is True |
| 27 | + |
| 28 | + |
| 29 | +def test_parser_clone_command(): |
| 30 | + """Test clone command arguments.""" |
| 31 | + parser = create_parser() |
| 32 | + args = parser.parse_args(["clone", "https://github.com/test/repo.git"]) |
| 33 | + assert args.command == "clone" |
| 34 | + assert args.url == "https://github.com/test/repo.git" |
| 35 | + assert args.num_spaces == 3 |
| 36 | + |
| 37 | + |
| 38 | +def test_parser_clone_with_options(): |
| 39 | + """Test clone command with options.""" |
| 40 | + parser = create_parser() |
| 41 | + args = parser.parse_args( |
| 42 | + ["clone", "https://github.com/test/repo.git", "-n", "5", "-d", "/tmp/test"] |
| 43 | + ) |
| 44 | + assert args.num_spaces == 5 |
| 45 | + assert args.directory == "/tmp/test" |
| 46 | + |
| 47 | + |
| 48 | +def test_parser_switch_command(): |
| 49 | + """Test switch command.""" |
| 50 | + parser = create_parser() |
| 51 | + args = parser.parse_args(["switch", "main"]) |
| 52 | + assert args.command == "switch" |
| 53 | + assert args.space == "main" |
| 54 | + |
| 55 | + |
| 56 | +def test_parser_sleep_command(): |
| 57 | + """Test sleep command.""" |
| 58 | + parser = create_parser() |
| 59 | + args = parser.parse_args(["sleep", "feature"]) |
| 60 | + assert args.command == "sleep" |
| 61 | + assert args.space == "feature" |
| 62 | + |
| 63 | + |
| 64 | +def test_parser_rename_command(): |
| 65 | + """Test rename command.""" |
| 66 | + parser = create_parser() |
| 67 | + args = parser.parse_args(["rename", "old", "new"]) |
| 68 | + assert args.command == "rename" |
| 69 | + assert args.old_name == "old" |
| 70 | + assert args.new_name == "new" |
| 71 | + |
| 72 | + |
| 73 | +def test_parser_code_command(): |
| 74 | + """Test code command.""" |
| 75 | + parser = create_parser() |
| 76 | + args = parser.parse_args(["code", "main"]) |
| 77 | + assert args.command == "code" |
| 78 | + assert args.space == "main" |
| 79 | + |
| 80 | + |
| 81 | +def test_parser_config_command(): |
| 82 | + """Test config command.""" |
| 83 | + parser = create_parser() |
| 84 | + args = parser.parse_args(["config", "key", "value"]) |
| 85 | + assert args.command == "config" |
| 86 | + assert args.key == "key" |
| 87 | + assert args.value == "value" |
| 88 | + |
| 89 | + |
| 90 | +def test_parser_extend_command(): |
| 91 | + """Test extend command.""" |
| 92 | + parser = create_parser() |
| 93 | + args = parser.parse_args(["extend", "-n", "2", "main"]) |
| 94 | + assert args.command == "extend" |
| 95 | + assert args.num_spaces == 2 |
| 96 | + assert args.space == "main" |
| 97 | + |
| 98 | + |
| 99 | +def test_parser_setup_command(): |
| 100 | + """Test setup command.""" |
| 101 | + parser = create_parser() |
| 102 | + args = parser.parse_args(["setup"]) |
| 103 | + assert args.command == "setup" |
| 104 | + |
| 105 | + |
| 106 | +@patch("gitspaces.cli.init_config") |
| 107 | +@patch("gitspaces.cli.run_user_environment_checks") |
| 108 | +@patch("gitspaces.cli.Console") |
| 109 | +def test_main_with_debug(mock_console, mock_checks, mock_init, monkeypatch): |
| 110 | + """Test main with debug flag.""" |
| 111 | + mock_checks.return_value = True |
| 112 | + monkeypatch.setattr(sys, "argv", ["gitspaces", "--debug", "setup"]) |
| 113 | + |
| 114 | + mock_func = Mock() |
| 115 | + with patch("gitspaces.cli.create_parser") as mock_parser: |
| 116 | + parser = MagicMock() |
| 117 | + args = MagicMock() |
| 118 | + args.debug = True |
| 119 | + args.command = "setup" |
| 120 | + args.func = mock_func |
| 121 | + parser.parse_args.return_value = args |
| 122 | + mock_parser.return_value = parser |
| 123 | + |
| 124 | + main() |
| 125 | + |
| 126 | + mock_console.println.assert_any_call(f"Args: {sys.argv}") |
| 127 | + mock_func.assert_called_once() |
| 128 | + |
| 129 | + |
| 130 | +@patch("gitspaces.cli.init_config") |
| 131 | +@patch("gitspaces.cli.run_user_environment_checks") |
| 132 | +def test_main_config_error(mock_checks, mock_init, monkeypatch, capsys): |
| 133 | + """Test main when config initialization fails.""" |
| 134 | + mock_init.side_effect = Exception("Config error") |
| 135 | + monkeypatch.setattr(sys, "argv", ["gitspaces", "setup"]) |
| 136 | + |
| 137 | + with pytest.raises(SystemExit) as exc_info: |
| 138 | + main() |
| 139 | + |
| 140 | + assert exc_info.value.code == 1 |
| 141 | + |
| 142 | + |
| 143 | +@patch("gitspaces.cli.init_config") |
| 144 | +@patch("gitspaces.cli.run_user_environment_checks") |
| 145 | +def test_main_environment_check_fails(mock_checks, mock_init, monkeypatch): |
| 146 | + """Test main when environment checks fail.""" |
| 147 | + mock_checks.return_value = False |
| 148 | + monkeypatch.setattr(sys, "argv", ["gitspaces", "setup"]) |
| 149 | + |
| 150 | + with pytest.raises(SystemExit) as exc_info: |
| 151 | + main() |
| 152 | + |
| 153 | + assert exc_info.value.code == 1 |
| 154 | + |
| 155 | + |
| 156 | +@patch("gitspaces.cli.init_config") |
| 157 | +@patch("gitspaces.cli.run_user_environment_checks") |
| 158 | +@patch("gitspaces.modules.cmd_switch.switch_command") |
| 159 | +def test_main_no_command_defaults_to_switch( |
| 160 | + mock_switch, mock_checks, mock_init, monkeypatch |
| 161 | +): |
| 162 | + """Test main defaults to switch when no command provided.""" |
| 163 | + mock_checks.return_value = True |
| 164 | + monkeypatch.setattr(sys, "argv", ["gitspaces"]) |
| 165 | + |
| 166 | + main() |
| 167 | + |
| 168 | + mock_switch.assert_called_once() |
| 169 | + |
| 170 | + |
| 171 | +@patch("gitspaces.cli.init_config") |
| 172 | +@patch("gitspaces.cli.run_user_environment_checks") |
| 173 | +def test_main_keyboard_interrupt(mock_checks, mock_init, monkeypatch): |
| 174 | + """Test main handles keyboard interrupt.""" |
| 175 | + mock_checks.return_value = True |
| 176 | + monkeypatch.setattr(sys, "argv", ["gitspaces", "setup"]) |
| 177 | + |
| 178 | + mock_func = Mock(side_effect=KeyboardInterrupt()) |
| 179 | + with patch("gitspaces.cli.create_parser") as mock_parser: |
| 180 | + parser = MagicMock() |
| 181 | + args = MagicMock() |
| 182 | + args.debug = False |
| 183 | + args.command = "setup" |
| 184 | + args.func = mock_func |
| 185 | + parser.parse_args.return_value = args |
| 186 | + mock_parser.return_value = parser |
| 187 | + |
| 188 | + with pytest.raises(SystemExit) as exc_info: |
| 189 | + main() |
| 190 | + |
| 191 | + assert exc_info.value.code == 1 |
| 192 | + |
| 193 | + |
| 194 | +@patch("gitspaces.cli.init_config") |
| 195 | +@patch("gitspaces.cli.run_user_environment_checks") |
| 196 | +def test_main_user_aborted(mock_checks, mock_init, monkeypatch): |
| 197 | + """Test main handles user abort exception.""" |
| 198 | + mock_checks.return_value = True |
| 199 | + monkeypatch.setattr(sys, "argv", ["gitspaces", "setup"]) |
| 200 | + |
| 201 | + mock_func = Mock(side_effect=Exception("user aborted")) |
| 202 | + with patch("gitspaces.cli.create_parser") as mock_parser: |
| 203 | + parser = MagicMock() |
| 204 | + args = MagicMock() |
| 205 | + args.debug = False |
| 206 | + args.command = "setup" |
| 207 | + args.func = mock_func |
| 208 | + parser.parse_args.return_value = args |
| 209 | + mock_parser.return_value = parser |
| 210 | + |
| 211 | + # User aborted doesn't raise SystemExit, just returns |
| 212 | + main() |
| 213 | + |
| 214 | + |
| 215 | +@patch("gitspaces.cli.init_config") |
| 216 | +@patch("gitspaces.cli.run_user_environment_checks") |
| 217 | +def test_main_general_exception(mock_checks, mock_init, monkeypatch): |
| 218 | + """Test main handles general exceptions.""" |
| 219 | + mock_checks.return_value = True |
| 220 | + monkeypatch.setattr(sys, "argv", ["gitspaces", "setup"]) |
| 221 | + |
| 222 | + mock_func = Mock(side_effect=Exception("Something went wrong")) |
| 223 | + with patch("gitspaces.cli.create_parser") as mock_parser: |
| 224 | + parser = MagicMock() |
| 225 | + args = MagicMock() |
| 226 | + args.debug = False |
| 227 | + args.command = "setup" |
| 228 | + args.func = mock_func |
| 229 | + parser.parse_args.return_value = args |
| 230 | + mock_parser.return_value = parser |
| 231 | + |
| 232 | + with pytest.raises(SystemExit) as exc_info: |
| 233 | + main() |
| 234 | + |
| 235 | + assert exc_info.value.code == 1 |
0 commit comments