(pyfile)
| 17 | |
| 18 | @pytest.fixture |
| 19 | def cli(pyfile): |
| 20 | @pyfile |
| 21 | def cli_parser(): |
| 22 | import os |
| 23 | import pickle |
| 24 | import sys |
| 25 | from debugpy.server import cli |
| 26 | |
| 27 | try: |
| 28 | cli.parse_args() |
| 29 | except Exception as exc: |
| 30 | os.write(1, pickle.dumps(exc)) |
| 31 | sys.exit(1) |
| 32 | |
| 33 | # Check that sys.argv has the correct type after parsing - there should be no bytes. |
| 34 | assert all(isinstance(s, str) for s in sys.argv) |
| 35 | |
| 36 | # We only care about options that correspond to public switches. |
| 37 | options = { |
| 38 | name: getattr(cli.options, name) |
| 39 | for name in [ |
| 40 | "address", |
| 41 | "config", |
| 42 | "log_to", |
| 43 | "log_to_stderr", |
| 44 | "mode", |
| 45 | "target", |
| 46 | "target_kind", |
| 47 | "wait_for_client", |
| 48 | "parent_session_pid", |
| 49 | ] |
| 50 | } |
| 51 | |
| 52 | # Serialize the command line args and the options to stdout |
| 53 | serialized_data = pickle.dumps([sys.argv[1:], options]) |
| 54 | os.write(1, serialized_data) |
| 55 | # Ensure all data is written before process exits |
| 56 | sys.stdout.flush() |
| 57 | |
| 58 | def parse(args): |
| 59 | log.debug("Parsing argv: {0!r}", args) |
| 60 | try: |
| 61 | try: |
| 62 | # Run the CLI parser in a subprocess, and capture its output. |
| 63 | output = subprocess.check_output( |
| 64 | [sys.executable, "-u", cli_parser.strpath] + args, |
| 65 | stderr=subprocess.PIPE # Capture stderr to help with debugging |
| 66 | ) |
| 67 | |
| 68 | # Deserialize the output and return the parsed argv and options. |
| 69 | try: |
| 70 | argv, options = pickle.loads(output) |
| 71 | except Exception as e: |
| 72 | log.debug("Failed to deserialize output: {0}, Output was: {1!r}", e, output) |
| 73 | raise |
| 74 | except subprocess.CalledProcessError as exc: |
| 75 | log.debug("Process exited with code {0}. Output: {1!r}, Error: {2!r}", |
| 76 | exc.returncode, exc.output, exc.stderr) |
no outgoing calls
no test coverage detected