Skip to content

Commit

Permalink
Turn the tests notebooks into fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Nov 23, 2023
1 parent 1417ca7 commit a2cd5c7
Show file tree
Hide file tree
Showing 53 changed files with 650 additions and 659 deletions.
9 changes: 9 additions & 0 deletions src/jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def system(*args, **kwargs):
return out.decode("utf-8")


def tool_version(tool):
try:
args = tool.split(" ")
args.append("--version")
return system(*args)
except (OSError, SystemExit): # pragma: no cover
return None


def str2bool(value):
"""Parse Yes/No/Default string
https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse"""
Expand Down
79 changes: 77 additions & 2 deletions src/jupytext/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import difflib
import json
import os
import re

from jupytext.paired_paths import full_path

from .cell_metadata import _IGNORE_CELL_METADATA
from .combine import combine_inputs_with_outputs
from .formats import long_form_one_format
from .formats import check_auto_ext, long_form_one_format
from .header import _DEFAULT_NOTEBOOK_METADATA
from .jupytext import reads, writes
from .jupytext import read, reads, write, writes
from .metadata_filter import filter_metadata

_BLANK_LINE = re.compile(r"^\s*$")
Expand Down Expand Up @@ -388,3 +391,75 @@ def test_round_trip_conversion(
allow_expected_differences,
raise_on_first_difference=stop_on_first_error,
)


# The functions below are used in the Jupytext text collection
def create_mirror_file_if_missing(mirror_file, notebook, fmt):
if not os.path.isfile(mirror_file):
write(notebook, mirror_file, fmt=fmt)


def assert_conversion_same_as_mirror(nb_file, fmt, mirror_name, compare_notebook=False):
"""This function is used in the tests"""
dirname, basename = os.path.split(nb_file)
file_name, org_ext = os.path.splitext(basename)
fmt = long_form_one_format(fmt)
notebook = read(nb_file, fmt=fmt)
fmt = check_auto_ext(fmt, notebook.metadata, "")
ext = fmt["extension"]
mirror_file = os.path.join(
dirname, "..", "..", "outputs", mirror_name, full_path(file_name, fmt)
)

# it's better not to have Jupytext metadata in test notebooks:
if fmt == "ipynb" and "jupytext" in notebook.metadata: # pragma: no cover
notebook.metadata.pop("jupytext")
write(nb_file, fmt=fmt)

create_mirror_file_if_missing(mirror_file, notebook, fmt)

# Compare the text representation of the two notebooks
if compare_notebook:
# Read and convert the mirror file to the latest nbformat version if necessary
nb_mirror = read(mirror_file, as_version=notebook.nbformat)
nb_mirror.nbformat_minor = notebook.nbformat_minor
compare_notebooks(nb_mirror, notebook)
return
elif ext == ".ipynb":
notebook = read(mirror_file)
fmt.update({"extension": org_ext})
actual = writes(notebook, fmt)
with open(nb_file, encoding="utf-8") as fp:
expected = fp.read()
else:
actual = writes(notebook, fmt)
with open(mirror_file, encoding="utf-8") as fp:
expected = fp.read()

if not actual.endswith("\n"):
actual = actual + "\n"
compare(actual, expected)

# Compare the two notebooks
if ext != ".ipynb":
notebook = read(nb_file)
nb_mirror = read(mirror_file, fmt=fmt)

if fmt.get("format_name") == "sphinx":
nb_mirror.cells = nb_mirror.cells[1:]
for cell in notebook.cells:
cell.metadata = {}
for cell in nb_mirror.cells:
cell.metadata = {}

compare_notebooks(nb_mirror, notebook, fmt)

nb_mirror = combine_inputs_with_outputs(nb_mirror, notebook)
compare_notebooks(nb_mirror, notebook, fmt, compare_outputs=True)


def notebook_model(nb):
"""Return a notebook model, with content a
dictionary rather than a notebook object.
To be used in tests only."""
return dict(type="notebook", content=json.loads(json.dumps(nb)))
12 changes: 11 additions & 1 deletion src/jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
myst_extensions,
myst_version,
)
from .pandoc import pandoc_version
from .pandoc import is_pandoc_available, pandoc_version
from .stringparser import StringParser
from .version import __version__

Expand Down Expand Up @@ -825,3 +825,13 @@ def check_auto_ext(fmt, metadata, option):
option, short_form_one_format(fmt)
)
)


def formats_with_support_for_cell_metadata():
for fmt in JUPYTEXT_FORMATS:
if fmt.format_name == "myst" and not is_myst_available():
continue
if fmt.format_name == "pandoc" and not is_pandoc_available():
continue
if fmt.format_name not in ["sphinx", "nomarker", "spin", "quarto"]:
yield f"{fmt.extension[1:]}:{fmt.format_name}"
Empty file removed tests/__init__.py
Empty file.
Loading

0 comments on commit a2cd5c7

Please sign in to comment.