|
5 | 5 |
|
6 | 6 | import click |
7 | 7 |
|
8 | | -from docstub._cli import main |
| 8 | +from docstub._cli import main as docstub_main |
9 | 9 |
|
10 | 10 | PROJECT_ROOT = Path(__file__).parent.parent |
11 | 11 |
|
12 | 12 |
|
| 13 | +def test_getting_started_example(tmp_path): |
| 14 | + # Load user guide |
| 15 | + md_file = PROJECT_ROOT / "doc/user_guide.md" |
| 16 | + with md_file.open("r") as io: |
| 17 | + md_content = io.read() |
| 18 | + |
| 19 | + # Extract code block for example.py |
| 20 | + regex_py = ( |
| 21 | + r"<!--- begin example.py --->" |
| 22 | + r"\n```python\n(.*)\n```\n" |
| 23 | + r"<!--- end example.py --->" |
| 24 | + ) |
| 25 | + matches_py = re.findall(regex_py, md_content, flags=re.DOTALL) |
| 26 | + assert len(matches_py) == 1 |
| 27 | + py_source = matches_py[0] |
| 28 | + |
| 29 | + # Create example.py and run docstub on it |
| 30 | + py_file = tmp_path / "example.py" |
| 31 | + with py_file.open("x") as io: |
| 32 | + io.write(py_source) |
| 33 | + docstub_main([str(py_file)], standalone_mode=False) |
| 34 | + |
| 35 | + # Load created PYI file, this is what we expect to find in the user guide's |
| 36 | + # code block for example.pyi |
| 37 | + pyi_file = py_file.with_suffix(".pyi") |
| 38 | + assert pyi_file.is_file() |
| 39 | + with pyi_file.open("r") as io: |
| 40 | + expected_pyi = io.read().strip() |
| 41 | + |
| 42 | + # Extract code block for example.pyi from guide |
| 43 | + regex_pyi = ( |
| 44 | + r"<!--- begin example.pyi --->" |
| 45 | + r"\n```python\n(.*)\n```\n" |
| 46 | + r"<!--- end example.pyi --->" |
| 47 | + ) |
| 48 | + matches_pyi = re.findall(regex_pyi, md_content, flags=re.DOTALL) |
| 49 | + assert len(matches_pyi) == 1 |
| 50 | + actual_pyi = matches_pyi[0].strip() |
| 51 | + |
| 52 | + assert expected_pyi == actual_pyi |
| 53 | + |
| 54 | + |
13 | 55 | def test_command_line_help(): |
14 | | - ctx = click.Context(main, info_name="docstub") |
| 56 | + ctx = click.Context(docstub_main, info_name="docstub") |
15 | 57 | expected_help = f""" |
16 | | -
|
17 | 58 | ```plain |
18 | | -{main.get_help(ctx)} |
| 59 | +{docstub_main.get_help(ctx)} |
19 | 60 | ``` |
20 | | -
|
21 | | -""" |
| 61 | +""".strip() |
22 | 62 | md_file = PROJECT_ROOT / "doc/command_line_reference.md" |
23 | 63 | with md_file.open("r") as io: |
24 | 64 | md_content = io.read() |
25 | 65 |
|
26 | 66 | regex = r"<!--- begin command-line-help --->(.*)<!--- end command-line-help --->" |
27 | | - match = re.findall(regex, md_content, flags=re.DOTALL) |
28 | | - assert len(match) == 1 |
| 67 | + matches = re.findall(regex, md_content, flags=re.DOTALL) |
| 68 | + assert len(matches) == 1 |
29 | 69 |
|
30 | | - actual_help = match[0] |
| 70 | + actual_help = matches[0].strip() |
31 | 71 | assert actual_help == expected_help |
0 commit comments