-
Notifications
You must be signed in to change notification settings - Fork 557
Implement init-command similar to mycli. #1504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3775c05
Implement init-command similar to mycli.
amjith c021d65
Add tests for init-command.
amjith c97298c
Update the default pgclirc.
amjith 1023f57
Update tests.
amjith d1ebd5c
Downgrade click version to 8.1.7
amjith 5aa912c
Reenable the behave tests in CI.
amjith a98163e
Fix the behave test expectation about PING/PONG test.
amjith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,8 @@ urls = { Homepage = "https://pgcli.com" } | |
requires-python = ">=3.9" | ||
dependencies = [ | ||
"pgspecial>=2.0.0", | ||
"click >= 4.1", | ||
"Pygments>=2.0", # Pygments has to be Capitalcased. WTF? | ||
"click >= 4.1,<8.1.8", | ||
"Pygments>=2.0", # Pygments has to be Capitalcased. WTF? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I... would not leave a wtf in there. 😂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is from a long time ago. When I was young and stupid. |
||
# We still need to use pt-2 unless pt-3 released on Fedora32 | ||
# see: https://github.com/dbcli/pgcli/pull/1197 | ||
"prompt_toolkit>=2.0.6,<4.0.0", | ||
|
@@ -66,10 +66,7 @@ version = { attr = "pgcli.__version__" } | |
find = { namespaces = false } | ||
|
||
[tool.setuptools.package-data] | ||
pgcli = [ | ||
"pgclirc", | ||
"packages/pgliterals/pgliterals.json", | ||
] | ||
pgcli = ["pgclirc", "packages/pgliterals/pgliterals.json"] | ||
|
||
[tool.black] | ||
line-length = 88 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import pytest | ||
from click.testing import CliRunner | ||
|
||
from pgcli.main import cli, PGCli | ||
|
||
|
||
@pytest.fixture | ||
def dummy_exec(monkeypatch, tmp_path): | ||
# Capture executed commands | ||
# Isolate config directory for tests | ||
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path)) | ||
dummy_cmds = [] | ||
|
||
class DummyExec: | ||
def run(self, cmd): | ||
# Ignore ping SELECT 1 commands used for exiting CLI | ||
if cmd.strip().upper() == "SELECT 1": | ||
return [] | ||
# Record init commands | ||
dummy_cmds.append(cmd) | ||
return [] | ||
|
||
def get_timezone(self): | ||
return "UTC" | ||
|
||
def set_timezone(self, *args, **kwargs): | ||
pass | ||
|
||
def fake_connect(self, *args, **kwargs): | ||
self.pgexecute = DummyExec() | ||
|
||
monkeypatch.setattr(PGCli, "connect", fake_connect) | ||
return dummy_cmds | ||
|
||
|
||
def test_init_command_option(dummy_exec): | ||
"Test that --init-command triggers execution of the command." | ||
runner = CliRunner() | ||
# Use a custom init command and --ping to exit the CLI after init commands | ||
result = runner.invoke( | ||
cli, ["--init-command", "SELECT foo", "--ping", "db", "user"] | ||
) | ||
assert result.exit_code == 0 | ||
# Should print the init command | ||
assert "Running init commands: SELECT foo" in result.output | ||
# Should exit via ping | ||
assert "PONG" in result.output | ||
# DummyExec should have recorded only the init command | ||
assert dummy_exec == ["SELECT foo"] | ||
|
||
|
||
def test_init_commands_from_config(dummy_exec, tmp_path): | ||
""" | ||
Test that init commands defined in the config file are executed on startup. | ||
""" | ||
# Create a temporary config file with init-commands | ||
config_file = tmp_path / "pgclirc_test" | ||
config_file.write_text( | ||
"[main]\n[init-commands]\nfirst = SELECT foo;\nsecond = SELECT bar;\n" | ||
) | ||
|
||
runner = CliRunner() | ||
# Use --ping to exit the CLI after init commands | ||
result = runner.invoke( | ||
cli, ["--pgclirc", str(config_file.absolute()), "--ping", "testdb", "user"] | ||
) | ||
assert result.exit_code == 0 | ||
# Should print both init commands in order (note trailing semicolons cause double ';;') | ||
assert "Running init commands: SELECT foo;; SELECT bar;" in result.output | ||
# DummyExec should have recorded both commands | ||
assert dummy_exec == ["SELECT foo;", "SELECT bar;"] | ||
|
||
|
||
def test_init_commands_option_and_config(dummy_exec, tmp_path): | ||
""" | ||
Test that CLI-provided init command is appended after config-defined commands. | ||
""" | ||
# Create a temporary config file with init-commands | ||
config_file = tmp_path / "pgclirc_test" | ||
config_file.write_text("[main]\n [init-commands]\nfirst = SELECT foo;\n") | ||
|
||
runner = CliRunner() | ||
# Use --ping to exit the CLI after init commands | ||
result = runner.invoke( | ||
cli, | ||
[ | ||
"--pgclirc", | ||
str(config_file), | ||
"--init-command", | ||
"SELECT baz;", | ||
"--ping", | ||
"testdb", | ||
"user", | ||
], | ||
) | ||
assert result.exit_code == 0 | ||
# Should print config command followed by CLI option (double ';' between commands) | ||
assert "Running init commands: SELECT foo;; SELECT baz;" in result.output | ||
# DummyExec should record both commands in order | ||
assert dummy_exec == ["SELECT foo;", "SELECT baz;"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for providing the examples. 👍