Skip to content

Commit 767c1bf

Browse files
committed
Add tests for caching-related CLI
1 parent b8b8b51 commit 767c1bf

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

docs/command_line.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Options:
1515
-h, --help Show this message and exit.
1616
1717
Commands:
18-
run Generate Python stub files.
18+
clean Clean the cache.
19+
run Generate Python stub files.
1920
```
2021

2122
<!--- end cli-docstub --->
@@ -58,3 +59,24 @@ Options:
5859
```
5960

6061
<!--- end cli-docstub-run --->
62+
63+
64+
## Command `docstub clean`
65+
66+
<!--- The following block is checked by the test suite --->
67+
<!--- begin cli-docstub-clean --->
68+
69+
```plain
70+
Usage: docstub clean [OPTIONS]
71+
72+
Clean the cache.
73+
74+
Looks for a cache directory relative to the current working directory. If
75+
one exists, remove it.
76+
77+
Options:
78+
-v, --verbose Print more details (repeatable).
79+
-h, --help Show this message and exit.
80+
```
81+
82+
<!--- end cli-docstub-clean --->

tests/test_cli.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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()

tests/test_docs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def test_user_guide_example(tmp_path):
5656

5757

5858
@pytest.mark.parametrize(
59-
("command", "name"), [(_cli.cli, "docstub"), (_cli.run, "docstub run")]
59+
("command", "name"),
60+
[(_cli.cli, "docstub"), (_cli.run, "docstub run"), (_cli.clean, "docstub clean")],
6061
)
6162
def test_command_line_reference(command, name):
6263
ctx = click.Context(command, info_name=name)

0 commit comments

Comments
 (0)