Skip to content

Commit

Permalink
fix notebooks test #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed May 21, 2022
1 parent d282d4f commit fae235b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 9 additions & 5 deletions bazel/python.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@bazel_skylib//lib:paths.bzl", "paths")

# py_test_module_list creates a py_test target for each
# Python file in `files`
def py_test_module_list(files, size, deps, extra_srcs, name_suffix="", **kwargs):
Expand Down Expand Up @@ -25,13 +27,15 @@ def py_test_run_all_subdirectory(include, exclude, extra_srcs, **kwargs):

# Runs all included notebooks as py_test targets, by first converting them to .py files with "test_myst_doc.py".
def py_test_run_all_notebooks(include, exclude, **kwargs):
for file in native.glob(include = include, exclude = exclude):
for file in native.glob(include = include, exclude = exclude, allow_empty=False):
print(file)
basename = file.rpartition("/")[-1]
basename = paths.split_extension(file)[0]
if basename == file:
basename = basename + "_test"
native.py_test(
name = basename[:-3],
name = basename,
main = "test_myst_doc.py",
srcs = ["//doc:test_myst_doc.py"],
args = ["--path", file],
srcs = ["//doc:test_myst_doc.py"],
args = ["--find-recursively", "--path", file],
**kwargs
)
14 changes: 13 additions & 1 deletion doc/test_myst_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import tempfile
import sys
from pathlib import Path

import jupytext

Expand All @@ -13,12 +14,23 @@
"--path",
help="path to the jupytext-compatible file",
)
parser.add_argument(
"--find-recursively",
action="store_true",
help="if true, will attempt to find path recursively in cwd",
)

if __name__ == "__main__":

args, remainder = parser.parse_known_args()

with open(args.path, "r") as f:
path = Path(args.path)
cwd = Path.cwd()
if args.find_recursively and not path.exists():
path = next((p for p in cwd.rglob("*") if str(p).endswith(args.path)), None)
assert path and path.exists()

with open(path, "r") as f:
notebook = jupytext.read(f)

name = ""
Expand Down

0 comments on commit fae235b

Please sign in to comment.