|
| 1 | +"""What the command line does with the current and the pre-2.0 form.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | +import pytest |
| 9 | +from click.shell_completion import ShellComplete |
| 10 | +from click.testing import CliRunner |
| 11 | + |
| 12 | +from in2lambda.main import cli |
| 13 | + |
| 14 | + |
| 15 | +def test_convert_writes_the_set(filters_dir: str, tmp_path) -> None: |
| 16 | + """`in2lambda convert` reaches the runner and produces the zipped set.""" |
| 17 | + example = os.path.join(filters_dir, "PartsSepSol", "example.tex") |
| 18 | + |
| 19 | + result = CliRunner().invoke( |
| 20 | + cli, ["convert", example, "PartsSepSol", "-o", str(tmp_path)] |
| 21 | + ) |
| 22 | + |
| 23 | + assert result.exit_code == 0, result.output |
| 24 | + assert (tmp_path / "set.zip").exists() |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("path", ["example.tex", "./example.tex", "ABSOLUTE"]) |
| 28 | +def test_old_form_fails_and_names_convert( |
| 29 | + path: str, filters_dir: str, monkeypatch, tmp_path |
| 30 | +) -> None: |
| 31 | + """The pre-2.0 form errors out whatever the file path looks like. |
| 32 | +
|
| 33 | + A path starting with ``/`` or ``.`` used to make click print the help and exit 0, |
| 34 | + so every script passing a full path appeared to succeed without converting anything. |
| 35 | + """ |
| 36 | + monkeypatch.setenv("COLUMNS", "200") # So the message is not wrapped mid-sentence. |
| 37 | + # Run from a directory of our own, so `out` appearing there is this command's doing. |
| 38 | + shutil.copy(os.path.join(filters_dir, "PartsSepSol", "example.tex"), tmp_path) |
| 39 | + monkeypatch.chdir(tmp_path) |
| 40 | + if path == "ABSOLUTE": # Only known once we're in the directory holding the file. |
| 41 | + path = os.path.abspath("example.tex") |
| 42 | + |
| 43 | + result = CliRunner().invoke(cli, [path, "PartsSepSol"]) |
| 44 | + |
| 45 | + assert result.exit_code != 0 |
| 46 | + assert "in2lambda convert" in result.output |
| 47 | + assert not (tmp_path / "out").exists() |
| 48 | + |
| 49 | + |
| 50 | +def test_old_form_fails_when_run_as_the_installed_command( |
| 51 | + filters_dir: str, tmp_path |
| 52 | +) -> None: |
| 53 | + """The same holds for ``cli()``, which is what the installed command runs. |
| 54 | +
|
| 55 | + CliRunner calls ``cli.main`` instead, so it cannot see this path: on beartype |
| 56 | + 0.18.5 the import hook left ``cli`` a plain function, and ``cli()`` ran |
| 57 | + ``convert``'s body whatever the arguments. |
| 58 | + """ |
| 59 | + result = subprocess.run( |
| 60 | + [ |
| 61 | + sys.executable, |
| 62 | + "-c", |
| 63 | + "from in2lambda.main import cli; cli()", |
| 64 | + os.path.join(filters_dir, "PartsSepSol", "example.tex"), |
| 65 | + "PartsSepSol", |
| 66 | + ], |
| 67 | + capture_output=True, |
| 68 | + text=True, |
| 69 | + cwd=tmp_path, |
| 70 | + env={**os.environ, "COLUMNS": "200"}, |
| 71 | + ) |
| 72 | + |
| 73 | + assert result.returncode != 0 |
| 74 | + assert "in2lambda convert" in result.stdout + result.stderr |
| 75 | + assert not (tmp_path / "out").exists() |
| 76 | + |
| 77 | + |
| 78 | +def test_completing_the_old_form_offers_the_subcommand() -> None: |
| 79 | + """Completion resolves half-typed command lines, so the guard must not fire there. |
| 80 | +
|
| 81 | + Without that exemption, `in2lambda ./questions.tex <TAB>` printed a traceback |
| 82 | + where the shell expected candidates. |
| 83 | + """ |
| 84 | + completions = ShellComplete( |
| 85 | + cli, {}, "in2lambda", "_IN2LAMBDA_COMPLETE" |
| 86 | + ).get_completions(["./questions.tex"], "") |
| 87 | + |
| 88 | + assert [candidate.value for candidate in completions] == ["convert"] |
0 commit comments