| 289 | |
| 290 | |
| 291 | def test_argcomplete(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: |
| 292 | try: |
| 293 | bash_version = subprocess.run( |
| 294 | ["bash", "--version"], |
| 295 | stdout=subprocess.PIPE, |
| 296 | stderr=subprocess.DEVNULL, |
| 297 | check=True, |
| 298 | universal_newlines=True, |
| 299 | ).stdout |
| 300 | except (OSError, subprocess.CalledProcessError): |
| 301 | pytest.skip("bash is not available") |
| 302 | if "GNU bash" not in bash_version: |
| 303 | # See #7518. |
| 304 | pytest.skip("not a real bash") |
| 305 | |
| 306 | script = str(pytester.path.joinpath("test_argcomplete")) |
| 307 | |
| 308 | with open(str(script), "w") as fp: |
| 309 | # redirect output from argcomplete to stdin and stderr is not trivial |
| 310 | # http://stackoverflow.com/q/12589419/1307905 |
| 311 | # so we use bash |
| 312 | fp.write( |
| 313 | 'COMP_WORDBREAKS="$COMP_WORDBREAKS" {} -m pytest 8>&1 9>&2'.format( |
| 314 | shlex.quote(sys.executable) |
| 315 | ) |
| 316 | ) |
| 317 | # alternative would be extended Pytester.{run(),_run(),popen()} to be able |
| 318 | # to handle a keyword argument env that replaces os.environ in popen or |
| 319 | # extends the copy, advantage: could not forget to restore |
| 320 | monkeypatch.setenv("_ARGCOMPLETE", "1") |
| 321 | monkeypatch.setenv("_ARGCOMPLETE_IFS", "\x0b") |
| 322 | monkeypatch.setenv("COMP_WORDBREAKS", " \\t\\n\"\\'><=;|&(:") |
| 323 | |
| 324 | arg = "--fu" |
| 325 | monkeypatch.setenv("COMP_LINE", "pytest " + arg) |
| 326 | monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) |
| 327 | result = pytester.run("bash", str(script), arg) |
| 328 | if result.ret == 255: |
| 329 | # argcomplete not found |
| 330 | pytest.skip("argcomplete not available") |
| 331 | elif not result.stdout.str(): |
| 332 | pytest.skip( |
| 333 | "bash provided no output on stdout, argcomplete not available? (stderr={!r})".format( |
| 334 | result.stderr.str() |
| 335 | ) |
| 336 | ) |
| 337 | else: |
| 338 | result.stdout.fnmatch_lines(["--funcargs", "--fulltrace"]) |
| 339 | os.mkdir("test_argcomplete.d") |
| 340 | arg = "test_argc" |
| 341 | monkeypatch.setenv("COMP_LINE", "pytest " + arg) |
| 342 | monkeypatch.setenv("COMP_POINT", str(len("pytest " + arg))) |
| 343 | result = pytester.run("bash", str(script), arg) |
| 344 | result.stdout.fnmatch_lines(["test_argcomplete", "test_argcomplete.d/"]) |