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