(pyfile, tmpdir, target, run, expansion, python_with_space)
| 39 | @pytest.mark.parametrize("expansion", ["preserve", "expand"]) |
| 40 | @pytest.mark.parametrize("python_with_space", [False, True]) |
| 41 | def test_shell_expansion(pyfile, tmpdir, target, run, expansion, python_with_space): |
| 42 | if expansion == "expand" and run.console == "internalConsole": |
| 43 | pytest.skip('Shell expansion is not supported for "internalConsole"') |
| 44 | |
| 45 | # Skip tests with python_with_space=True and target="code" on Windows |
| 46 | # because .cmd wrappers cannot properly handle multiline string arguments |
| 47 | if (python_with_space and target == targets.Code and sys.platform == "win32"): |
| 48 | pytest.skip('Windows .cmd wrapper cannot handle multiline code arguments') |
| 49 | |
| 50 | @pyfile |
| 51 | def code_to_debug(): |
| 52 | import sys |
| 53 | import debuggee |
| 54 | from debuggee import backchannel |
| 55 | |
| 56 | debuggee.setup() |
| 57 | backchannel.send(sys.argv) |
| 58 | |
| 59 | def expand(args): |
| 60 | if expansion != "expand": |
| 61 | return |
| 62 | log.info("Before expansion: {0}", args) |
| 63 | for i, arg in enumerate(args): |
| 64 | if arg.startswith("$"): |
| 65 | args[i] = arg[1:] |
| 66 | log.info("After expansion: {0}", args) |
| 67 | |
| 68 | captured_run_in_terminal_args = [] |
| 69 | |
| 70 | class Session(debug.Session): |
| 71 | def run_in_terminal(self, args, cwd, env): |
| 72 | captured_run_in_terminal_args.append(args[:]) # Capture a copy of the args |
| 73 | expand(args) |
| 74 | return super().run_in_terminal(args, cwd, env) |
| 75 | |
| 76 | argslist = ["0", "$1", "2"] |
| 77 | args = argslist if expansion == "preserve" else " ".join(argslist) |
| 78 | |
| 79 | with Session() as session: |
| 80 | # Create a Python wrapper with a space in the path if requested |
| 81 | if python_with_space: |
| 82 | # Create a directory with a space in the name |
| 83 | python_dir = tmpdir / "python with space" |
| 84 | python_dir.mkdir() |
| 85 | |
| 86 | if sys.platform == "win32": |
| 87 | wrapper = python_dir / "python.cmd" |
| 88 | wrapper.write(f'@echo off\n"{sys.executable}" %*') |
| 89 | else: |
| 90 | wrapper = python_dir / "python.sh" |
| 91 | wrapper.write(f'#!/bin/sh\nexec "{sys.executable}" "$@"') |
| 92 | os.chmod(wrapper.strpath, 0o777) |
| 93 | |
| 94 | session.config["python"] = wrapper.strpath |
| 95 | |
| 96 | backchannel = session.open_backchannel() |
| 97 | with run(session, target(code_to_debug, args=args)): |
| 98 | pass |
nothing calls this directly
no test coverage detected
searching dependent graphs…