-
Notifications
You must be signed in to change notification settings - Fork 602
Add support for -c/--command option to execute commands and exit #1542
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
Open
DiegoDAF
wants to merge
4
commits into
dbcli:main
Choose a base branch
from
DiegoDAF:feature/command-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+272
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4fb7b2d
Add support for multiple -c/--command parameters
DiegoDAF 19c9730
Merge branch 'main' into feature/command-option
diego-feito-stori 8bef1a3
Assert on the \dt output in the command-output step
DiegoDAF aa831d0
Merge remote-tracking branch 'original/main' into feature/command-option
DiegoDAF 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| Feature: run the cli with -c/--command option, | ||
| execute a single command, | ||
| and exit | ||
|
|
||
| Scenario: run pgcli with -c and a SQL query | ||
| When we run pgcli with -c "SELECT 1 as test_column" | ||
| then we see the query result | ||
| and pgcli exits successfully | ||
|
|
||
| Scenario: run pgcli with --command and a SQL query | ||
| When we run pgcli with --command "SELECT 'hello' as greeting" | ||
| then we see the query result | ||
| and pgcli exits successfully | ||
|
|
||
| Scenario: run pgcli with -c and a special command | ||
| When we run pgcli with -c "\dt" | ||
| then we see the command output | ||
| and pgcli exits successfully | ||
|
|
||
| Scenario: run pgcli with -c and an invalid query | ||
| When we run pgcli with -c "SELECT invalid_column FROM nonexistent_table" | ||
| then we see an error message | ||
| and pgcli exits successfully | ||
|
|
||
| Scenario: run pgcli with -c and multiple statements | ||
| When we run pgcli with -c "SELECT 1; SELECT 2" | ||
| then we see both query results | ||
| and pgcli exits successfully | ||
|
|
||
| Scenario: run pgcli with multiple -c options | ||
| When we run pgcli with multiple -c options | ||
| then we see all command outputs | ||
| and pgcli exits successfully | ||
|
|
||
| Scenario: run pgcli with mixed -c and --command options | ||
| When we run pgcli with mixed -c and --command | ||
| then we see all command outputs | ||
| and pgcli exits successfully |
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,198 @@ | ||
| """ | ||
| Steps for testing -c/--command option behavioral tests. | ||
| """ | ||
|
|
||
| import subprocess | ||
| from behave import when, then | ||
|
|
||
|
|
||
| @when('we run pgcli with -c "{command}"') | ||
| def step_run_pgcli_with_c(context, command): | ||
| """Run pgcli with -c flag and a command.""" | ||
| cmd = [ | ||
| "pgcli", | ||
| "-h", | ||
| context.conf["host"], | ||
| "-p", | ||
| str(context.conf["port"]), | ||
| "-U", | ||
| context.conf["user"], | ||
| "-d", | ||
| context.conf["dbname"], | ||
| "-c", | ||
| command, | ||
| ] | ||
| try: | ||
| context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=5) | ||
| context.exit_code = 0 | ||
| except subprocess.CalledProcessError as e: | ||
| context.cmd_output = e.output | ||
| context.exit_code = e.returncode | ||
| except subprocess.TimeoutExpired: | ||
| context.cmd_output = b"Command timed out" | ||
| context.exit_code = -1 | ||
|
|
||
|
|
||
| @when('we run pgcli with --command "{command}"') | ||
| def step_run_pgcli_with_command(context, command): | ||
| """Run pgcli with --command flag and a command.""" | ||
| cmd = [ | ||
| "pgcli", | ||
| "-h", | ||
| context.conf["host"], | ||
| "-p", | ||
| str(context.conf["port"]), | ||
| "-U", | ||
| context.conf["user"], | ||
| "-d", | ||
| context.conf["dbname"], | ||
| "--command", | ||
| command, | ||
| ] | ||
| try: | ||
| context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=5) | ||
| context.exit_code = 0 | ||
| except subprocess.CalledProcessError as e: | ||
| context.cmd_output = e.output | ||
| context.exit_code = e.returncode | ||
| except subprocess.TimeoutExpired: | ||
| context.cmd_output = b"Command timed out" | ||
| context.exit_code = -1 | ||
|
|
||
|
|
||
| @then("we see the query result") | ||
| def step_see_query_result(context): | ||
| """Verify that the query result is in the output.""" | ||
| output = context.cmd_output.decode('utf-8') | ||
| # Check for common query result indicators | ||
| assert any([ | ||
| "SELECT" in output, | ||
| "test_column" in output, | ||
| "greeting" in output, | ||
| "hello" in output, | ||
| "+-" in output, # table border | ||
| "|" in output, # table column separator | ||
| ]), f"Expected query result in output, but got: {output}" | ||
|
|
||
|
|
||
| @then("we see both query results") | ||
| def step_see_both_query_results(context): | ||
| """Verify that both query results are in the output.""" | ||
| output = context.cmd_output.decode('utf-8') | ||
| # Should contain output from both SELECT statements | ||
| assert "SELECT" in output, f"Expected SELECT in output, but got: {output}" | ||
| # The output should have multiple result sets | ||
| assert output.count("SELECT") >= 2, f"Expected at least 2 SELECT results, but got: {output}" | ||
|
|
||
|
|
||
| @then("we see the command output") | ||
| def step_see_command_output(context): | ||
| """Verify that the special command output is present.""" | ||
| output = context.cmd_output.decode('utf-8') | ||
| # `\dt` renders its column headers whether or not any tables exist, so the | ||
| # headers are what tells us the special command actually ran and produced | ||
| # its listing (rather than erroring out). | ||
| for header in ("Schema", "Name", "Type", "Owner"): | ||
| assert header in output, f"Expected {header!r} in \\dt output, but got: {output}" | ||
| assert context.exit_code == 0, f"Expected exit code 0, but got: {context.exit_code}" | ||
|
|
||
|
|
||
| @then("we see an error message") | ||
| def step_see_error_message(context): | ||
| """Verify that an error message is in the output.""" | ||
| output = context.cmd_output.decode('utf-8') | ||
| assert any([ | ||
| "does not exist" in output, | ||
| "error" in output.lower(), | ||
| "ERROR" in output, | ||
| ]), f"Expected error message in output, but got: {output}" | ||
|
|
||
|
|
||
| @then("pgcli exits successfully") | ||
| def step_pgcli_exits_successfully(context): | ||
| """Verify that pgcli exited with code 0.""" | ||
| assert context.exit_code == 0, f"Expected exit code 0, but got: {context.exit_code}" | ||
| # Clean up | ||
| context.cmd_output = None | ||
| context.exit_code = None | ||
|
|
||
|
|
||
| @then("pgcli exits with error") | ||
| def step_pgcli_exits_with_error(context): | ||
| """Verify that pgcli exited with a non-zero code.""" | ||
| assert context.exit_code != 0, f"Expected non-zero exit code, but got: {context.exit_code}" | ||
| # Clean up | ||
| context.cmd_output = None | ||
| context.exit_code = None | ||
|
|
||
|
|
||
| @when("we run pgcli with multiple -c options") | ||
| def step_run_pgcli_with_multiple_c(context): | ||
| """Run pgcli with multiple -c flags.""" | ||
| cmd = [ | ||
| "pgcli", | ||
| "-h", | ||
| context.conf["host"], | ||
| "-p", | ||
| str(context.conf["port"]), | ||
| "-U", | ||
| context.conf["user"], | ||
| "-d", | ||
| context.conf["dbname"], | ||
| "-c", | ||
| "SELECT 'first' as result", | ||
| "-c", | ||
| "SELECT 'second' as result", | ||
| "-c", | ||
| "SELECT 'third' as result", | ||
| ] | ||
| try: | ||
| context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=10) | ||
| context.exit_code = 0 | ||
| except subprocess.CalledProcessError as e: | ||
| context.cmd_output = e.output | ||
| context.exit_code = e.returncode | ||
| except subprocess.TimeoutExpired: | ||
| context.cmd_output = b"Command timed out" | ||
| context.exit_code = -1 | ||
|
|
||
|
|
||
| @when("we run pgcli with mixed -c and --command") | ||
| def step_run_pgcli_with_mixed_options(context): | ||
| """Run pgcli with mixed -c and --command flags.""" | ||
| cmd = [ | ||
| "pgcli", | ||
| "-h", | ||
| context.conf["host"], | ||
| "-p", | ||
| str(context.conf["port"]), | ||
| "-U", | ||
| context.conf["user"], | ||
| "-d", | ||
| context.conf["dbname"], | ||
| "-c", | ||
| "SELECT 'from_c' as source", | ||
| "--command", | ||
| "SELECT 'from_command' as source", | ||
| ] | ||
| try: | ||
| context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=10) | ||
| context.exit_code = 0 | ||
| except subprocess.CalledProcessError as e: | ||
| context.cmd_output = e.output | ||
| context.exit_code = e.returncode | ||
| except subprocess.TimeoutExpired: | ||
| context.cmd_output = b"Command timed out" | ||
| context.exit_code = -1 | ||
|
|
||
|
|
||
| @then("we see all command outputs") | ||
| def step_see_all_command_outputs(context): | ||
| """Verify that all command outputs are present.""" | ||
| output = context.cmd_output.decode('utf-8') | ||
| # Should contain output from all commands | ||
| assert "first" in output or "from_c" in output, f"Expected 'first' or 'from_c' in output, but got: {output}" | ||
| assert "second" in output or "from_command" in output, f"Expected 'second' or 'from_command' in output, but got: {output}" | ||
| # For the 3-command test, also check for third | ||
| if "third" in output or "result" in output: | ||
| assert "third" in output, f"Expected 'third' in output for 3-command test, but got: {output}" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.