|
1 |
| -"""Test cases for the main.py file.""" |
| 1 | +"""Test cases for the command-line interface provided by main.""" |
2 | 2 |
|
3 |
| -import os |
4 |
| -import subprocess |
5 |
| -import sys |
6 |
| -import tempfile |
7 |
| -import venv |
8 |
| -from pathlib import Path |
9 |
| - |
10 |
| -import pytest |
11 | 3 | from typer.testing import CliRunner
|
12 | 4 |
|
| 5 | +from execexam import main |
| 6 | + |
| 7 | +# NOTE: Unless there is a clear reason to do so, only |
| 8 | +# write tests for the command-line interface using the |
| 9 | +# CliRunner provided by typer. |
| 10 | + |
13 | 11 | runner = CliRunner()
|
14 | 12 |
|
15 |
| -EXPECTED_EXIT_CODE_FILE_NOT_FOUND = 4 |
16 |
| - |
17 |
| - |
18 |
| -@pytest.fixture |
19 |
| -def poetry_env(): |
20 |
| - """Create a temporary virtual environment with poetry installed.""" |
21 |
| - with tempfile.TemporaryDirectory() as temp_dir: |
22 |
| - venv_path = Path(temp_dir) / "venv" |
23 |
| - # Create a virtual environment |
24 |
| - venv.create(venv_path, with_pip=True) |
25 |
| - # Get the path to the Python executable in the virtual environment |
26 |
| - if sys.platform == "win32": |
27 |
| - python_path = venv_path / "Scripts" / "python.exe" |
28 |
| - pip_path = venv_path / "Scripts" / "pip.exe" |
29 |
| - else: |
30 |
| - python_path = venv_path / "bin" / "python" |
31 |
| - pip_path = venv_path / "bin" / "pip" |
32 |
| - # Install poetry in the virtual environment |
33 |
| - subprocess.run( |
34 |
| - [str(pip_path), "install", "poetry"], |
35 |
| - check=True, |
36 |
| - capture_output=True, |
37 |
| - text=True, |
38 |
| - ) |
39 |
| - yield str(python_path) |
40 |
| - |
41 |
| - |
42 |
| -@pytest.fixture |
43 |
| -def cwd(): |
44 |
| - """Define a test fixture for the current working directory.""" |
45 |
| - return os.getcwd() |
46 |
| - |
47 |
| - |
48 |
| -@pytest.mark.no_print |
49 |
| -def test_run_with_missing_test(cwd, poetry_env, capfd): |
50 |
| - """Test the run command with default options.""" |
51 |
| - with tempfile.TemporaryDirectory() as temp_dir: |
52 |
| - test_one = Path(temp_dir) / "test_one" |
53 |
| - test_one.mkdir() |
54 |
| - test_path = Path(".") / "tests" / "test_question_one.py" |
55 |
| - test_path_str = str(test_path) |
56 |
| - env = os.environ.copy() |
57 |
| - if sys.platform == "win32": |
58 |
| - env["PYTHONIOENCODING"] = "utf-8" |
59 |
| - env["PYTHONUTF8"] = "1" |
60 |
| - try: |
61 |
| - # Disable output capture temporarily |
62 |
| - with capfd.disabled(): |
63 |
| - result = subprocess.run( |
64 |
| - [ |
65 |
| - poetry_env, |
66 |
| - "-m", |
67 |
| - "poetry", |
68 |
| - "run", |
69 |
| - "execexam", |
70 |
| - ".", |
71 |
| - test_path_str, |
72 |
| - "--report", |
73 |
| - "trace", |
74 |
| - "--report", |
75 |
| - "status", |
76 |
| - "--report", |
77 |
| - "failure", |
78 |
| - "--report", |
79 |
| - "code", |
80 |
| - "--report", |
81 |
| - "setup", |
82 |
| - ], |
83 |
| - capture_output=True, |
84 |
| - text=True, |
85 |
| - encoding="utf-8", |
86 |
| - errors="replace", |
87 |
| - check=False, |
88 |
| - env=env, |
89 |
| - cwd=cwd, |
90 |
| - ) |
91 |
| - assert ( |
92 |
| - result.returncode in [EXPECTED_EXIT_CODE_FILE_NOT_FOUND] |
93 |
| - ), f"Expected return code {EXPECTED_EXIT_CODE_FILE_NOT_FOUND}, got {result.returncode}" |
94 |
| - assert ( |
95 |
| - "file or directory not found" in result.stdout.lower() |
96 |
| - or "no such file or directory" in result.stderr.lower() |
97 |
| - ), "Expected error message about missing file not found in output" |
98 |
| - except UnicodeDecodeError as e: |
99 |
| - pytest.fail(f"Unicode decode error: {e!s}") |
100 |
| - except Exception as e: |
101 |
| - pytest.fail(f"Unexpected error: {e!s}") |
| 13 | +# NOTE: tests that run execexam through the its CLI |
| 14 | +# using the CliRunner can run into dependency issues |
| 15 | +# due to the fact that the pytest plugin that |
| 16 | +# execexam uses is going to be repeatedly loaded |
| 17 | +# and (potentially) not unloaded |
| 18 | + |
| 19 | +# Tests that provide valid arguments {{{ |
| 20 | + |
| 21 | + |
| 22 | +def test_run_use_help(): |
| 23 | + """Test the run command with the --help.""" |
| 24 | + result = runner.invoke(main.cli, ["run", "--help"]) |
| 25 | + assert result.exit_code == 0 |
| 26 | + assert "Arguments" in result.output |
| 27 | + assert "Options" in result.output |
| 28 | + |
| 29 | + |
| 30 | +def test_run_use_tldr(): |
| 31 | + """Test the run command with the --tldr.""" |
| 32 | + result = runner.invoke(main.cli, ["run", "--tldr"]) |
| 33 | + assert result.exit_code == 0 |
| 34 | + assert "Too" in result.output |
| 35 | + assert "Lazy" in result.output |
| 36 | + assert "--help" in result.output |
| 37 | + |
| 38 | + |
| 39 | +def test_run_use_tldr_and_help_defaults_to_help(): |
| 40 | + """Test the run command with the --tldr and --help.""" |
| 41 | + result = runner.invoke(main.cli, ["run", "--tldr", "--help"]) |
| 42 | + assert result.exit_code == 0 |
| 43 | + assert "Arguments" in result.output |
| 44 | + assert "Options" in result.output |
| 45 | + result = runner.invoke(main.cli, ["run", "--help", "--tldr"]) |
| 46 | + assert result.exit_code == 0 |
| 47 | + assert "Arguments" in result.output |
| 48 | + assert "Options" in result.output |
| 49 | + |
| 50 | + |
| 51 | +# }}} |
| 52 | + |
| 53 | + |
| 54 | +# Tests that provide invalid arguments {{{ |
| 55 | + |
| 56 | + |
| 57 | +def test_run_valid_argument_no_action(): |
| 58 | + """Test the run command with valid required arguments.""" |
| 59 | + result = runner.invoke(main.cli, ["run", ". tests/"]) |
| 60 | + assert result.exit_code != 0 |
| 61 | + |
| 62 | + |
| 63 | +def test_run_invalid_report_argument(): |
| 64 | + """Test the run command with invalid report argument.""" |
| 65 | + result = runner.invoke(main.cli, ["run", ". tests/", "--report invalid"]) |
| 66 | + assert result.exit_code != 0 |
| 67 | + |
| 68 | + |
| 69 | +def test_invalid_tldr_spelling(): |
| 70 | + """Test the run command with invalid tldr command-line argument spelling.""" |
| 71 | + result = runner.invoke(main.cli, ["run", ". tests/", "--tldear"]) |
| 72 | + assert result.exit_code != 0 |
| 73 | + |
| 74 | + |
| 75 | +def test_invalid_help_spelling(): |
| 76 | + """Test the run command with invalid help command-line argument spelling.""" |
| 77 | + result = runner.invoke(main.cli, ["run", ". tests/", "--hlp"]) |
| 78 | + assert result.exit_code != 0 |
| 79 | + |
| 80 | + |
| 81 | +# }}} |
0 commit comments