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