|
| 1 | +import pytest |
| 2 | +from click.testing import CliRunner |
| 3 | + |
| 4 | +from pgcli.main import cli, PGCli |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def dummy_exec(monkeypatch, tmp_path): |
| 9 | + # Capture executed commands |
| 10 | + # Isolate config directory for tests |
| 11 | + monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path)) |
| 12 | + dummy_cmds = [] |
| 13 | + |
| 14 | + class DummyExec: |
| 15 | + def run(self, cmd): |
| 16 | + # Ignore ping SELECT 1 commands used for exiting CLI |
| 17 | + if cmd.strip().upper() == "SELECT 1": |
| 18 | + return [] |
| 19 | + # Record init commands |
| 20 | + dummy_cmds.append(cmd) |
| 21 | + return [] |
| 22 | + |
| 23 | + def get_timezone(self): |
| 24 | + return "UTC" |
| 25 | + |
| 26 | + def set_timezone(self, *args, **kwargs): |
| 27 | + pass |
| 28 | + |
| 29 | + def fake_connect(self, *args, **kwargs): |
| 30 | + self.pgexecute = DummyExec() |
| 31 | + |
| 32 | + monkeypatch.setattr(PGCli, "connect", fake_connect) |
| 33 | + return dummy_cmds |
| 34 | + |
| 35 | + |
| 36 | +def test_init_command_option(dummy_exec): |
| 37 | + "Test that --init-command triggers execution of the command." |
| 38 | + runner = CliRunner() |
| 39 | + # Use a custom init command and --ping to exit the CLI after init commands |
| 40 | + result = runner.invoke( |
| 41 | + cli, ["--init-command", "SELECT foo", "--ping", "db", "user"] |
| 42 | + ) |
| 43 | + assert result.exit_code == 0 |
| 44 | + # Should print the init command |
| 45 | + assert "Running init commands: SELECT foo" in result.output |
| 46 | + # Should exit via ping |
| 47 | + assert "PONG" in result.output |
| 48 | + # DummyExec should have recorded only the init command |
| 49 | + assert dummy_exec == ["SELECT foo"] |
| 50 | + |
| 51 | + |
| 52 | +def test_init_commands_from_config(dummy_exec, tmp_path): |
| 53 | + """ |
| 54 | + Test that init commands defined in the config file are executed on startup. |
| 55 | + """ |
| 56 | + # Create a temporary config file with init-commands |
| 57 | + config_file = tmp_path / "pgclirc_test" |
| 58 | + config_file.write_text( |
| 59 | + "[main]\n[init-commands]\nfirst = SELECT foo;\nsecond = SELECT bar;\n" |
| 60 | + ) |
| 61 | + |
| 62 | + runner = CliRunner() |
| 63 | + # Use --ping to exit the CLI after init commands |
| 64 | + result = runner.invoke( |
| 65 | + cli, ["--pgclirc", str(config_file.absolute()), "--ping", "testdb", "user"] |
| 66 | + ) |
| 67 | + assert result.exit_code == 0 |
| 68 | + # Should print both init commands in order (note trailing semicolons cause double ';;') |
| 69 | + assert "Running init commands: SELECT foo;; SELECT bar;" in result.output |
| 70 | + # DummyExec should have recorded both commands |
| 71 | + assert dummy_exec == ["SELECT foo;", "SELECT bar;"] |
| 72 | + |
| 73 | + |
| 74 | +def test_init_commands_option_and_config(dummy_exec, tmp_path): |
| 75 | + """ |
| 76 | + Test that CLI-provided init command is appended after config-defined commands. |
| 77 | + """ |
| 78 | + # Create a temporary config file with init-commands |
| 79 | + config_file = tmp_path / "pgclirc_test" |
| 80 | + config_file.write_text("[main]\n [init-commands]\nfirst = SELECT foo;\n") |
| 81 | + |
| 82 | + runner = CliRunner() |
| 83 | + # Use --ping to exit the CLI after init commands |
| 84 | + result = runner.invoke( |
| 85 | + cli, |
| 86 | + [ |
| 87 | + "--pgclirc", |
| 88 | + str(config_file), |
| 89 | + "--init-command", |
| 90 | + "SELECT baz;", |
| 91 | + "--ping", |
| 92 | + "testdb", |
| 93 | + "user", |
| 94 | + ], |
| 95 | + ) |
| 96 | + assert result.exit_code == 0 |
| 97 | + # Should print config command followed by CLI option (double ';' between commands) |
| 98 | + assert "Running init commands: SELECT foo;; SELECT baz;" in result.output |
| 99 | + # DummyExec should record both commands in order |
| 100 | + assert dummy_exec == ["SELECT foo;", "SELECT baz;"] |
0 commit comments