|
| 1 | +"""Test command line interface.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | +from click.testing import CliRunner |
| 9 | + |
| 10 | +from docstub import _cli |
| 11 | +from docstub._cache import create_cache |
| 12 | + |
| 13 | +PROJECT_ROOT = Path(__file__).parent.parent |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def tmp_path_cwd(tmp_path): |
| 18 | + """Fixture: Create temporary directory and use it as working directory. |
| 19 | +
|
| 20 | + .. warning:: |
| 21 | + Not written with parallelization in mind! |
| 22 | + """ |
| 23 | + previous_cwd = Path.cwd() |
| 24 | + os.chdir(tmp_path) |
| 25 | + try: |
| 26 | + yield tmp_path |
| 27 | + except: |
| 28 | + os.chdir(previous_cwd) |
| 29 | + raise |
| 30 | + |
| 31 | + |
| 32 | +class Test_run: |
| 33 | + def test_no_cache(self, tmp_path_cwd, caplog): |
| 34 | + caplog.set_level(logging.INFO) |
| 35 | + |
| 36 | + source_file = tmp_path_cwd / "some_file.py" |
| 37 | + source_file.touch() |
| 38 | + |
| 39 | + # First run using '--no-cache' shouldn't create a cache directory |
| 40 | + runner = CliRunner() |
| 41 | + run_result = runner.invoke(_cli.run, args=["--no-cache", str(source_file)]) |
| 42 | + assert run_result.exception is None |
| 43 | + assert run_result.exit_code == 0 |
| 44 | + assert not _cli._cache_dir_in_cwd().exists() |
| 45 | + |
| 46 | + # Second run without '--no-cache' should create a cache directory |
| 47 | + caplog.clear() |
| 48 | + runner = CliRunner() |
| 49 | + run_result = runner.invoke(_cli.run, args=[str(source_file)]) |
| 50 | + assert run_result.exception is None |
| 51 | + assert run_result.exit_code == 0 |
| 52 | + assert _cli._cache_dir_in_cwd().exists() |
| 53 | + # Check that no collected file was logged as "(cached)" |
| 54 | + assert "(cached)" not in "\n".join(caplog.messages) |
| 55 | + |
| 56 | + # Third run with existing cache should use cache |
| 57 | + caplog.clear() |
| 58 | + runner = CliRunner() |
| 59 | + run_result = runner.invoke(_cli.run, args=[str(source_file)]) |
| 60 | + assert run_result.exception is None |
| 61 | + assert run_result.exit_code == 0 |
| 62 | + # Check that at least one collected file was logged as "(cached)" |
| 63 | + assert "(cached)" in "\n".join(caplog.messages) |
| 64 | + |
| 65 | + # Fourth run with '--no-cache' should ignore existing cache |
| 66 | + caplog.clear() |
| 67 | + runner = CliRunner() |
| 68 | + run_result = runner.invoke(_cli.run, args=["--no-cache", str(source_file)]) |
| 69 | + assert run_result.exception is None |
| 70 | + assert run_result.exit_code == 0 |
| 71 | + # Check that at least one collected file was logged as "(cached)" |
| 72 | + assert "(cached)" not in "\n".join(caplog.messages) |
| 73 | + |
| 74 | + |
| 75 | +class Test_clean: |
| 76 | + @pytest.mark.parametrize("verbosity", [["-v"], ["--verbose"], []]) |
| 77 | + def test_basic(self, tmp_path_cwd, verbosity): |
| 78 | + # Cleaning empty directory works |
| 79 | + runner = CliRunner() |
| 80 | + run_result = runner.invoke(_cli.clean, args=verbosity) |
| 81 | + assert run_result.exception is None |
| 82 | + assert run_result.exit_code == 0 |
| 83 | + |
| 84 | + cache_dir = _cli._cache_dir_in_cwd() |
| 85 | + create_cache(cache_dir) |
| 86 | + assert cache_dir.is_dir() |
| 87 | + |
| 88 | + # Cache directory should be removed after running clean again |
| 89 | + run_result = runner.invoke(_cli.clean, args=verbosity) |
| 90 | + assert run_result.exception is None |
| 91 | + assert run_result.exit_code == 0 |
| 92 | + assert not cache_dir.exists() |
0 commit comments