-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix some doc code * add tests for markdown docs
- Loading branch information
Showing
8 changed files
with
119 additions
and
18 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import pathlib | ||
|
||
import pytest | ||
|
||
# unfortunately cannot use existing solution, so have to write our own | ||
# (https://github.com/nschloe/pytest-codeblocks/issues/77) | ||
|
||
ignore_files = ["largedatasets.md", "visualization.md"] | ||
|
||
|
||
class DocFile: | ||
def __init__(self, path: pathlib.Path): | ||
self.path = path | ||
self.lines = [] | ||
self.codeblocks = [] | ||
self.extract_codeblocks() | ||
|
||
def extract_codeblocks(self): | ||
with open(self.path, "r") as f: | ||
lines = f.readlines() | ||
# check line for codeblock start/end | ||
lidx = [i for i, k in enumerate(lines) if k.startswith("```")] | ||
print(lidx) | ||
# assert that there are an even number of codeblock start/end | ||
assert len(lidx) % 2 == 0 | ||
cblines = [] | ||
for i in range(0, len(lidx), 2): | ||
start = lidx[i] # includes codeblock start line | ||
end = lidx[i + 1] | ||
# we check for the type. if its 'pycon', we need to remove the '>>>' prompt | ||
blocktype = lines[start].strip().replace("```", "").strip() | ||
blocktype = blocktype.split()[0].strip() | ||
if blocktype == "py" or blocktype == "python": | ||
cblines.append(lines[start + 1 : end]) | ||
elif blocktype == "pycon": | ||
cblines.append( | ||
[k[4:] for k in lines[start + 1 : end] if k.startswith(">>>")] | ||
) | ||
elif blocktype == "bash": | ||
# not python; ignore | ||
pass | ||
else: | ||
raise ValueError("Unknown codeblock type: %s" % blocktype) | ||
self.codeblocks = ["".join(cbl) for cbl in cblines] | ||
|
||
def evaluate(self): | ||
# evaluate all at once (for now) | ||
code = "\n".join(self.codeblocks) | ||
print("Evaluating code:") | ||
print(code) | ||
exec(code) | ||
|
||
|
||
def get_docfiles(): | ||
fixturename = "mddocfile" | ||
docfiles = [] | ||
paths = [] | ||
ids = [] | ||
|
||
docpath = pathlib.Path(__file__).parent.parent / "docs" | ||
for p in docpath.glob("*.md"): | ||
name = p.name | ||
if name in ignore_files: | ||
continue | ||
print("Evaluating %s" % p) | ||
# read lines | ||
docfile = DocFile(p) | ||
docfiles.append(docfile) | ||
paths.append(p) | ||
ids.append(name) | ||
|
||
params = docfiles | ||
return pytest.mark.parametrize(fixturename, params, ids=ids) | ||
|
||
|
||
@pytest.mark.xfail | ||
@get_docfiles() | ||
def test_docs(mddocfile): | ||
mddocfile.evaluate() |