|
| 1 | +"""Steps for the -c/--command option. |
| 2 | +
|
| 3 | +The steps shared with the -f/--file scenarios (query result, command output, |
| 4 | +error message, exit status) are defined once in file_option.py; behave's step |
| 5 | +registry is global, so redefining them here would raise AmbiguousStep. |
| 6 | +""" |
| 7 | + |
| 8 | +import subprocess |
| 9 | +from behave import when, then |
| 10 | + |
| 11 | + |
| 12 | +@when('we run pgcli with -c "{command}"') |
| 13 | +def step_run_pgcli_with_c(context, command): |
| 14 | + """Run pgcli with -c flag and a command.""" |
| 15 | + cmd = [ |
| 16 | + "pgcli", |
| 17 | + "-h", |
| 18 | + context.conf["host"], |
| 19 | + "-p", |
| 20 | + str(context.conf["port"]), |
| 21 | + "-U", |
| 22 | + context.conf["user"], |
| 23 | + "-d", |
| 24 | + context.conf["dbname"], |
| 25 | + "-c", |
| 26 | + command, |
| 27 | + ] |
| 28 | + try: |
| 29 | + context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=5) |
| 30 | + context.exit_code = 0 |
| 31 | + except subprocess.CalledProcessError as e: |
| 32 | + context.cmd_output = e.output |
| 33 | + context.exit_code = e.returncode |
| 34 | + except subprocess.TimeoutExpired: |
| 35 | + context.cmd_output = b"Command timed out" |
| 36 | + context.exit_code = -1 |
| 37 | + |
| 38 | + |
| 39 | +@when('we run pgcli with --command "{command}"') |
| 40 | +def step_run_pgcli_with_command(context, command): |
| 41 | + """Run pgcli with --command flag and a command.""" |
| 42 | + cmd = [ |
| 43 | + "pgcli", |
| 44 | + "-h", |
| 45 | + context.conf["host"], |
| 46 | + "-p", |
| 47 | + str(context.conf["port"]), |
| 48 | + "-U", |
| 49 | + context.conf["user"], |
| 50 | + "-d", |
| 51 | + context.conf["dbname"], |
| 52 | + "--command", |
| 53 | + command, |
| 54 | + ] |
| 55 | + try: |
| 56 | + context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=5) |
| 57 | + context.exit_code = 0 |
| 58 | + except subprocess.CalledProcessError as e: |
| 59 | + context.cmd_output = e.output |
| 60 | + context.exit_code = e.returncode |
| 61 | + except subprocess.TimeoutExpired: |
| 62 | + context.cmd_output = b"Command timed out" |
| 63 | + context.exit_code = -1 |
| 64 | + |
| 65 | + |
| 66 | +@then("pgcli exits with error") |
| 67 | +def step_pgcli_exits_with_error(context): |
| 68 | + """Verify that pgcli exited with a non-zero code.""" |
| 69 | + assert context.exit_code != 0, f"Expected non-zero exit code, but got: {context.exit_code}" |
| 70 | + # Clean up |
| 71 | + context.cmd_output = None |
| 72 | + context.exit_code = None |
| 73 | + |
| 74 | + |
| 75 | +@when("we run pgcli with multiple -c options") |
| 76 | +def step_run_pgcli_with_multiple_c(context): |
| 77 | + """Run pgcli with multiple -c flags.""" |
| 78 | + cmd = [ |
| 79 | + "pgcli", |
| 80 | + "-h", |
| 81 | + context.conf["host"], |
| 82 | + "-p", |
| 83 | + str(context.conf["port"]), |
| 84 | + "-U", |
| 85 | + context.conf["user"], |
| 86 | + "-d", |
| 87 | + context.conf["dbname"], |
| 88 | + "-c", |
| 89 | + "SELECT 'first' as result", |
| 90 | + "-c", |
| 91 | + "SELECT 'second' as result", |
| 92 | + "-c", |
| 93 | + "SELECT 'third' as result", |
| 94 | + ] |
| 95 | + try: |
| 96 | + context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=10) |
| 97 | + context.exit_code = 0 |
| 98 | + except subprocess.CalledProcessError as e: |
| 99 | + context.cmd_output = e.output |
| 100 | + context.exit_code = e.returncode |
| 101 | + except subprocess.TimeoutExpired: |
| 102 | + context.cmd_output = b"Command timed out" |
| 103 | + context.exit_code = -1 |
| 104 | + |
| 105 | + |
| 106 | +@when("we run pgcli with mixed -c and --command") |
| 107 | +def step_run_pgcli_with_mixed_options(context): |
| 108 | + """Run pgcli with mixed -c and --command flags.""" |
| 109 | + cmd = [ |
| 110 | + "pgcli", |
| 111 | + "-h", |
| 112 | + context.conf["host"], |
| 113 | + "-p", |
| 114 | + str(context.conf["port"]), |
| 115 | + "-U", |
| 116 | + context.conf["user"], |
| 117 | + "-d", |
| 118 | + context.conf["dbname"], |
| 119 | + "-c", |
| 120 | + "SELECT 'from_c' as source", |
| 121 | + "--command", |
| 122 | + "SELECT 'from_command' as source", |
| 123 | + ] |
| 124 | + try: |
| 125 | + context.cmd_output = subprocess.check_output(cmd, cwd=context.package_root, stderr=subprocess.STDOUT, timeout=10) |
| 126 | + context.exit_code = 0 |
| 127 | + except subprocess.CalledProcessError as e: |
| 128 | + context.cmd_output = e.output |
| 129 | + context.exit_code = e.returncode |
| 130 | + except subprocess.TimeoutExpired: |
| 131 | + context.cmd_output = b"Command timed out" |
| 132 | + context.exit_code = -1 |
| 133 | + |
| 134 | + |
| 135 | +@then("we see all command outputs") |
| 136 | +def step_see_all_command_outputs(context): |
| 137 | + """Verify that all command outputs are present.""" |
| 138 | + output = context.cmd_output.decode('utf-8') |
| 139 | + # Should contain output from all commands |
| 140 | + assert "first" in output or "from_c" in output, f"Expected 'first' or 'from_c' in output, but got: {output}" |
| 141 | + assert "second" in output or "from_command" in output, f"Expected 'second' or 'from_command' in output, but got: {output}" |
| 142 | + # For the 3-command test, also check for third |
| 143 | + if "third" in output or "result" in output: |
| 144 | + assert "third" in output, f"Expected 'third' in output for 3-command test, but got: {output}" |
0 commit comments