|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any, cast |
| 5 | + |
| 6 | +import mycli.main_modes.list_ssh_config as list_ssh_config_mode |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class DummyCliArgs: |
| 11 | + ssh_config_path: str = 'ssh_config' |
| 12 | + verbose: bool = False |
| 13 | + |
| 14 | + |
| 15 | +class DummySSHConfig: |
| 16 | + def __init__(self, hostnames: list[str] | Exception, lookups: dict[str, dict[str, str]] | None = None) -> None: |
| 17 | + self.hostnames = hostnames |
| 18 | + self.lookups = lookups or {} |
| 19 | + |
| 20 | + def get_hostnames(self) -> list[str]: |
| 21 | + if isinstance(self.hostnames, Exception): |
| 22 | + raise self.hostnames |
| 23 | + return self.hostnames |
| 24 | + |
| 25 | + def lookup(self, hostname: str) -> dict[str, str]: |
| 26 | + return self.lookups[hostname] |
| 27 | + |
| 28 | + |
| 29 | +def main_list_ssh_config(cli_args: DummyCliArgs) -> int: |
| 30 | + return list_ssh_config_mode.main_list_ssh_config(cast(Any, object()), cast(Any, cli_args)) |
| 31 | + |
| 32 | + |
| 33 | +def test_main_list_ssh_config_lists_hostnames(monkeypatch) -> None: |
| 34 | + secho_calls: list[tuple[str, bool | None, str | None]] = [] |
| 35 | + ssh_config = DummySSHConfig(['prod', 'staging']) |
| 36 | + |
| 37 | + monkeypatch.setattr(list_ssh_config_mode, 'read_ssh_config', lambda _path: ssh_config) |
| 38 | + monkeypatch.setattr( |
| 39 | + list_ssh_config_mode.click, |
| 40 | + 'secho', |
| 41 | + lambda message, err=None, fg=None: secho_calls.append((message, err, fg)), |
| 42 | + ) |
| 43 | + |
| 44 | + result = main_list_ssh_config(DummyCliArgs(verbose=False)) |
| 45 | + |
| 46 | + assert result == 0 |
| 47 | + assert secho_calls == [ |
| 48 | + ('prod', None, None), |
| 49 | + ('staging', None, None), |
| 50 | + ] |
| 51 | + |
| 52 | + |
| 53 | +def test_main_list_ssh_config_lists_verbose_host_details(monkeypatch) -> None: |
| 54 | + secho_calls: list[tuple[str, bool | None, str | None]] = [] |
| 55 | + ssh_config = DummySSHConfig( |
| 56 | + ['prod'], |
| 57 | + lookups={'prod': {'hostname': 'db.example.com'}}, |
| 58 | + ) |
| 59 | + |
| 60 | + monkeypatch.setattr(list_ssh_config_mode, 'read_ssh_config', lambda _path: ssh_config) |
| 61 | + monkeypatch.setattr( |
| 62 | + list_ssh_config_mode.click, |
| 63 | + 'secho', |
| 64 | + lambda message, err=None, fg=None: secho_calls.append((message, err, fg)), |
| 65 | + ) |
| 66 | + |
| 67 | + result = main_list_ssh_config(DummyCliArgs(verbose=True)) |
| 68 | + |
| 69 | + assert result == 0 |
| 70 | + assert secho_calls == [('prod : db.example.com', None, None)] |
| 71 | + |
| 72 | + |
| 73 | +def test_main_list_ssh_config_reports_host_lookup_errors(monkeypatch) -> None: |
| 74 | + secho_calls: list[tuple[str, bool | None, str | None]] = [] |
| 75 | + ssh_config = DummySSHConfig(KeyError('bad ssh config')) |
| 76 | + |
| 77 | + monkeypatch.setattr(list_ssh_config_mode, 'read_ssh_config', lambda _path: ssh_config) |
| 78 | + monkeypatch.setattr( |
| 79 | + list_ssh_config_mode.click, |
| 80 | + 'secho', |
| 81 | + lambda message, err=None, fg=None: secho_calls.append((message, err, fg)), |
| 82 | + ) |
| 83 | + |
| 84 | + result = main_list_ssh_config(DummyCliArgs()) |
| 85 | + |
| 86 | + assert result == 1 |
| 87 | + assert secho_calls == [('Error reading ssh config', True, 'red')] |
0 commit comments