|
| 1 | +""" |
| 2 | +Steps for testing -f/--file option behavioral tests. |
| 3 | +Reuses common steps from command_option.py |
| 4 | +""" |
| 5 | + |
| 6 | +import subprocess |
| 7 | +import tempfile |
| 8 | +import os |
| 9 | +from behave import when |
| 10 | + |
| 11 | + |
| 12 | +@when('we create a file with "{content}"') |
| 13 | +def step_create_file_with_content(context, content): |
| 14 | + """Create a temporary file with the given content.""" |
| 15 | + # Create a temporary file that will be cleaned up automatically |
| 16 | + temp_file = tempfile.NamedTemporaryFile( |
| 17 | + mode='w', |
| 18 | + delete=False, |
| 19 | + suffix='.sql' |
| 20 | + ) |
| 21 | + temp_file.write(content) |
| 22 | + temp_file.close() |
| 23 | + context.temp_file_path = temp_file.name |
| 24 | + |
| 25 | + |
| 26 | +@when('we run pgcli with -f and the file') |
| 27 | +def step_run_pgcli_with_f(context): |
| 28 | + """Run pgcli with -f flag and the temporary file.""" |
| 29 | + cmd = [ |
| 30 | + "pgcli", |
| 31 | + "-h", context.conf["host"], |
| 32 | + "-p", str(context.conf["port"]), |
| 33 | + "-U", context.conf["user"], |
| 34 | + "-d", context.conf["dbname"], |
| 35 | + "-f", context.temp_file_path |
| 36 | + ] |
| 37 | + try: |
| 38 | + context.cmd_output = subprocess.check_output( |
| 39 | + cmd, |
| 40 | + cwd=context.package_root, |
| 41 | + stderr=subprocess.STDOUT, |
| 42 | + timeout=5 |
| 43 | + ) |
| 44 | + context.exit_code = 0 |
| 45 | + except subprocess.CalledProcessError as e: |
| 46 | + context.cmd_output = e.output |
| 47 | + context.exit_code = e.returncode |
| 48 | + except subprocess.TimeoutExpired as e: |
| 49 | + context.cmd_output = b"Command timed out" |
| 50 | + context.exit_code = -1 |
| 51 | + finally: |
| 52 | + # Clean up the temporary file |
| 53 | + if hasattr(context, 'temp_file_path') and os.path.exists(context.temp_file_path): |
| 54 | + os.unlink(context.temp_file_path) |
| 55 | + |
| 56 | + |
| 57 | +@when('we run pgcli with --file and the file') |
| 58 | +def step_run_pgcli_with_file(context): |
| 59 | + """Run pgcli with --file flag and the temporary file.""" |
| 60 | + cmd = [ |
| 61 | + "pgcli", |
| 62 | + "-h", context.conf["host"], |
| 63 | + "-p", str(context.conf["port"]), |
| 64 | + "-U", context.conf["user"], |
| 65 | + "-d", context.conf["dbname"], |
| 66 | + "--file", context.temp_file_path |
| 67 | + ] |
| 68 | + try: |
| 69 | + context.cmd_output = subprocess.check_output( |
| 70 | + cmd, |
| 71 | + cwd=context.package_root, |
| 72 | + stderr=subprocess.STDOUT, |
| 73 | + timeout=5 |
| 74 | + ) |
| 75 | + context.exit_code = 0 |
| 76 | + except subprocess.CalledProcessError as e: |
| 77 | + context.cmd_output = e.output |
| 78 | + context.exit_code = e.returncode |
| 79 | + except subprocess.TimeoutExpired as e: |
| 80 | + context.cmd_output = b"Command timed out" |
| 81 | + context.exit_code = -1 |
| 82 | + finally: |
| 83 | + # Clean up the temporary file |
| 84 | + if hasattr(context, 'temp_file_path') and os.path.exists(context.temp_file_path): |
| 85 | + os.unlink(context.temp_file_path) |
| 86 | + |
| 87 | + |
| 88 | +@when('we run pgcli with -c "{command}" and -f with the file') |
| 89 | +def step_run_pgcli_with_c_and_f(context, command): |
| 90 | + """Run pgcli with both -c and -f flags.""" |
| 91 | + cmd = [ |
| 92 | + "pgcli", |
| 93 | + "-h", context.conf["host"], |
| 94 | + "-p", str(context.conf["port"]), |
| 95 | + "-U", context.conf["user"], |
| 96 | + "-d", context.conf["dbname"], |
| 97 | + "-c", command, |
| 98 | + "-f", context.temp_file_path |
| 99 | + ] |
| 100 | + try: |
| 101 | + context.cmd_output = subprocess.check_output( |
| 102 | + cmd, |
| 103 | + cwd=context.package_root, |
| 104 | + stderr=subprocess.STDOUT, |
| 105 | + timeout=5 |
| 106 | + ) |
| 107 | + context.exit_code = 0 |
| 108 | + except subprocess.CalledProcessError as e: |
| 109 | + context.cmd_output = e.output |
| 110 | + context.exit_code = e.returncode |
| 111 | + except subprocess.TimeoutExpired as e: |
| 112 | + context.cmd_output = b"Command timed out" |
| 113 | + context.exit_code = -1 |
| 114 | + finally: |
| 115 | + # Clean up the temporary file |
| 116 | + if hasattr(context, 'temp_file_path') and os.path.exists(context.temp_file_path): |
| 117 | + os.unlink(context.temp_file_path) |
0 commit comments