-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* TST: use sybil for doctests * Hand fixtures to sybil * Add filesystem_backend fixture * Further work on sybil for docstrings * Fix doctests * Add further ideas * Clean up doctests * Test usage documentation as well * Move filesystem fixture * Avoid creation of file outside tmpdir * Remove dependency on jupyer-sphinx * Update legacy backend section * Remove jupyter-sphinx from docs/conf.py * Try to fix under MacOS * Fix typo * Try to fix under Windows * Add missing SkipParser * Update docs/usage.rst Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --------- Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b8d71ac
commit 2647892
Showing
18 changed files
with
355 additions
and
451 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,88 @@ | ||
import datetime | ||
import os | ||
import tempfile | ||
import doctest | ||
|
||
import pytest | ||
import sybil | ||
from sybil.parsers.rest import DocTestParser | ||
|
||
import audeer | ||
|
||
import audbackend | ||
|
||
|
||
class DoctestFileSystem(audbackend.backend.FileSystem): | ||
def _date( | ||
self, | ||
path: str, | ||
) -> str: | ||
# Collect doctests | ||
pytest_collect_file = sybil.Sybil( | ||
parsers=[DocTestParser(optionflags=doctest.ELLIPSIS)], | ||
patterns=["*.py"], | ||
fixtures=[ | ||
"filesystem", | ||
"mock_date", | ||
"mock_owner", | ||
"mock_repr", | ||
"prepare_docstring_tests", | ||
], | ||
).pytest() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def filesystem(tmpdir): | ||
"""Filesystem backend. | ||
A repository with unique name is created | ||
for the filesystem backend. | ||
The filesystem backend is marked as opened | ||
and returned. | ||
Args: | ||
tmpdir: tmpdir fixture | ||
Returns: | ||
filesystem backend object | ||
""" | ||
repo = f"repo-{audeer.uid()[:8]}" | ||
host = audeer.mkdir(tmpdir, "host") | ||
audeer.mkdir(host, repo) | ||
backend = audbackend.backend.FileSystem(host, repo) | ||
backend.opened = True | ||
yield backend | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mock_date(): | ||
r"""Custom date method to return a fixed date.""" | ||
|
||
def date(path: str, version: str = None) -> str: | ||
date = datetime.datetime(1991, 2, 20) | ||
date = audbackend.core.utils.date_format(date) | ||
return date | ||
|
||
def _owner( | ||
self, | ||
path: str, | ||
) -> str: | ||
yield date | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mock_owner(): | ||
r"""Custom owner method to return a fixed owner.""" | ||
|
||
def owner(path: str, version: str = None) -> str: | ||
return "doctest" | ||
|
||
yield owner | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mock_repr(): | ||
"""Custom __repr__ method to return fixed string.""" | ||
return 'audbackend.interface.FileSystem("host", "repo")' | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
def prepare_docstring_tests(doctest_namespace): | ||
with tempfile.TemporaryDirectory() as tmp: | ||
# Change to tmp dir | ||
current_dir = os.getcwd() | ||
os.chdir(tmp) | ||
# Prepare backend | ||
audeer.mkdir("host") | ||
audbackend.backend.FileSystem.create("host", "repo") | ||
# Provide example file `src.txt` | ||
audeer.touch("src.txt") | ||
# Provide DoctestFileSystem as FileSystem, | ||
# and audbackend | ||
# in docstring examples | ||
doctest_namespace["DoctestFileSystem"] = DoctestFileSystem | ||
doctest_namespace["audbackend"] = audbackend | ||
|
||
yield | ||
|
||
# Remove backend | ||
audbackend.backend.FileSystem.delete("host", "repo") | ||
# Change back to current dir | ||
os.chdir(current_dir) | ||
def prepare_docstring_tests(tmpdir, monkeypatch): | ||
r"""Code to be run before each doctest.""" | ||
# Change to tmp dir | ||
monkeypatch.chdir(tmpdir) | ||
|
||
# Provide example file `src.txt` | ||
audeer.touch("src.txt") | ||
|
||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.