From a2cd5c7e1f264a42294d8986b99a2ca874853fb3 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Tue, 21 Nov 2023 21:50:15 +0000 Subject: [PATCH] Turn the tests notebooks into fixtures --- src/jupytext/cli.py | 9 + src/jupytext/compare.py | 79 +++++- src/jupytext/formats.py | 12 +- tests/__init__.py | 0 tests/conftest.py | 248 +++++++++++++++++- tests/external/__init__.py | 0 tests/external/cli/__init__.py | 0 tests/external/cli/test_black.py | 21 +- tests/external/docs/__init__.py | 0 tests/external/jupyter_fs/__init__.py | 0 tests/external/jupyter_fs/test_jupyter_fs.py | 4 +- tests/external/pre_commit/__init__.py | 0 .../pre_commit/test_pre_commit_scripts.py | 7 +- tests/external/round_trip/__init__.py | 0 .../round_trip/test_mirror_external.py | 45 +--- tests/external/rst2md/__init__.py | 0 .../simple_external_notebooks/__init__.py | 0 tests/functional/__init__.py | 0 tests/functional/cli/__init__.py | 0 tests/functional/cli/test_cli.py | 167 +++++------- tests/functional/config/__init__.py | 0 tests/functional/docs/__init__.py | 0 tests/functional/metadata/__init__.py | 0 tests/functional/others/__init__.py | 0 tests/functional/others/test_auto_ext.py | 24 +- .../others/test_cell_tags_are_preserved.py | 2 +- tests/functional/others/test_combine.py | 7 +- .../test_sample_notebooks_are_normalized.py | 9 +- tests/functional/others/test_save_multiple.py | 19 +- .../functional/others/test_trust_notebook.py | 41 +-- tests/functional/others/test_unicode.py | 13 +- .../test_write_does_not_modify_notebook.py | 14 +- tests/functional/round_trip/__init__.py | 0 .../test_jupytext_nbconvert_round_trip.py | 12 +- tests/functional/round_trip/test_mirror.py | 210 ++++----------- .../functional/round_trip/test_read_all_py.py | 13 - .../round_trip/test_rmd_to_ipynb.py | 9 +- tests/functional/simple_notebooks/__init__.py | 0 .../simple_notebooks/test_ipynb_to_R.py | 12 +- .../simple_notebooks/test_ipynb_to_py.py | 8 +- .../simple_notebooks/test_ipynb_to_rmd.py | 8 +- .../simple_notebooks/test_knitr_spin.py | 11 +- .../test_read_simple_percent.py | 4 +- tests/integration/__init__.py | 0 tests/integration/cli/__init__.py | 0 .../integration/contents_manager/__init__.py | 0 .../contents_manager/test_cm_config.py | 3 +- .../contents_manager/test_contentsmanager.py | 191 ++++++-------- tests/unit/__init__.py | 0 tests/unit/test_escape_magics.py | 4 +- tests/unit/test_formats.py | 17 +- tests/unit/test_pep8.py | 11 - tests/utils.py | 75 ------ 53 files changed, 650 insertions(+), 659 deletions(-) delete mode 100644 tests/__init__.py delete mode 100644 tests/external/__init__.py delete mode 100644 tests/external/cli/__init__.py delete mode 100644 tests/external/docs/__init__.py delete mode 100644 tests/external/jupyter_fs/__init__.py delete mode 100644 tests/external/pre_commit/__init__.py delete mode 100644 tests/external/round_trip/__init__.py delete mode 100644 tests/external/rst2md/__init__.py delete mode 100644 tests/external/simple_external_notebooks/__init__.py delete mode 100644 tests/functional/__init__.py delete mode 100644 tests/functional/cli/__init__.py delete mode 100644 tests/functional/config/__init__.py delete mode 100644 tests/functional/docs/__init__.py delete mode 100644 tests/functional/metadata/__init__.py delete mode 100644 tests/functional/others/__init__.py delete mode 100644 tests/functional/round_trip/__init__.py delete mode 100644 tests/functional/simple_notebooks/__init__.py delete mode 100644 tests/integration/__init__.py delete mode 100644 tests/integration/cli/__init__.py delete mode 100644 tests/integration/contents_manager/__init__.py delete mode 100644 tests/unit/__init__.py delete mode 100644 tests/utils.py diff --git a/src/jupytext/cli.py b/src/jupytext/cli.py index dcb1b9630..6ce98a033 100644 --- a/src/jupytext/cli.py +++ b/src/jupytext/cli.py @@ -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""" diff --git a/src/jupytext/compare.py b/src/jupytext/compare.py index df3ea4739..5b3347c7e 100644 --- a/src/jupytext/compare.py +++ b/src/jupytext/compare.py @@ -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*$") @@ -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))) diff --git a/src/jupytext/formats.py b/src/jupytext/formats.py index d9a481768..efed28cd4 100644 --- a/src/jupytext/formats.py +++ b/src/jupytext/formats.py @@ -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__ @@ -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}" diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/conftest.py b/tests/conftest.py index 5b0d5b6c5..f74b72bbb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,5 @@ +import itertools +import re import sys import unittest.mock as mock from pathlib import Path @@ -14,17 +16,19 @@ import jupytext from jupytext.cell_reader import rst2md -from jupytext.cli import system +from jupytext.cli import system, tool_version +from jupytext.formats import formats_with_support_for_cell_metadata from jupytext.myst import is_myst_available from jupytext.pandoc import is_pandoc_available from jupytext.quarto import is_quarto_available -from .utils import formats_with_support_for_cell_metadata, tool_version - # Pytest's tmpdir is in /tmp (at least for me), so this helps to avoid interferences between # global configuration on HOME and the test collection jupytext.config.JUPYTEXT_CEILING_DIRECTORIES = ["/tmp/"] +SAMPLE_NOTEBOOK_PATH = Path(__file__).parent / "data" / "notebooks" / "inputs" +ROOT_PATH = Path(__file__).parent.parent + @pytest.fixture def no_jupytext_version_number(): @@ -70,7 +74,16 @@ def python_notebook(): "display_name": "Python 3", "language": "python", "name": "python_kernel", - } + }, + "language_info": { + "codemirror_mode": {"name": "ipython", "version": 3}, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.4", + }, }, ) @@ -106,6 +119,233 @@ def fmt_with_cell_metadata(request): return request.param +def list_notebooks(path="ipynb", skip=""): + """All notebooks in the directory notebooks/path, + or in the package itself""" + if path == "ipynb": + return ( + list_notebooks("ipynb_julia", skip=skip) + + list_notebooks("ipynb_py", skip=skip) + + list_notebooks("ipynb_R", skip=skip) + ) + + nb_path = SAMPLE_NOTEBOOK_PATH + + if path == "ipynb_all": + return itertools.chain( + *( + list_notebooks(folder.name, skip=skip) + for folder in nb_path.iterdir() + if folder.name.startswith("ipynb_") + ) + ) + + if path == "all": + return itertools.chain( + *(list_notebooks(folder.name, skip=skip) for folder in nb_path.iterdir()) + ) + + if path.startswith("."): + nb_path = Path(__file__).parent / ".." / path + else: + nb_path = nb_path / path + + if skip: + skip_re = re.compile(".*" + skip + ".*") + return [ + str(nb_file) + for nb_file in nb_path.iterdir() + if nb_file.is_file() and not skip_re.match(nb_file.name) + ] + + return [str(nb_file) for nb_file in nb_path.iterdir() if nb_file.is_file()] + + +def notebook_id_func(nb_file): + nb_file = Path(nb_file) + if SAMPLE_NOTEBOOK_PATH in nb_file.parents: + return str(nb_file.relative_to(SAMPLE_NOTEBOOK_PATH)) + return str(nb_file.relative_to(ROOT_PATH)) + + +@pytest.fixture(params=list_notebooks("ipynb_all"), ids=notebook_id_func) +def ipynb_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("all"), ids=notebook_id_func) +def any_nb_file(request): + return request.param + + +@pytest.fixture +def ipynb_py_R_jl_files(): + return list_notebooks() + + +@pytest.fixture(params=list_notebooks(), ids=notebook_id_func) +def ipynb_py_R_jl_file(request): + return request.param + + +@pytest.fixture +def ipynb_py_R_jl_ext(ipynb_py_R_jl_file): + for language in "py", "R", "julia": + if f"/ipynb_{language}/" in str(ipynb_py_R_jl_file): + return ".jl" if language == "julia" else "." + language + + raise RuntimeError(f"language not found for {ipynb_py_R_jl_file}") + + +@pytest.fixture( + params=list_notebooks("ipynb") + list_notebooks("Rmd"), ids=notebook_id_func +) +def ipynb_or_rmd_file(request): + return request.param + + +@pytest.fixture( + params=list_notebooks("ipynb_py") + list_notebooks("ipynb_R"), ids=notebook_id_func +) +def ipynb_py_R_file(request): + return request.param + + +@pytest.fixture +def ipynb_py_files(): + return list_notebooks("ipynb_py") + + +@pytest.fixture(params=list_notebooks("ipynb_py"), ids=notebook_id_func) +def ipynb_py_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("ipynb_R"), ids=notebook_id_func) +def ipynb_R_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("ipynb_julia"), ids=notebook_id_func) +def ipynb_julia_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("ipynb_scheme"), ids=notebook_id_func) +def ipynb_scheme_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("ipynb_cpp"), ids=notebook_id_func) +def ipynb_cpp_file(request): + return request.param + + +@pytest.fixture( + params=list_notebooks("ipynb_all", skip="many hash"), ids=notebook_id_func +) +def ipynb_to_light(request): + return request.param + + +@pytest.fixture(params=list_notebooks("ipynb_all"), ids=notebook_id_func) +def ipynb_to_myst(request): + return request.param + + +@pytest.fixture( + params=[ + py_file + for py_file in list_notebooks("./src/jupytext") + if py_file.endswith(".py") and "folding_markers" not in py_file + ], + ids=notebook_id_func, +) +def py_file(request): + return request.param + + +@pytest.fixture( + params=list_notebooks("julia") + + list_notebooks("python") + + list_notebooks("R") + + list_notebooks("ps1"), + ids=notebook_id_func, +) +def script_to_ipynb(request): + return request.param + + +@pytest.fixture(params=list_notebooks("python"), ids=notebook_id_func) +def python_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("percent"), ids=notebook_id_func) +def percent_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("hydrogen"), ids=notebook_id_func) +def hydrogen_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("R"), ids=notebook_id_func) +def r_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("R_spin"), ids=notebook_id_func) +def r_spin_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("md"), ids=notebook_id_func) +def md_file(request): + return request.param + + +@pytest.fixture( + params=list_notebooks( + "ipynb", skip="(functional|Notebook with|flavors|invalid|305)" + ), + ids=notebook_id_func, +) +def ipynb_to_pandoc(request): + return request.param + + +@pytest.fixture( + params=list_notebooks( + "ipynb", + skip="(functional|Notebook with|plotly_graphs|flavors|complex_metadata|" + "update83|raw_cell|_66|nteract|LaTeX|invalid|305|text_outputs|ir_notebook|jupyter|with_R_magic)", + ), + ids=notebook_id_func, +) +def ipynb_to_quarto(request): + return request.param + + +@pytest.fixture( + params=list_notebooks("ipynb_py", skip="(raw|hash|frozen|magic|html|164|long)"), + ids=notebook_id_func, +) +def ipynb_to_sphinx(request): + return request.param + + +@pytest.fixture(params=list_notebooks("Rmd"), ids=notebook_id_func) +def rmd_file(request): + return request.param + + +@pytest.fixture(params=list_notebooks("sphinx"), ids=notebook_id_func) +def sphinx_file(request): + return request.param + + def pytest_runtest_setup(item): for mark in item.iter_markers(): for tool in [ diff --git a/tests/external/__init__.py b/tests/external/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/external/cli/__init__.py b/tests/external/cli/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/external/cli/test_black.py b/tests/external/cli/test_black.py index cf2dc2107..1f5f79f20 100644 --- a/tests/external/cli/test_black.py +++ b/tests/external/cli/test_black.py @@ -11,19 +11,18 @@ from jupytext.compare import compare, compare_cells, compare_notebooks from jupytext.header import _DEFAULT_NOTEBOOK_METADATA -from ...utils import list_notebooks - @pytest.mark.requires_black -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1]) -def test_apply_black_on_python_notebooks(tmpdir, cwd_tmpdir, nb_file): - copyfile(nb_file, "notebook.ipynb") +def test_apply_black_on_python_notebooks(tmpdir, cwd_tmpdir, ipynb_py_file): + if "cell metadata" in ipynb_py_file: + pytest.skip() + copyfile(ipynb_py_file, "notebook.ipynb") jupytext(args=["notebook.ipynb", "--to", "py:percent"]) system("black", "notebook.py") jupytext(args=["notebook.py", "--to", "ipynb", "--update"]) - nb1 = read(nb_file) + nb1 = read(ipynb_py_file) nb2 = read("notebook.ipynb") nb3 = read("notebook.py") @@ -90,10 +89,9 @@ def test_pipe_into_flake8(): @pytest.mark.requires_black @pytest.mark.requires_flake8 -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1]) -def test_apply_black_through_jupytext(tmpdir, nb_file): +def test_apply_black_through_jupytext(tmpdir, python_notebook): # Load real notebook metadata to get the 'auto' extension in --pipe-fmt to work - metadata = read(nb_file).metadata + metadata = python_notebook.metadata nb_org = new_notebook( cells=[new_code_cell("1 +1", id="cell-id")], metadata=metadata @@ -142,10 +140,9 @@ def test_apply_black_through_jupytext(tmpdir, nb_file): @pytest.mark.requires_black -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1]) -def test_apply_black_and_sync_on_paired_notebook(tmpdir, cwd_tmpdir, nb_file): +def test_apply_black_and_sync_on_paired_notebook(tmpdir, cwd_tmpdir, python_notebook): # Load real notebook metadata to get the 'auto' extension in --pipe-fmt to work - metadata = read(nb_file).metadata + metadata = python_notebook.metadata metadata["jupytext"] = {"formats": "ipynb,py"} assert "language_info" in metadata diff --git a/tests/external/docs/__init__.py b/tests/external/docs/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/external/jupyter_fs/__init__.py b/tests/external/jupyter_fs/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/external/jupyter_fs/test_jupyter_fs.py b/tests/external/jupyter_fs/test_jupyter_fs.py index 92ef6d960..0b703e542 100644 --- a/tests/external/jupyter_fs/test_jupyter_fs.py +++ b/tests/external/jupyter_fs/test_jupyter_fs.py @@ -4,9 +4,7 @@ from nbformat.v4.nbbase import new_code_cell, new_markdown_cell, new_notebook import jupytext -from jupytext.compare import compare_cells - -from ...utils import notebook_model +from jupytext.compare import compare_cells, notebook_model def fs_meta_manager(tmpdir): diff --git a/tests/external/pre_commit/__init__.py b/tests/external/pre_commit/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/external/pre_commit/test_pre_commit_scripts.py b/tests/external/pre_commit/test_pre_commit_scripts.py index c61c78eff..1d2ec3531 100644 --- a/tests/external/pre_commit/test_pre_commit_scripts.py +++ b/tests/external/pre_commit/test_pre_commit_scripts.py @@ -9,8 +9,6 @@ from jupytext.cli import jupytext, system from jupytext.compare import compare_cells, compare_notebooks -from ...utils import list_notebooks - def git_in_tmpdir(tmpdir): """Return a function that will execute git instruction in the desired directory""" @@ -211,10 +209,9 @@ def test_pre_commit_hook_py_to_ipynb_and_md(tmpdir): @pytest.mark.requires_black @pytest.mark.requires_flake8 @pytest.mark.requires_jupytext -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1]) -def test_pre_commit_hook_sync_black_flake8(tmpdir, nb_file): +def test_pre_commit_hook_sync_black_flake8(tmpdir, python_notebook): # Load real notebook metadata to get the 'auto' extension in --pipe-fmt to work - metadata = read(nb_file).metadata + metadata = python_notebook.metadata git = git_in_tmpdir(tmpdir) hook = str(tmpdir.join(".git/hooks/pre-commit")) diff --git a/tests/external/round_trip/__init__.py b/tests/external/round_trip/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/external/round_trip/test_mirror_external.py b/tests/external/round_trip/test_mirror_external.py index 794cc66f5..70467a197 100644 --- a/tests/external/round_trip/test_mirror_external.py +++ b/tests/external/round_trip/test_mirror_external.py @@ -1,7 +1,6 @@ import pytest -from ...functional.round_trip.test_mirror import assert_conversion_same_as_mirror -from ...utils import list_notebooks +from jupytext.compare import assert_conversion_same_as_mirror """--------------------------------------------------------------------------------- @@ -11,36 +10,21 @@ @pytest.mark.requires_pandoc -@pytest.mark.parametrize( - "nb_file", - list_notebooks("ipynb", skip="(functional|Notebook with|flavors|invalid|305)"), -) -def test_ipynb_to_pandoc(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "md:pandoc", "ipynb_to_pandoc") +def test_ipynb_to_pandoc(ipynb_to_pandoc, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_to_pandoc, "md:pandoc", "ipynb_to_pandoc") @pytest.mark.requires_quarto -@pytest.mark.parametrize( - "nb_file", - list_notebooks( - "ipynb", - skip="(functional|Notebook with|plotly_graphs|flavors|complex_metadata|" - "update83|raw_cell|_66|nteract|LaTeX|invalid|305|text_outputs|ir_notebook|jupyter|with_R_magic)", - ), -) def test_ipynb_to_quarto( - nb_file, + ipynb_to_quarto, no_jupytext_version_number, ): - assert_conversion_same_as_mirror(nb_file, "qmd", "ipynb_to_quarto") + assert_conversion_same_as_mirror(ipynb_to_quarto, "qmd", "ipynb_to_quarto") @pytest.mark.requires_sphinx_gallery -@pytest.mark.parametrize( - "nb_file", list_notebooks("ipynb_py", skip="(raw|hash|frozen|magic|html|164|long)") -) -def test_ipynb_to_python_sphinx(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "py:sphinx", "ipynb_to_sphinx") +def test_ipynb_to_python_sphinx(ipynb_to_sphinx, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_to_sphinx, "py:sphinx", "ipynb_to_sphinx") """--------------------------------------------------------------------------------- @@ -50,22 +34,19 @@ def test_ipynb_to_python_sphinx(nb_file, no_jupytext_version_number): ---------------------------------------------------------------------------------""" -@pytest.mark.parametrize("nb_file", list_notebooks("Rmd")) -def test_Rmd_to_ipynb(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "ipynb", "Rmd_to_ipynb") +def test_Rmd_to_ipynb(rmd_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(rmd_file, "ipynb", "Rmd_to_ipynb") @pytest.mark.requires_sphinx_gallery -@pytest.mark.parametrize("nb_file", list_notebooks("sphinx")) -def test_sphinx_to_ipynb(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "ipynb:sphinx", "sphinx_to_ipynb") +def test_sphinx_to_ipynb(sphinx_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(sphinx_file, "ipynb:sphinx", "sphinx_to_ipynb") @pytest.mark.requires_sphinx_gallery -@pytest.mark.parametrize("nb_file", list_notebooks("sphinx")) -def test_sphinx_md_to_ipynb(nb_file, no_jupytext_version_number): +def test_sphinx_md_to_ipynb(sphinx_file, no_jupytext_version_number): assert_conversion_same_as_mirror( - nb_file, + sphinx_file, {"extension": ".ipynb", "format_name": "sphinx", "rst2md": True}, "sphinx-rst2md_to_ipynb", compare_notebook=True, diff --git a/tests/external/rst2md/__init__.py b/tests/external/rst2md/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/external/simple_external_notebooks/__init__.py b/tests/external/simple_external_notebooks/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/__init__.py b/tests/functional/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/cli/__init__.py b/tests/functional/cli/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/cli/test_cli.py b/tests/functional/cli/test_cli.py index aa2b3e65d..9123cfb47 100644 --- a/tests/functional/cli/test_cli.py +++ b/tests/functional/cli/test_cli.py @@ -21,8 +21,6 @@ from jupytext.paired_paths import InconsistentPath, paired_paths from jupytext.pandoc import is_pandoc_available -from ...utils import list_notebooks - def test_str2bool(): assert str2bool("d") is None @@ -32,20 +30,22 @@ def test_str2bool(): str2bool("UNEXPECTED") -@pytest.mark.parametrize("nb_file", list_notebooks()) -def test_cli_single_file(nb_file): - assert parse_jupytext_args([nb_file] + ["--to", "py"]).notebooks == [nb_file] +def test_cli_single_file(ipynb_py_R_jl_file): + assert parse_jupytext_args([ipynb_py_R_jl_file] + ["--to", "py"]).notebooks == [ + ipynb_py_R_jl_file + ] -@pytest.mark.parametrize("nb_files", [list_notebooks()]) -def test_cli_multiple_files(nb_files): - assert parse_jupytext_args(nb_files + ["--to", "py"]).notebooks == nb_files +def test_cli_multiple_files(ipynb_py_R_jl_files): + assert ( + parse_jupytext_args(ipynb_py_R_jl_files + ["--to", "py"]).notebooks + == ipynb_py_R_jl_files + ) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_convert_single_file_in_place(nb_file, tmpdir): - nb_org = str(tmpdir.join(os.path.basename(nb_file))) - copyfile(nb_file, nb_org) +def test_convert_single_file_in_place(ipynb_py_file, tmpdir): + nb_org = str(tmpdir.join(os.path.basename(ipynb_py_file))) + copyfile(ipynb_py_file, nb_org) base, ext = os.path.splitext(nb_org) nb_other = base + ".py" @@ -59,10 +59,9 @@ def test_convert_single_file_in_place(nb_file, tmpdir): @pytest.mark.requires_jupytext -def test_convert_single_file_in_place_m(tmpdir): - nb_file = list_notebooks("ipynb_py")[0] - nb_org = str(tmpdir.join(os.path.basename(nb_file))) - copyfile(nb_file, nb_org) +def test_convert_single_file_in_place_m(ipynb_py_file, tmpdir): + nb_org = str(tmpdir.join(os.path.basename(ipynb_py_file))) + copyfile(ipynb_py_file, nb_org) base, ext = os.path.splitext(nb_org) @@ -77,12 +76,11 @@ def test_convert_single_file_in_place_m(tmpdir): compare_notebooks(nb2, nb1) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb") + list_notebooks("Rmd")) -def test_convert_single_file(nb_file, tmpdir, capsys): - nb_org = str(tmpdir.join(os.path.basename(nb_file))) - copyfile(nb_file, nb_org) +def test_convert_single_file(ipynb_or_rmd_file, tmpdir, capsys): + nb_org = str(tmpdir.join(os.path.basename(ipynb_or_rmd_file))) + copyfile(ipynb_or_rmd_file, nb_org) - nb1 = read(nb_file) + nb1 = read(ipynb_or_rmd_file) pynb = writes(nb1, "py") jupytext([nb_org, "--to", "py", "-o", "-"]) @@ -119,10 +117,9 @@ def test_wildcard(tmpdir): jupytext(["nb3.ipynb", "--to", "py"]) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_cpp")) -def test_to_cpluplus(nb_file, tmpdir, capsys): - nb_org = str(tmpdir.join(os.path.basename(nb_file))) - copyfile(nb_file, nb_org) +def test_to_cpluplus(ipynb_cpp_file, tmpdir, capsys): + nb_org = str(tmpdir.join(os.path.basename(ipynb_cpp_file))) + copyfile(ipynb_cpp_file, nb_org) nb1 = read(nb_org) text_cpp = writes(nb1, "cpp") @@ -133,12 +130,11 @@ def test_to_cpluplus(nb_file, tmpdir, capsys): compare(out, text_cpp) -@pytest.mark.parametrize("nb_files", [list_notebooks("ipynb_py")]) -def test_convert_multiple_file(nb_files, tmpdir): +def test_convert_multiple_file(ipynb_py_files, tmpdir): nb_orgs = [] nb_others = [] - for nb_file in nb_files: + for nb_file in ipynb_py_files: nb_org = str(tmpdir.join(os.path.basename(nb_file))) base, ext = os.path.splitext(nb_org) nb_other = base + ".py" @@ -303,13 +299,12 @@ def test_combine_lower_version_raises(tmpdir): jupytext([tmp_nbpy, "--to", "ipynb", "--update"]) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_ipynb_to_py_then_update_test(nb_file, tmpdir): +def test_ipynb_to_py_then_update_test(ipynb_py_file, tmpdir): """Reproduce https://github.com/mwouts/jupytext/issues/83""" tmp_ipynb = str(tmpdir.join("notebook.ipynb")) tmp_nbpy = str(tmpdir.join("notebook.py")) - copyfile(nb_file, tmp_ipynb) + copyfile(ipynb_py_file, tmp_ipynb) jupytext(["--to", "py", tmp_ipynb]) jupytext(["--test", "--update", "--to", "ipynb", tmp_nbpy]) @@ -344,12 +339,11 @@ def test_test_to_ipynb_ignore_version_number_414( assert jupytext(["--test", "--to", "ipynb", tmp_py]) == 0 -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_convert_to_percent_format(nb_file, tmpdir): +def test_convert_to_percent_format(ipynb_py_file, tmpdir): tmp_ipynb = str(tmpdir.join("notebook.ipynb")) tmp_nbpy = str(tmpdir.join("notebook.py")) - copyfile(nb_file, tmp_ipynb) + copyfile(ipynb_py_file, tmp_ipynb) jupytext(["--to", "py:percent", tmp_ipynb]) @@ -363,12 +357,11 @@ def test_convert_to_percent_format(nb_file, tmpdir): compare_notebooks(nb2, nb1) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_convert_to_percent_format_and_keep_magics(nb_file, tmpdir): +def test_convert_to_percent_format_and_keep_magics(ipynb_py_file, tmpdir): tmp_ipynb = str(tmpdir.join("notebook.ipynb")) tmp_nbpy = str(tmpdir.join("notebook.py")) - copyfile(nb_file, tmp_ipynb) + copyfile(ipynb_py_file, tmp_ipynb) jupytext(["--to", "py:percent", "--opt", "comment_magics=False", tmp_ipynb]) @@ -384,24 +377,22 @@ def test_convert_to_percent_format_and_keep_magics(nb_file, tmpdir): compare_notebooks(nb2, nb1) -@pytest.mark.parametrize("py_file", list_notebooks("python")) -def test_set_formats(py_file, tmpdir): +def test_set_formats(python_file, tmpdir): tmp_py = str(tmpdir.join("notebook.py")) tmp_ipynb = str(tmpdir.join("notebook.ipynb")) - copyfile(py_file, tmp_py) + copyfile(python_file, tmp_py) jupytext([tmp_py, "--set-formats", "ipynb,py:light"]) nb = read(tmp_ipynb) assert nb.metadata["jupytext"]["formats"] == "ipynb,py:light" -@pytest.mark.parametrize("py_file", list_notebooks("python")) -def test_update_metadata(py_file, tmpdir, capsys): +def test_update_metadata(python_file, tmpdir, capsys): tmp_py = str(tmpdir.join("notebook.py")) tmp_ipynb = str(tmpdir.join("notebook.ipynb")) - copyfile(py_file, tmp_py) + copyfile(python_file, tmp_py) jupytext( [ @@ -431,11 +422,10 @@ def test_update_metadata(py_file, tmpdir, capsys): @pytest.mark.requires_user_kernel_python3 -@pytest.mark.parametrize("py_file", list_notebooks("python")) -def test_set_kernel_inplace(py_file, tmpdir): +def test_set_kernel_inplace(python_file, tmpdir): tmp_py = str(tmpdir.join("notebook.py")) - copyfile(py_file, tmp_py) + copyfile(python_file, tmp_py) jupytext([tmp_py, "--set-kernel", "-"]) @@ -446,12 +436,11 @@ def test_set_kernel_inplace(py_file, tmpdir): @pytest.mark.requires_user_kernel_python3 -@pytest.mark.parametrize("py_file", list_notebooks("python")) -def test_set_kernel_auto(py_file, tmpdir): +def test_set_kernel_auto(python_file, tmpdir): tmp_py = str(tmpdir.join("notebook.py")) tmp_ipynb = str(tmpdir.join("notebook.ipynb")) - copyfile(py_file, tmp_py) + copyfile(python_file, tmp_py) jupytext(["--to", "ipynb", tmp_py, "--set-kernel", "-"]) @@ -462,12 +451,11 @@ def test_set_kernel_auto(py_file, tmpdir): @pytest.mark.requires_user_kernel_python3 -@pytest.mark.parametrize("py_file", list_notebooks("python")) -def test_set_kernel_with_name(py_file, tmpdir): +def test_set_kernel_with_name(python_file, tmpdir): tmp_py = str(tmpdir.join("notebook.py")) tmp_ipynb = str(tmpdir.join("notebook.ipynb")) - copyfile(py_file, tmp_py) + copyfile(python_file, tmp_py) for kernel in find_kernel_specs(): jupytext(["--to", "ipynb", tmp_py, "--set-kernel", kernel]) @@ -479,10 +467,9 @@ def test_set_kernel_with_name(py_file, tmpdir): jupytext(["--to", "ipynb", tmp_py, "--set-kernel", "non_existing_env"]) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_paired_paths(nb_file, tmpdir, capsys): +def test_paired_paths(ipynb_py_file, tmpdir, capsys): tmp_ipynb = str(tmpdir.join("notebook.ipynb")) - nb = read(nb_file) + nb = read(ipynb_py_file) nb.metadata.setdefault("jupytext", {})[ "formats" ] = "ipynb,_light.py,_percent.py:percent" @@ -499,12 +486,11 @@ def test_paired_paths(nb_file, tmpdir, capsys): } -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_sync(nb_file, tmpdir, cwd_tmpdir, capsys): +def test_sync(ipynb_py_file, tmpdir, cwd_tmpdir, capsys): tmp_ipynb = "notebook.ipynb" tmp_py = "notebook.py" tmp_rmd = "notebook.Rmd" - nb = read(nb_file) + nb = read(ipynb_py_file) write(nb, tmp_ipynb) # Test that sync issues a warning when the notebook is not paired @@ -551,13 +537,10 @@ def test_sync(nb_file, tmpdir, cwd_tmpdir, capsys): @pytest.mark.requires_pandoc -@pytest.mark.parametrize( - "nb_file", list_notebooks("ipynb_py", skip="(Notebook with|flavors|305)") -) -def test_sync_pandoc(nb_file, tmpdir, cwd_tmpdir, capsys): +def test_sync_pandoc(ipynb_to_pandoc, tmpdir, cwd_tmpdir, capsys): tmp_ipynb = "notebook.ipynb" tmp_md = "notebook.md" - nb = read(nb_file) + nb = read(ipynb_to_pandoc) write(nb, tmp_ipynb) # Test that sync issues a warning when the notebook is not paired @@ -579,16 +562,12 @@ def test_sync_pandoc(nb_file, tmpdir, cwd_tmpdir, capsys): assert "pandoc" in fp.read() -@pytest.mark.parametrize( - "nb_file,ext", - [(nb_file, ".py") for nb_file in list_notebooks("ipynb_py")] - + [(nb_file, ".R") for nb_file in list_notebooks("ipynb_R")] - + [(nb_file, ".jl") for nb_file in list_notebooks("ipynb_julia")], -) -def test_cli_can_infer_jupytext_format(nb_file, ext, tmpdir, cwd_tmpdir): +def test_cli_can_infer_jupytext_format( + ipynb_py_R_jl_file, ipynb_py_R_jl_ext, tmpdir, cwd_tmpdir +): tmp_ipynb = "notebook.ipynb" - tmp_text = "notebook" + ext - nb = read(nb_file) + tmp_text = "notebook" + ipynb_py_R_jl_ext + nb = read(ipynb_py_R_jl_file) # Light format to Jupyter notebook write(nb, tmp_text) @@ -597,21 +576,16 @@ def test_cli_can_infer_jupytext_format(nb_file, ext, tmpdir, cwd_tmpdir): compare_notebooks(nb2, nb) # Percent format to Jupyter notebook - write(nb, tmp_text, fmt=ext + ":percent") + write(nb, tmp_text, fmt=ipynb_py_R_jl_ext + ":percent") jupytext(["--to", "notebook", tmp_text]) nb2 = read(tmp_ipynb) compare_notebooks(nb2, nb) -@pytest.mark.parametrize( - "nb_file,ext", - [(nb_file, ".py") for nb_file in list_notebooks("ipynb_py")] - + [(nb_file, ".R") for nb_file in list_notebooks("ipynb_R")], -) -def test_cli_to_script(nb_file, ext, tmpdir, cwd_tmpdir): +def test_cli_to_script(ipynb_py_R_jl_file, ipynb_py_R_jl_ext, tmpdir, cwd_tmpdir): tmp_ipynb = "notebook.ipynb" - tmp_text = "notebook" + ext - nb = read(nb_file) + tmp_text = "notebook" + ipynb_py_R_jl_ext + nb = read(ipynb_py_R_jl_file) write(nb, tmp_ipynb) jupytext(["--to", "script", tmp_ipynb]) @@ -619,15 +593,10 @@ def test_cli_to_script(nb_file, ext, tmpdir, cwd_tmpdir): compare_notebooks(nb2, nb) -@pytest.mark.parametrize( - "nb_file,ext", - [(nb_file, ".py") for nb_file in list_notebooks("ipynb_py")] - + [(nb_file, ".R") for nb_file in list_notebooks("ipynb_R")], -) -def test_cli_to_auto(nb_file, ext, tmpdir, cwd_tmpdir): +def test_cli_to_auto(ipynb_py_R_jl_file, ipynb_py_R_jl_ext, tmpdir, cwd_tmpdir): tmp_ipynb = "notebook.ipynb" - tmp_text = "notebook" + ext - nb = read(nb_file) + tmp_text = "notebook" + ipynb_py_R_jl_ext + nb = read(ipynb_py_R_jl_file) write(nb, tmp_ipynb) jupytext(["--to", "auto", tmp_ipynb]) @@ -635,15 +604,14 @@ def test_cli_to_auto(nb_file, ext, tmpdir, cwd_tmpdir): compare_notebooks(nb2, nb) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_cli_can_infer_jupytext_format_from_stdin(nb_file, tmpdir, cwd_tmpdir): +def test_cli_can_infer_jupytext_format_from_stdin(ipynb_py_file, tmpdir, cwd_tmpdir): tmp_ipynb = "notebook.ipynb" tmp_py = "notebook.py" tmp_rmd = "notebook.Rmd" - nb = read(nb_file) + nb = read(ipynb_py_file) # read ipynb notebook on stdin, write to python - with open(nb_file) as fp, mock.patch("sys.stdin", fp): + with open(ipynb_py_file) as fp, mock.patch("sys.stdin", fp): jupytext(["--to", "py:percent", "-o", tmp_py]) nb2 = read(tmp_py) compare_notebooks(nb2, nb) @@ -655,7 +623,7 @@ def test_cli_can_infer_jupytext_format_from_stdin(nb_file, tmpdir, cwd_tmpdir): compare_notebooks(nb2, nb) # read ipynb notebook on stdin, write to R markdown - with open(nb_file) as fp, mock.patch("sys.stdin", fp): + with open(ipynb_py_file) as fp, mock.patch("sys.stdin", fp): jupytext(["-o", tmp_rmd]) nb2 = read(tmp_rmd) compare_notebooks(nb2, nb, "Rmd") @@ -845,26 +813,25 @@ def test_remove_jupytext_metadata(tmpdir, cwd_tmpdir): } -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) @pytest.mark.parametrize("fmt", ["py:light", "py:percent", "md"]) -def test_convert_and_update_preserves_notebook(nb_file, fmt, tmpdir, cwd_tmpdir): +def test_convert_and_update_preserves_notebook(ipynb_py_file, fmt, tmpdir, cwd_tmpdir): # cannot encode magic parameters in markdown yet - if ("magic" in nb_file or "LateX" in nb_file) and fmt == "md": + if ("magic" in ipynb_py_file or "LateX" in ipynb_py_file) and fmt == "md": return tmp_ipynb = "notebook.ipynb" - copyfile(nb_file, tmp_ipynb) + copyfile(ipynb_py_file, tmp_ipynb) ext = long_form_one_format(fmt)["extension"] tmp_text = "notebook" + ext jupytext(["--to", fmt, tmp_ipynb]) jupytext(["--to", "ipynb", "--update", tmp_text]) - nb_org = read(nb_file) + nb_org = read(ipynb_py_file) nb_now = read(tmp_ipynb) # The cell marker changes from """ to r""" on the LateX notebook #836 - if "LateX" in nb_file and fmt == "py:percent": + if "LateX" in ipynb_py_file and fmt == "py:percent": last_cell = nb_now.cells[-1] last_cell.metadata["cell_marker"] = last_cell.metadata["cell_marker"][1:] diff --git a/tests/functional/config/__init__.py b/tests/functional/config/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/docs/__init__.py b/tests/functional/docs/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/metadata/__init__.py b/tests/functional/metadata/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/others/__init__.py b/tests/functional/others/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/others/test_auto_ext.py b/tests/functional/others/test_auto_ext.py index e70785ce2..e427ce273 100644 --- a/tests/functional/others/test_auto_ext.py +++ b/tests/functional/others/test_auto_ext.py @@ -3,14 +3,9 @@ from jupytext import read, reads, writes from jupytext.formats import JupytextFormatError, auto_ext_from_metadata -from ...utils import list_notebooks - -@pytest.mark.parametrize( - "nb_file", list_notebooks("ipynb_R") + list_notebooks("ipynb_py") -) -def test_auto_in_fmt(nb_file): - nb = read(nb_file) +def test_auto_in_fmt(ipynb_py_R_file): + nb = read(ipynb_py_R_file) auto_ext = auto_ext_from_metadata(nb.metadata) fmt = auto_ext[1:] + ":percent" text = writes(nb, "auto:percent") @@ -27,9 +22,8 @@ def test_auto_in_fmt(nb_file): writes(nb, "auto:percent") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_all")) -def test_auto_from_kernelspecs_works(nb_file): - nb = read(nb_file) +def test_auto_from_kernelspecs_works(ipynb_file): + nb = read(ipynb_file) language_info = nb.metadata.pop("language_info") expected_ext = language_info.get("file_extension") if not expected_ext: @@ -46,12 +40,10 @@ def test_auto_from_kernelspecs_works(nb_file): assert auto_ext == expected_ext -@pytest.mark.parametrize( - "nb_file", - list_notebooks("ipynb_R") + list_notebooks("ipynb_py", skip="(plotly)"), -) -def test_auto_in_formats(nb_file): - nb = read(nb_file) +def test_auto_in_formats(ipynb_py_R_jl_file): + if any(pattern in ipynb_py_R_jl_file for pattern in ["plotly", "julia"]): + pytest.skip() + nb = read(ipynb_py_R_jl_file) nb.metadata["jupytext"] = {"formats": "ipynb,auto:percent"} fmt = auto_ext_from_metadata(nb.metadata)[1:] + ":percent" expected_formats = "ipynb," + fmt diff --git a/tests/functional/others/test_cell_tags_are_preserved.py b/tests/functional/others/test_cell_tags_are_preserved.py index 01552513c..2069e40cf 100644 --- a/tests/functional/others/test_cell_tags_are_preserved.py +++ b/tests/functional/others/test_cell_tags_are_preserved.py @@ -2,7 +2,7 @@ from nbformat.v4.nbbase import new_code_cell, new_markdown_cell from jupytext import reads, writes -from tests.utils import formats_with_support_for_cell_metadata, is_myst_available +from jupytext.formats import formats_with_support_for_cell_metadata, is_myst_available @pytest.fixture() diff --git a/tests/functional/others/test_combine.py b/tests/functional/others/test_combine.py index 6968d53f5..c801f8fba 100644 --- a/tests/functional/others/test_combine.py +++ b/tests/functional/others/test_combine.py @@ -1,12 +1,10 @@ from copy import deepcopy -import pytest from nbformat.v4.nbbase import new_code_cell, new_markdown_cell, new_notebook import jupytext from jupytext.combine import combine_inputs_with_outputs from jupytext.compare import compare, compare_notebooks -from tests.utils import list_notebooks def test_combine(): @@ -132,9 +130,8 @@ def test_read_text_and_combine_with_outputs(tmpdir): assert len(nb.cells) == 3 -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_all")) -def test_combine_stable(nb_file): - nb_org = jupytext.read(nb_file) +def test_combine_stable(ipynb_file): + nb_org = jupytext.read(ipynb_file) nb_source = deepcopy(nb_org) nb_outputs = deepcopy(nb_org) diff --git a/tests/functional/others/test_sample_notebooks_are_normalized.py b/tests/functional/others/test_sample_notebooks_are_normalized.py index 2a6922f85..573fe5d68 100644 --- a/tests/functional/others/test_sample_notebooks_are_normalized.py +++ b/tests/functional/others/test_sample_notebooks_are_normalized.py @@ -3,19 +3,16 @@ import jupytext -from ...utils import list_notebooks - @pytest.mark.skipif(nbformat.__version__ <= "5.7", reason="normalize is not available") -@pytest.mark.parametrize("nb_file", list_notebooks("all", skip="(invalid|pyc)")) -def test_sample_notebooks_are_normalized(nb_file): - nb = jupytext.read(nb_file) +def test_sample_notebooks_are_normalized(any_nb_file): + nb = jupytext.read(any_nb_file) changes, normalized_nb = nbformat.validator.normalize(nb) nbformat.validate(normalized_nb) if changes: # pragma: no cover - with open(nb_file, "w") as fp: + with open(any_nb_file, "w") as fp: jupytext.write(normalized_nb, fp) assert not changes diff --git a/tests/functional/others/test_save_multiple.py b/tests/functional/others/test_save_multiple.py index 4d92f0abd..0e23c79c3 100644 --- a/tests/functional/others/test_save_multiple.py +++ b/tests/functional/others/test_save_multiple.py @@ -6,15 +6,12 @@ from tornado.web import HTTPError import jupytext -from jupytext.compare import compare_notebooks +from jupytext.compare import compare_notebooks, notebook_model from jupytext.contentsmanager import TextFileContentsManager -from ...utils import list_notebooks, notebook_model - -@pytest.mark.parametrize("nb_file", list_notebooks(skip="66")) -def test_rmd_is_ok(nb_file, tmpdir): - nb = jupytext.read(nb_file) +def test_rmd_is_ok(ipynb_file, tmpdir): + nb = jupytext.read(ipynb_file) tmp_ipynb = "notebook.ipynb" tmp_rmd = "notebook.Rmd" @@ -30,9 +27,8 @@ def test_rmd_is_ok(nb_file, tmpdir): compare_notebooks(nb2, nb, "Rmd") -@pytest.mark.parametrize("nb_file", list_notebooks("Rmd")) -def test_ipynb_is_ok(nb_file, tmpdir): - nb = jupytext.read(nb_file) +def test_ipynb_is_ok(rmd_file, tmpdir): + nb = jupytext.read(rmd_file) tmp_ipynb = "notebook.ipynb" tmp_rmd = "notebook.Rmd" @@ -46,9 +42,8 @@ def test_ipynb_is_ok(nb_file, tmpdir): compare_notebooks(nb2, nb) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py", skip="66")) -def test_all_files_created(nb_file, tmpdir): - nb = jupytext.read(nb_file) +def test_all_files_created(ipynb_py_file, tmpdir): + nb = jupytext.read(ipynb_py_file) tmp_ipynb = "notebook.ipynb" tmp_rmd = "notebook.Rmd" tmp_py = "notebook.py" diff --git a/tests/functional/others/test_trust_notebook.py b/tests/functional/others/test_trust_notebook.py index cc1bc36d6..05c4b24a8 100644 --- a/tests/functional/others/test_trust_notebook.py +++ b/tests/functional/others/test_trust_notebook.py @@ -7,37 +7,36 @@ from jupytext.compare import compare_notebooks from jupytext.contentsmanager import TextFileContentsManager -from ...utils import list_notebooks - -@pytest.mark.parametrize("nb_file", list_notebooks("python")) -def test_py_notebooks_are_trusted(nb_file): +def test_py_notebooks_are_trusted(python_file): cm = TextFileContentsManager() - root, file = os.path.split(nb_file) + root, file = os.path.split(python_file) cm.root_dir = root nb = cm.get(file) for cell in nb["content"].cells: assert cell.metadata.get("trusted", True) -@pytest.mark.parametrize("nb_file", list_notebooks("Rmd")) -def test_rmd_notebooks_are_trusted(nb_file): +def test_rmd_notebooks_are_trusted(rmd_file): cm = TextFileContentsManager() - root, file = os.path.split(nb_file) + root, file = os.path.split(rmd_file) cm.root_dir = root nb = cm.get(file) for cell in nb["content"].cells: assert cell.metadata.get("trusted", True) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py", skip="hash sign")) -def test_ipynb_notebooks_can_be_trusted(nb_file, tmpdir, no_jupytext_version_number): +def test_ipynb_notebooks_can_be_trusted( + ipynb_py_file, tmpdir, no_jupytext_version_number +): + if "hash sign" in ipynb_py_file: + pytest.skip() cm = TextFileContentsManager() - root, file = os.path.split(nb_file) + root, file = os.path.split(ipynb_py_file) tmp_ipynb = str(tmpdir.join(file)) py_file = file.replace(".ipynb", ".py") tmp_py = str(tmpdir.join(py_file)) - shutil.copy(nb_file, tmp_ipynb) + shutil.copy(ipynb_py_file, tmp_ipynb) cm.formats = "ipynb,py" cm.root_dir = str(tmpdir) @@ -78,16 +77,17 @@ def test_ipynb_notebooks_can_be_trusted(nb_file, tmpdir, no_jupytext_version_num cm.trust_notebook(file) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py", skip="hash sign")) def test_ipynb_notebooks_can_be_trusted_even_with_metadata_filter( - nb_file, tmpdir, no_jupytext_version_number + ipynb_py_file, tmpdir, no_jupytext_version_number ): + if "hash sign" in ipynb_py_file: + pytest.skip() cm = TextFileContentsManager() - root, file = os.path.split(nb_file) + root, file = os.path.split(ipynb_py_file) tmp_ipynb = str(tmpdir.join(file)) py_file = file.replace(".ipynb", ".py") tmp_py = str(tmpdir.join(py_file)) - shutil.copy(nb_file, tmp_ipynb) + shutil.copy(ipynb_py_file, tmp_ipynb) cm.formats = "ipynb,py" cm.notebook_metadata_filter = "all" @@ -119,12 +119,13 @@ def test_ipynb_notebooks_can_be_trusted_even_with_metadata_filter( compare_notebooks(nb2["content"], model["content"]) -@pytest.mark.parametrize("nb_file", list_notebooks("percent", skip="hash sign")) -def test_text_notebooks_can_be_trusted(nb_file, tmpdir, no_jupytext_version_number): +def test_text_notebooks_can_be_trusted( + percent_file, tmpdir, no_jupytext_version_number +): cm = TextFileContentsManager() - root, file = os.path.split(nb_file) + root, file = os.path.split(percent_file) py_file = str(tmpdir.join(file)) - shutil.copy(nb_file, py_file) + shutil.copy(percent_file, py_file) cm.root_dir = str(tmpdir) model = cm.get(file) diff --git a/tests/functional/others/test_unicode.py b/tests/functional/others/test_unicode.py index 0946058a7..0dbc632a0 100644 --- a/tests/functional/others/test_unicode.py +++ b/tests/functional/others/test_unicode.py @@ -1,15 +1,18 @@ -import pytest from nbformat.v4.nbbase import new_markdown_cell, new_notebook import jupytext from jupytext.compare import compare -from ...utils import list_notebooks +def test_notebook_contents_is_unicode(ipynb_file): + nb = jupytext.read(ipynb_file) -@pytest.mark.parametrize("nb_file", list_notebooks() + list_notebooks("Rmd")) -def test_notebook_contents_is_unicode(nb_file): - nb = jupytext.read(nb_file) + for cell in nb.cells: + assert isinstance(cell.source, str) + + +def test_rmd_notebook_contents_is_unicode(rmd_file): + nb = jupytext.read(rmd_file) for cell in nb.cells: assert isinstance(cell.source, str) diff --git a/tests/functional/others/test_write_does_not_modify_notebook.py b/tests/functional/others/test_write_does_not_modify_notebook.py index 492743d58..76948a81a 100644 --- a/tests/functional/others/test_write_does_not_modify_notebook.py +++ b/tests/functional/others/test_write_does_not_modify_notebook.py @@ -1,5 +1,4 @@ from copy import deepcopy -from itertools import product import pytest @@ -7,18 +6,13 @@ from jupytext.compare import compare from jupytext.formats import long_form_one_format -from ...utils import list_notebooks - @pytest.mark.parametrize( - "nb_file,fmt", - product( - list_notebooks("ipynb_py") + list_notebooks("ipynb_R"), - ["auto:light", "auto:percent", "md", ".Rmd", ".ipynb"], - ), + "fmt", + ["auto:light", "auto:percent", "md", ".Rmd", ".ipynb"], ) -def test_write_notebook_does_not_change_it(nb_file, fmt, tmpdir): - nb_org = read(nb_file) +def test_write_notebook_does_not_change_it(ipynb_py_R_jl_file, fmt, tmpdir): + nb_org = read(ipynb_py_R_jl_file) nb_org_copied = deepcopy(nb_org) ext = long_form_one_format(fmt, nb_org.metadata)["extension"] diff --git a/tests/functional/round_trip/__init__.py b/tests/functional/round_trip/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/round_trip/test_jupytext_nbconvert_round_trip.py b/tests/functional/round_trip/test_jupytext_nbconvert_round_trip.py index cb60523fc..e259df7a0 100644 --- a/tests/functional/round_trip/test_jupytext_nbconvert_round_trip.py +++ b/tests/functional/round_trip/test_jupytext_nbconvert_round_trip.py @@ -3,14 +3,13 @@ import jupytext from jupytext.header import header_to_metadata_and_cell -from ...utils import list_notebooks - @pytest.mark.requires_nbconvert -@pytest.mark.parametrize("md_file", list_notebooks("md", skip="jupytext")) def test_markdown_jupytext_nbconvert_is_identity(md_file): """Test that a Markdown file, converted to a notebook, then exported back to Markdown with nbconvert, yields the original file""" + if "jupytext" in md_file: + pytest.skip() with open(md_file) as fp: md_org = fp.read() @@ -40,11 +39,12 @@ def test_markdown_jupytext_nbconvert_is_identity(md_file): @pytest.mark.requires_nbconvert -@pytest.mark.parametrize("nb_file", list_notebooks(skip="(html|magic)")) -def test_jupytext_markdown_similar_to_nbconvert(nb_file): +def test_jupytext_markdown_similar_to_nbconvert(ipynb_py_R_jl_file): """Test that the nbconvert export for a notebook matches Jupytext's one""" + if "magic" in ipynb_py_R_jl_file or "html" in ipynb_py_R_jl_file: + pytest.skip() - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) # Remove cell outputs and metadata for cell in nb.cells: diff --git a/tests/functional/round_trip/test_mirror.py b/tests/functional/round_trip/test_mirror.py index 64c7442fa..8a04f75be 100644 --- a/tests/functional/round_trip/test_mirror.py +++ b/tests/functional/round_trip/test_mirror.py @@ -4,26 +4,19 @@ """ import os +import re import pytest from nbformat.v4.nbbase import new_notebook import jupytext -from jupytext.compare import combine_inputs_with_outputs, compare, compare_notebooks -from jupytext.formats import ( - auto_ext_from_metadata, - check_auto_ext, - long_form_one_format, +from jupytext.compare import ( + assert_conversion_same_as_mirror, + compare_notebooks, + create_mirror_file_if_missing, ) +from jupytext.formats import auto_ext_from_metadata from jupytext.languages import _SCRIPT_EXTENSIONS -from jupytext.paired_paths import full_path - -from ...utils import list_notebooks - - -def create_mirror_file_if_missing(mirror_file, notebook, fmt): - if not os.path.isfile(mirror_file): - jupytext.write(notebook, mirror_file, fmt=fmt) def test_create_mirror_file_if_missing(tmpdir, no_jupytext_version_number): @@ -33,64 +26,6 @@ def test_create_mirror_file_if_missing(tmpdir, no_jupytext_version_number): assert os.path.isfile(py_file) -def assert_conversion_same_as_mirror(nb_file, fmt, mirror_name, compare_notebook=False): - dirname, basename = os.path.split(nb_file) - file_name, org_ext = os.path.splitext(basename) - fmt = long_form_one_format(fmt) - notebook = jupytext.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") - 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 = jupytext.read(mirror_file, as_version=notebook.nbformat) - nb_mirror.nbformat_minor = notebook.nbformat_minor - compare_notebooks(nb_mirror, notebook) - return - elif ext == ".ipynb": - notebook = jupytext.read(mirror_file) - fmt.update({"extension": org_ext}) - actual = jupytext.writes(notebook, fmt) - with open(nb_file, encoding="utf-8") as fp: - expected = fp.read() - else: - actual = jupytext.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 = jupytext.read(nb_file) - nb_mirror = jupytext.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) - - """--------------------------------------------------------------------------------- Part I: ipynb -> fmt -> ipynb @@ -98,40 +33,33 @@ def assert_conversion_same_as_mirror(nb_file, fmt, mirror_name, compare_notebook ---------------------------------------------------------------------------------""" -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_all", skip="many hash")) -def test_ipynb_to_light(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "auto", "ipynb_to_script") +def test_ipynb_to_percent(ipynb_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_file, "auto:percent", "ipynb_to_percent") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_all", skip="")) -def test_ipynb_to_percent(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "auto:percent", "ipynb_to_percent") +def test_ipynb_to_hydrogen(ipynb_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_file, "auto:hydrogen", "ipynb_to_hydrogen") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_all", skip="")) -def test_ipynb_to_hydrogen(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "auto:hydrogen", "ipynb_to_hydrogen") +def test_ipynb_to_light(ipynb_to_light, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_to_light, "auto", "ipynb_to_script") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_all", skip="")) -def test_ipynb_to_md(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "md", "ipynb_to_md") +def test_ipynb_to_md(ipynb_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_file, "md", "ipynb_to_md") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_all", skip="")) -def test_ipynb_to_Rmd(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "Rmd", "ipynb_to_Rmd") +def test_ipynb_to_Rmd(ipynb_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_file, "Rmd", "ipynb_to_Rmd") @pytest.mark.requires_myst -@pytest.mark.parametrize( - "nb_file", - list_notebooks( - "ipynb_all", skip="html-demo|julia_functional_geometry|xcpp_by_quantstack" - ), -) -def test_ipynb_to_myst(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "md:myst", "ipynb_to_myst") +def test_ipynb_to_myst(ipynb_file, no_jupytext_version_number): + if re.match( + r".*(html-demo|julia_functional_geometry|xcpp_by_quantstack).*", ipynb_file + ): + pytest.skip() + assert_conversion_same_as_mirror(ipynb_file, "md:myst", "ipynb_to_myst") """--------------------------------------------------------------------------------- @@ -141,35 +69,24 @@ def test_ipynb_to_myst(nb_file, no_jupytext_version_number): ---------------------------------------------------------------------------------""" -@pytest.mark.parametrize( - "nb_file", - list_notebooks("julia") - + list_notebooks("python") - + list_notebooks("R") - + list_notebooks("ps1"), -) -def test_script_to_ipynb(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "ipynb", "script_to_ipynb") +def test_script_to_ipynb(script_to_ipynb, no_jupytext_version_number): + assert_conversion_same_as_mirror(script_to_ipynb, "ipynb", "script_to_ipynb") -@pytest.mark.parametrize("nb_file", list_notebooks("percent")) -def test_percent_to_ipynb(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "ipynb:percent", "script_to_ipynb") +def test_percent_to_ipynb(percent_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(percent_file, "ipynb:percent", "script_to_ipynb") -@pytest.mark.parametrize("nb_file", list_notebooks("hydrogen")) -def test_hydrogen_to_ipynb(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "ipynb:hydrogen", "script_to_ipynb") +def test_hydrogen_to_ipynb(hydrogen_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(hydrogen_file, "ipynb:hydrogen", "script_to_ipynb") -@pytest.mark.parametrize("nb_file", list_notebooks("R_spin")) -def test_spin_to_ipynb(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "ipynb:spin", "script_to_ipynb") +def test_spin_to_ipynb(r_spin_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(r_spin_file, "ipynb:spin", "script_to_ipynb") -@pytest.mark.parametrize("nb_file", list_notebooks("md")) -def test_md_to_ipynb(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "ipynb", "md_to_ipynb") +def test_md_to_ipynb(md_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(md_file, "ipynb", "md_to_ipynb") """--------------------------------------------------------------------------------- @@ -179,9 +96,8 @@ def test_md_to_ipynb(nb_file, no_jupytext_version_number): ---------------------------------------------------------------------------------""" -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_all", skip="")) -def test_ipynb_to_percent_to_light(nb_file): - nb = jupytext.read(nb_file) +def test_ipynb_to_percent_to_light(ipynb_file): + nb = jupytext.read(ipynb_file) pct = jupytext.writes(nb, "auto:percent") auto_ext = auto_ext_from_metadata(nb.metadata) comment = _SCRIPT_EXTENSIONS[auto_ext]["comment"] @@ -197,65 +113,47 @@ def test_ipynb_to_percent_to_light(nb_file): compare_notebooks(nb2, nb) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py", skip="")) -def test_ipynb_to_python_vim(nb_file, no_jupytext_version_number): +def test_ipynb_to_python_vim(ipynb_py_file, no_jupytext_version_number): assert_conversion_same_as_mirror( - nb_file, + ipynb_py_file, {"extension": ".py", "cell_markers": "{{{,}}}"}, "ipynb_to_script_vim_folding_markers", ) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py", skip="")) -def test_ipynb_to_python_vscode(nb_file, no_jupytext_version_number): +def test_ipynb_to_python_vscode(ipynb_py_file, no_jupytext_version_number): assert_conversion_same_as_mirror( - nb_file, + ipynb_py_file, {"extension": ".py", "cell_markers": "region,endregion"}, "ipynb_to_script_vscode_folding_markers", ) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_R")) -def test_ipynb_to_r(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, ".low.r", "ipynb_to_script") +def test_ipynb_to_r(ipynb_R_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_R_file, ".low.r", "ipynb_to_script") -@pytest.mark.parametrize( - "nb_file,extension", - [ - (nb_file, extension) - for nb_file in list_notebooks("ipynb_scheme") - for extension in ("ss", "scm") - ], -) -def test_ipynb_to_scheme(nb_file, extension, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, extension, "ipynb_to_script") +def test_ipynb_to_r_percent(ipynb_R_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_R_file, ".low.r:percent", "ipynb_to_percent") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_R")) -def test_ipynb_to_r_percent(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, ".low.r:percent", "ipynb_to_percent") +def test_ipynb_to_R_spin(ipynb_R_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_R_file, "R", "ipynb_to_spin") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_R")) -def test_ipynb_to_R_spin(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, "R", "ipynb_to_spin") +def test_ipynb_to_r_spin(ipynb_R_file, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_R_file, ".low.r", "ipynb_to_spin") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_R")) -def test_ipynb_to_r_spin(nb_file, no_jupytext_version_number): - assert_conversion_same_as_mirror(nb_file, ".low.r", "ipynb_to_spin") +@pytest.mark.parametrize("extension", ("ss", "scm")) +def test_ipynb_to_scheme(ipynb_scheme_file, extension, no_jupytext_version_number): + assert_conversion_same_as_mirror(ipynb_scheme_file, extension, "ipynb_to_script") -@pytest.mark.parametrize( - "nb_file,extension", - [ - (nb_file, extension) - for nb_file in list_notebooks("ipynb_scheme") - for extension in ("ss", "scm") - ], -) -def test_ipynb_to_scheme_percent(nb_file, extension, no_jupytext_version_number): +@pytest.mark.parametrize("extension", ("ss", "scm")) +def test_ipynb_to_scheme_percent( + ipynb_scheme_file, extension, no_jupytext_version_number +): assert_conversion_same_as_mirror( - nb_file, f"{extension}:percent", "ipynb_to_percent" + ipynb_scheme_file, f"{extension}:percent", "ipynb_to_percent" ) diff --git a/tests/functional/round_trip/test_read_all_py.py b/tests/functional/round_trip/test_read_all_py.py index 8a150d339..c7e564f78 100644 --- a/tests/functional/round_trip/test_read_all_py.py +++ b/tests/functional/round_trip/test_read_all_py.py @@ -1,20 +1,7 @@ -import pytest - import jupytext from jupytext.compare import compare -from ...utils import list_notebooks - -@pytest.mark.parametrize( - "py_file", - [ - py_file - for py_file in list_notebooks("./src/jupytext") - if py_file.endswith(".py") - if "folding_markers" not in py_file - ], -) def test_identity_source_write_read(py_file): with open(py_file) as fp: py = fp.read() diff --git a/tests/functional/round_trip/test_rmd_to_ipynb.py b/tests/functional/round_trip/test_rmd_to_ipynb.py index 0b446d741..2167f8e99 100644 --- a/tests/functional/round_trip/test_rmd_to_ipynb.py +++ b/tests/functional/round_trip/test_rmd_to_ipynb.py @@ -1,16 +1,11 @@ -import pytest - import jupytext from jupytext.compare import compare -from ...utils import list_notebooks - -@pytest.mark.parametrize("nb_file", list_notebooks("Rmd")) -def test_identity_write_read(nb_file, no_jupytext_version_number): +def test_identity_write_read(rmd_file, no_jupytext_version_number): """Test that writing the notebook with ipynb, and read again, yields identity""" - with open(nb_file) as fp: + with open(rmd_file) as fp: rmd = fp.read() nb = jupytext.reads(rmd, "Rmd") diff --git a/tests/functional/simple_notebooks/__init__.py b/tests/functional/simple_notebooks/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/functional/simple_notebooks/test_ipynb_to_R.py b/tests/functional/simple_notebooks/test_ipynb_to_R.py index dfa840f3c..b14451606 100644 --- a/tests/functional/simple_notebooks/test_ipynb_to_R.py +++ b/tests/functional/simple_notebooks/test_ipynb_to_R.py @@ -1,24 +1,18 @@ -import itertools - import nbformat import pytest import jupytext from jupytext.compare import compare_notebooks -from ...utils import list_notebooks - -@pytest.mark.parametrize( - "nb_file,ext", itertools.product(list_notebooks("ipynb_R"), [".r", ".R"]) -) -def test_identity_source_write_read(nb_file, ext): +@pytest.mark.parametrize("ext", [".r", ".R"]) +def test_identity_source_write_read(ipynb_R_file, ext): """ Test that writing the notebook with R, and read again, is the same as removing outputs """ - with open(nb_file) as fp: + with open(ipynb_R_file) as fp: nb1 = nbformat.read(fp, as_version=4) R = jupytext.writes(nb1, ext) diff --git a/tests/functional/simple_notebooks/test_ipynb_to_py.py b/tests/functional/simple_notebooks/test_ipynb_to_py.py index cf8721c7c..b46800189 100644 --- a/tests/functional/simple_notebooks/test_ipynb_to_py.py +++ b/tests/functional/simple_notebooks/test_ipynb_to_py.py @@ -1,18 +1,14 @@ import nbformat -import pytest import jupytext from jupytext.compare import compare_notebooks -from ...utils import list_notebooks - -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_identity_source_write_read(nb_file): +def test_identity_source_write_read(ipynb_py_file): """Test that writing the notebook with jupytext, and read again, is the same as removing outputs""" - with open(nb_file) as fp: + with open(ipynb_py_file) as fp: nb1 = nbformat.read(fp, as_version=4) py = jupytext.writes(nb1, "py") diff --git a/tests/functional/simple_notebooks/test_ipynb_to_rmd.py b/tests/functional/simple_notebooks/test_ipynb_to_rmd.py index f8bd0e690..e8e7bf5bf 100644 --- a/tests/functional/simple_notebooks/test_ipynb_to_rmd.py +++ b/tests/functional/simple_notebooks/test_ipynb_to_rmd.py @@ -1,18 +1,14 @@ import nbformat -import pytest import jupytext from jupytext.compare import compare_notebooks -from ...utils import list_notebooks - -@pytest.mark.parametrize("nb_file", list_notebooks(skip="66")) -def test_identity_source_write_read(nb_file): +def test_identity_source_write_read(ipynb_py_R_jl_file): """Test that writing the notebook with rmd, and read again, is the same as removing outputs""" - with open(nb_file) as fp: + with open(ipynb_py_R_jl_file) as fp: nb1 = nbformat.read(fp, as_version=4) rmd = jupytext.writes(nb1, "Rmd") diff --git a/tests/functional/simple_notebooks/test_knitr_spin.py b/tests/functional/simple_notebooks/test_knitr_spin.py index 736ed9828..579a67055 100644 --- a/tests/functional/simple_notebooks/test_knitr_spin.py +++ b/tests/functional/simple_notebooks/test_knitr_spin.py @@ -1,17 +1,12 @@ -import pytest - import jupytext -from ...utils import list_notebooks - -@pytest.mark.parametrize("r_file", list_notebooks("R_spin")) -def test_jupytext_same_as_knitr_spin(r_file, tmpdir): - nb = jupytext.read(r_file) +def test_jupytext_same_as_knitr_spin(r_spin_file, tmpdir): + nb = jupytext.read(r_spin_file) rmd_jupytext = jupytext.writes(nb, "Rmd") # Rmd file generated with spin(hair='R/spin.R', knit=FALSE) - rmd_file = r_file.replace("R_spin", "Rmd").replace(".R", ".Rmd") + rmd_file = r_spin_file.replace("R_spin", "Rmd").replace(".R", ".Rmd") with open(rmd_file) as fp: rmd_spin = fp.read() diff --git a/tests/functional/simple_notebooks/test_read_simple_percent.py b/tests/functional/simple_notebooks/test_read_simple_percent.py index 21522a076..eea25a5cd 100644 --- a/tests/functional/simple_notebooks/test_read_simple_percent.py +++ b/tests/functional/simple_notebooks/test_read_simple_percent.py @@ -9,9 +9,7 @@ ) import jupytext -from jupytext.compare import compare, compare_notebooks - -from ...utils import notebook_model +from jupytext.compare import compare, compare_notebooks, notebook_model def test_read_simple_file( diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/integration/cli/__init__.py b/tests/integration/cli/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/integration/contents_manager/__init__.py b/tests/integration/contents_manager/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/integration/contents_manager/test_cm_config.py b/tests/integration/contents_manager/test_cm_config.py index dad3a7502..28215f247 100644 --- a/tests/integration/contents_manager/test_cm_config.py +++ b/tests/integration/contents_manager/test_cm_config.py @@ -10,8 +10,7 @@ import jupytext from jupytext import TextFileContentsManager -from jupytext.compare import compare_cells -from tests.utils import notebook_model +from jupytext.compare import compare_cells, notebook_model SAMPLE_NOTEBOOK = new_notebook( cells=[new_markdown_cell("A Markdown cell"), new_code_cell("# A code cell\n1 + 1")] diff --git a/tests/integration/contents_manager/test_contentsmanager.py b/tests/integration/contents_manager/test_contentsmanager.py index 6bd492609..43659accc 100644 --- a/tests/integration/contents_manager/test_contentsmanager.py +++ b/tests/integration/contents_manager/test_contentsmanager.py @@ -1,4 +1,3 @@ -import itertools import os import re import shutil @@ -10,14 +9,12 @@ import jupytext from jupytext.cli import jupytext as jupytext_cli -from jupytext.compare import compare, compare_notebooks +from jupytext.compare import compare, compare_notebooks, notebook_model from jupytext.formats import auto_ext_from_metadata, read_format_from_metadata from jupytext.header import header_to_metadata_and_cell from jupytext.jupytext import read, write, writes from jupytext.kernels import kernelspec_from_language -from ...utils import list_notebooks, notebook_model - def test_create_contentsmanager(): jupytext.TextFileContentsManager() @@ -107,8 +104,7 @@ def test_pair_unpair_notebook(tmpdir): assert len(nb2.cells[0]["outputs"]) == 0 -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb", skip="66")) -def test_load_save_rename(nb_file, tmpdir): +def test_load_save_rename(ipynb_py_R_jl_file, tmpdir): tmp_ipynb = "notebook.ipynb" tmp_rmd = "notebook.Rmd" @@ -117,7 +113,7 @@ def test_load_save_rename(nb_file, tmpdir): cm.root_dir = str(tmpdir) # open ipynb, save Rmd, reopen - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) cm.save(model=notebook_model(nb), path=tmp_rmd) nb_rmd = cm.get(tmp_rmd) compare_notebooks(nb_rmd["content"], nb, "Rmd") @@ -150,8 +146,7 @@ def test_load_save_rename(nb_file, tmpdir): assert not os.path.isfile(str(tmpdir.join("new.Rmd"))) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb", skip="magic")) -def test_save_load_paired_md_notebook(nb_file, tmpdir): +def test_save_load_paired_md_notebook(ipynb_py_R_jl_file, tmpdir): tmp_ipynb = "notebook.ipynb" tmp_md = "notebook.md" @@ -159,7 +154,7 @@ def test_save_load_paired_md_notebook(nb_file, tmpdir): cm.root_dir = str(tmpdir) # open ipynb, save with cm, reopen - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) nb.metadata["jupytext"] = {"formats": "ipynb,md"} cm.save(model=notebook_model(nb), path=tmp_ipynb) @@ -170,11 +165,11 @@ def test_save_load_paired_md_notebook(nb_file, tmpdir): @pytest.mark.requires_pandoc -@pytest.mark.parametrize( - "nb_file", - list_notebooks("ipynb", skip="(functional|Notebook with|flavors|invalid|305)"), -) -def test_save_load_paired_md_pandoc_notebook(nb_file, tmpdir): +def test_save_load_paired_md_pandoc_notebook(ipynb_py_R_jl_file, tmpdir): + if re.match( + r".*(functional|Notebook with|flavors|invalid|305).*", ipynb_py_R_jl_file + ): + pytest.skip() tmp_ipynb = "notebook.ipynb" tmp_md = "notebook.md" @@ -182,7 +177,7 @@ def test_save_load_paired_md_pandoc_notebook(nb_file, tmpdir): cm.root_dir = str(tmpdir) # open ipynb, save with cm, reopen - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) nb.metadata["jupytext"] = {"formats": "ipynb,md:pandoc"} cm.save(model=notebook_model(nb), path=tmp_ipynb) @@ -193,15 +188,13 @@ def test_save_load_paired_md_pandoc_notebook(nb_file, tmpdir): @pytest.mark.requires_quarto -@pytest.mark.parametrize( - "nb_file", - list_notebooks( - "ipynb", - skip="(functional|Notebook with|plotly_graphs|flavors|complex_metadata|" - "update83|raw_cell|_66|nteract|LaTeX|invalid|305|text_outputs|ir_notebook|jupyter|with_R_magic)", - ), -) -def test_save_load_paired_qmd_notebook(nb_file, tmpdir): +def test_save_load_paired_qmd_notebook(ipynb_py_R_jl_file, tmpdir): + if re.match( + r".*(functional|Notebook with|plotly_graphs|flavors|complex_metadata|" + "update83|raw_cell|_66|nteract|LaTeX|invalid|305|text_outputs|ir_notebook|jupyter|with_R_magic).*", + ipynb_py_R_jl_file, + ): + pytest.skip() tmp_ipynb = "notebook.ipynb" tmp_qmd = "notebook.qmd" @@ -209,7 +202,7 @@ def test_save_load_paired_qmd_notebook(nb_file, tmpdir): cm.root_dir = str(tmpdir) # open ipynb, save with cm, reopen - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) nb.metadata["jupytext"] = {"formats": "ipynb,qmd"} cm.save(model=notebook_model(nb), path=tmp_ipynb) @@ -219,8 +212,7 @@ def test_save_load_paired_qmd_notebook(nb_file, tmpdir): assert nb_md["content"].metadata["jupytext"]["formats"] == "ipynb,qmd" -@pytest.mark.parametrize("py_file", list_notebooks("percent")) -def test_pair_plain_script(py_file, tmpdir, caplog): +def test_pair_plain_script(percent_file, tmpdir, caplog): tmp_py = "notebook.py" tmp_ipynb = "notebook.ipynb" @@ -228,7 +220,7 @@ def test_pair_plain_script(py_file, tmpdir, caplog): cm.root_dir = str(tmpdir) # open py file, pair, save with cm - nb = jupytext.read(py_file) + nb = jupytext.read(percent_file) nb.metadata["jupytext"]["formats"] = "ipynb,py:hydrogen" cm.save(model=notebook_model(nb), path=tmp_py) @@ -238,7 +230,7 @@ def test_pair_plain_script(py_file, tmpdir, caplog): assert os.path.isfile(str(tmpdir.join(tmp_ipynb))) # Make sure we've not changed the script - with open(py_file) as fp: + with open(percent_file) as fp: script = fp.read() with open(str(tmpdir.join(tmp_py))) as fp: @@ -261,8 +253,7 @@ def test_pair_plain_script(py_file, tmpdir, caplog): assert "formats" not in nb2.metadata["jupytext"] -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_load_save_rename_nbpy(nb_file, tmpdir): +def test_load_save_rename_nbpy(ipynb_py_file, tmpdir): tmp_ipynb = "notebook.ipynb" tmp_nbpy = "notebook.nb.py" @@ -271,7 +262,7 @@ def test_load_save_rename_nbpy(nb_file, tmpdir): cm.root_dir = str(tmpdir) # open ipynb, save nb.py, reopen - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_file) cm.save(model=notebook_model(nb), path=tmp_nbpy) nbpy = cm.get(tmp_nbpy) compare_notebooks(nbpy["content"], nb) @@ -292,15 +283,17 @@ def test_load_save_rename_nbpy(nb_file, tmpdir): cm.rename_file(tmp_nbpy, "suffix_missing.py") -@pytest.mark.parametrize("script", list_notebooks("python", skip="light")) -def test_load_save_py_freeze_metadata(script, tmpdir): +def test_load_save_py_freeze_metadata(python_file, tmpdir): + if "light" in python_file: + pytest.skip() + tmp_nbpy = "notebook.py" cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) # read original file - with open(script) as fp: + with open(python_file) as fp: text_py = fp.read() # write to tmp_nbpy @@ -343,8 +336,7 @@ def test_load_text_notebook(tmpdir): assert nb_model[key] == py_model[key], key -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_load_save_rename_notebook_with_dot(nb_file, tmpdir): +def test_load_save_rename_notebook_with_dot(ipynb_py_file, tmpdir): tmp_ipynb = "1.notebook.ipynb" tmp_nbpy = "1.notebook.py" @@ -353,7 +345,7 @@ def test_load_save_rename_notebook_with_dot(nb_file, tmpdir): cm.root_dir = str(tmpdir) # open ipynb, save nb.py, reopen - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_file) cm.save(model=notebook_model(nb), path=tmp_nbpy) nbpy = cm.get(tmp_nbpy) compare_notebooks(nbpy["content"], nb) @@ -370,8 +362,7 @@ def test_load_save_rename_notebook_with_dot(nb_file, tmpdir): assert os.path.isfile(str(tmpdir.join("2.new_notebook.py"))) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_load_save_rename_nbpy_default_config(nb_file, tmpdir): +def test_load_save_rename_nbpy_default_config(ipynb_py_file, tmpdir): tmp_ipynb = "notebook.ipynb" tmp_nbpy = "notebook.nb.py" @@ -380,7 +371,7 @@ def test_load_save_rename_nbpy_default_config(nb_file, tmpdir): cm.root_dir = str(tmpdir) # open ipynb, save nb.py, reopen - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_file) cm.save(model=notebook_model(nb), path=tmp_nbpy) nbpy = cm.get(tmp_nbpy) @@ -410,8 +401,7 @@ def test_load_save_rename_nbpy_default_config(nb_file, tmpdir): assert not os.path.isfile(str(tmpdir.join("new.nb.py"))) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_load_save_rename_non_ascii_path(nb_file, tmpdir): +def test_load_save_rename_non_ascii_path(ipynb_py_file, tmpdir): tmp_ipynb = "notebôk.ipynb" tmp_nbpy = "notebôk.nb.py" @@ -421,7 +411,7 @@ def test_load_save_rename_non_ascii_path(nb_file, tmpdir): cm.root_dir = tmpdir # open ipynb, save nb.py, reopen - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_file) cm.save(model=notebook_model(nb), path=tmp_nbpy) nbpy = cm.get(tmp_nbpy) @@ -451,8 +441,7 @@ def test_load_save_rename_non_ascii_path(nb_file, tmpdir): assert not os.path.isfile(os.path.join(tmpdir, "nêw.nb.py")) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1]) -def test_outdated_text_notebook(nb_file, tmpdir): +def test_outdated_text_notebook(python_notebook, tmpdir): # 1. write py ipynb cm = jupytext.TextFileContentsManager() cm.formats = "py,ipynb" @@ -460,7 +449,7 @@ def test_outdated_text_notebook(nb_file, tmpdir): cm.root_dir = str(tmpdir) # open ipynb, save py, reopen - nb = jupytext.read(nb_file) + nb = python_notebook cm.save(model=notebook_model(nb), path="notebook.py") model_py = cm.get("notebook.py", load_alternative_format=False) model_ipynb = cm.get("notebook.ipynb", load_alternative_format=False) @@ -565,8 +554,7 @@ def test_outdated_text_notebook_diff_is_shown(tmpdir, python_notebook): ) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1]) -def test_reload_notebook_after_jupytext_cli(nb_file, tmpdir): +def test_reload_notebook_after_jupytext_cli(python_notebook, tmpdir): tmp_ipynb = str(tmpdir.join("notebook.ipynb")) tmp_nbpy = str(tmpdir.join("notebook.py")) @@ -575,7 +563,7 @@ def test_reload_notebook_after_jupytext_cli(nb_file, tmpdir): cm.root_dir = str(tmpdir) # write the paired notebook - nb = jupytext.read(nb_file) + nb = python_notebook nb.metadata.setdefault("jupytext", {})["formats"] = "py,ipynb" cm.save(model=notebook_model(nb), path="notebook.py") @@ -593,10 +581,9 @@ def test_reload_notebook_after_jupytext_cli(nb_file, tmpdir): compare_notebooks(nb, nb2) -@pytest.mark.parametrize("nb_file", list_notebooks("percent")) -def test_load_save_percent_format(nb_file, tmpdir): +def test_load_save_percent_format(percent_file, tmpdir): tmp_py = "notebook.py" - with open(nb_file) as stream: + with open(percent_file) as stream: text_py = stream.read() with open(str(tmpdir.join(tmp_py)), "w") as stream: stream.write(text_py) @@ -623,8 +610,7 @@ def test_load_save_percent_format(nb_file, tmpdir): compare(text_py2, text_py) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_julia")) -def test_save_to_percent_format(nb_file, tmpdir): +def test_save_to_percent_format(ipynb_julia_file, tmpdir): tmp_ipynb = "notebook.ipynb" tmp_jl = "notebook.jl" @@ -632,7 +618,7 @@ def test_save_to_percent_format(nb_file, tmpdir): cm.root_dir = str(tmpdir) cm.preferred_jupytext_formats_save = "jl:percent" - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_julia_file) nb["metadata"]["jupytext"] = {"formats": "ipynb,jl"} # save to ipynb and jl @@ -647,9 +633,8 @@ def test_save_to_percent_format(nb_file, tmpdir): assert metadata["jupytext"]["formats"] == "ipynb,jl:percent" -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_save_using_preferred_and_default_format_170(nb_file, tmpdir): - nb = read(nb_file) +def test_save_using_preferred_and_default_format_170(ipynb_py_file, tmpdir): + nb = read(ipynb_py_file) # Way 0: preferred_jupytext_formats_save, no prefix + formats tmp_py = str(tmpdir.join("python/notebook.py")) @@ -696,13 +681,12 @@ def test_save_using_preferred_and_default_format_170(nb_file, tmpdir): assert nb_py.metadata["jupytext"]["text_representation"]["format_name"] == "percent" -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_open_using_preferred_and_default_format_174(nb_file, tmpdir): +def test_open_using_preferred_and_default_format_174(ipynb_py_file, tmpdir): tmp_ipynb = str(tmpdir.join("notebook.ipynb")) tmp_py = str(tmpdir.join("python/notebook.py")) tmp_py2 = str(tmpdir.join("other/notebook.py")) os.makedirs(str(tmpdir.join("other"))) - shutil.copyfile(nb_file, tmp_ipynb) + shutil.copyfile(ipynb_py_file, tmp_ipynb) cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) @@ -732,11 +716,12 @@ def test_open_using_preferred_and_default_format_174(nb_file, tmpdir): assert not os.path.isfile(str(tmpdir.join("other/notebook.ipynb"))) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py", skip="many hash")) -def test_kernelspec_are_preserved(nb_file, tmpdir): +def test_kernelspec_are_preserved(ipynb_py_file, tmpdir): + if "many hash" in ipynb_py_file: + pytest.skip() tmp_ipynb = str(tmpdir.join("notebook.ipynb")) tmp_py = str(tmpdir.join("notebook.py")) - shutil.copyfile(nb_file, tmp_ipynb) + shutil.copyfile(ipynb_py_file, tmp_ipynb) cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) @@ -760,8 +745,7 @@ def test_kernelspec_are_preserved(nb_file, tmpdir): compare_notebooks(model2["content"], model["content"]) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_save_to_light_percent_sphinx_format(nb_file, tmpdir): +def test_save_to_light_percent_sphinx_format(ipynb_py_file, tmpdir): tmp_ipynb = "notebook.ipynb" tmp_lgt_py = "notebook.lgt.py" tmp_pct_py = "notebook.pct.py" @@ -770,7 +754,7 @@ def test_save_to_light_percent_sphinx_format(nb_file, tmpdir): cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_file) nb["metadata"]["jupytext"] = { "formats": "ipynb,.pct.py:percent,.lgt.py:light,.spx.py:sphinx" } @@ -801,8 +785,7 @@ def test_save_to_light_percent_sphinx_format(nb_file, tmpdir): compare_notebooks(model["content"], nb) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_pair_notebook_with_dot(nb_file, tmpdir): +def test_pair_notebook_with_dot(ipynb_py_file, tmpdir): # Reproduce issue #138 tmp_py = "file.5.1.py" tmp_ipynb = "file.5.1.ipynb" @@ -810,7 +793,7 @@ def test_pair_notebook_with_dot(nb_file, tmpdir): cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_file) nb["metadata"]["jupytext"] = {"formats": "ipynb,py:percent"} # save to ipynb and three python flavors @@ -831,8 +814,7 @@ def test_pair_notebook_with_dot(nb_file, tmpdir): compare_notebooks(model["content"], nb) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1]) -def test_preferred_format_allows_to_read_others_format(nb_file, tmpdir): +def test_preferred_format_allows_to_read_others_format(python_notebook, tmpdir): # 1. write py ipynb tmp_ipynb = "notebook.ipynb" tmp_nbpy = "notebook.py" @@ -842,7 +824,7 @@ def test_preferred_format_allows_to_read_others_format(nb_file, tmpdir): cm.root_dir = str(tmpdir) # load notebook and save it using the cm - nb = jupytext.read(nb_file) + nb = python_notebook nb["metadata"]["jupytext"] = {"formats": "ipynb,py"} cm.save(model=notebook_model(nb), path=tmp_ipynb) @@ -900,10 +882,9 @@ def test_preferred_formats_read_auto(tmpdir): ) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb")) -def test_save_in_auto_extension_global(nb_file, tmpdir): +def test_save_in_auto_extension_global(ipynb_py_R_jl_file, tmpdir): # load notebook - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) auto_ext = auto_ext_from_metadata(nb.metadata) tmp_ipynb = "notebook.ipynb" @@ -984,10 +965,9 @@ def test_global_auto_pairing_works_with_empty_notebook(tmpdir): assert nb2.cells[0].source == "2+2" -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb")) -def test_save_in_auto_extension_global_with_format(nb_file, tmpdir): +def test_save_in_auto_extension_global_with_format(ipynb_py_R_jl_file, tmpdir): # load notebook - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) auto_ext = auto_ext_from_metadata(nb.metadata) tmp_ipynb = "notebook.ipynb" @@ -1014,10 +994,9 @@ def test_save_in_auto_extension_global_with_format(nb_file, tmpdir): compare_notebooks(model["content"], nb) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb")) -def test_save_in_auto_extension_local(nb_file, tmpdir): +def test_save_in_auto_extension_local(ipynb_py_R_jl_file, tmpdir): # load notebook - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) nb.metadata.setdefault("jupytext", {})["formats"] = "ipynb,auto:percent" auto_ext = auto_ext_from_metadata(nb.metadata) @@ -1041,10 +1020,9 @@ def test_save_in_auto_extension_local(nb_file, tmpdir): compare_notebooks(model["content"], nb) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb")) -def test_save_in_pct_and_lgt_auto_extensions(nb_file, tmpdir): +def test_save_in_pct_and_lgt_auto_extensions(ipynb_py_R_jl_file, tmpdir): # load notebook - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_R_jl_file) auto_ext = auto_ext_from_metadata(nb.metadata) tmp_ipynb = "notebook.ipynb" @@ -1069,9 +1047,10 @@ def test_save_in_pct_and_lgt_auto_extensions(nb_file, tmpdir): assert read_format_from_metadata(stream.read(), auto_ext) == "light" -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb", skip="(magic|305)")) -def test_metadata_filter_is_effective(nb_file, tmpdir): - nb = jupytext.read(nb_file) +def test_metadata_filter_is_effective(ipynb_py_R_jl_file, tmpdir): + if re.match(r".*(magic|305).*", ipynb_py_R_jl_file): + pytest.skip() + nb = jupytext.read(ipynb_py_R_jl_file) tmp_ipynb = "notebook.ipynb" tmp_script = "notebook.py" @@ -1155,12 +1134,10 @@ def test_no_metadata_added_to_scripts_139(tmpdir): compare(fp.read(), text) -@pytest.mark.parametrize( - "nb_file,ext", itertools.product(list_notebooks("ipynb_py"), [".py", ".ipynb"]) -) -def test_local_format_can_deactivate_pairing(nb_file, ext, tmpdir): +@pytest.mark.parametrize("ext", [".py", ".ipynb"]) +def test_local_format_can_deactivate_pairing(ipynb_py_file, ext, tmpdir): """This is a test for #157: local format can be used to deactivate the global pairing""" - nb = jupytext.read(nb_file) + nb = jupytext.read(ipynb_py_file) nb.metadata["jupytext_formats"] = ext[1:] # py or ipynb # create contents manager with default pairing @@ -1186,10 +1163,9 @@ def test_local_format_can_deactivate_pairing(nb_file, ext, tmpdir): compare_notebooks(nb3, nb) -@pytest.mark.parametrize("nb_file", list_notebooks("Rmd")) -def test_global_pairing_allows_to_save_other_file_types(nb_file, tmpdir): +def test_global_pairing_allows_to_save_other_file_types(rmd_file, tmpdir): """This is a another test for #157: local format can be used to deactivate the global pairing""" - nb = jupytext.read(nb_file) + nb = jupytext.read(rmd_file) # create contents manager with default pairing cm = jupytext.TextFileContentsManager() @@ -1209,11 +1185,10 @@ def test_global_pairing_allows_to_save_other_file_types(nb_file, tmpdir): @pytest.mark.requires_user_kernel_python3 -@pytest.mark.parametrize("nb_file", list_notebooks("R")) -def test_python_kernel_preserves_R_files(nb_file, tmpdir): +def test_python_kernel_preserves_R_files(r_file, tmpdir): """Opening a R file with a Jupyter server that has no R kernel should not modify the file""" tmp_r_file = str(tmpdir.join("script.R")) - with open(nb_file) as fp: + with open(r_file) as fp: script = fp.read() with open(tmp_r_file, "w") as fp: fp.write(script) @@ -1363,12 +1338,11 @@ def test_set_then_change_formats(tmpdir): assert not os.path.isfile(tmp_py) -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")[:1]) -def test_set_then_change_auto_formats(tmpdir, nb_file): +def test_set_then_change_auto_formats(tmpdir, python_notebook): tmp_ipynb = str(tmpdir.join("nb.ipynb")) tmp_py = str(tmpdir.join("nb.py")) tmp_rmd = str(tmpdir.join("nb.Rmd")) - nb = new_notebook(metadata=read(nb_file).metadata) + nb = new_notebook(metadata=python_notebook.metadata) cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmpdir) @@ -1402,8 +1376,7 @@ def test_set_then_change_auto_formats(tmpdir, nb_file): cm.get("nb.ipynb") -@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py")) -def test_share_py_recreate_ipynb(tmpdir, nb_file): +def test_share_py_recreate_ipynb(tmpdir, ipynb_py_R_jl_file): tmp_ipynb = str(tmpdir.join("nb.ipynb")) tmp_py = str(tmpdir.join("nb.py")) @@ -1420,7 +1393,7 @@ def test_share_py_recreate_ipynb(tmpdir, nb_file): cm.notebook_metadata_filter = "-all" cm.cell_metadata_filter = "-all" - nb = read(nb_file) + nb = read(ipynb_py_R_jl_file) model_ipynb = cm.save(model=notebook_model(nb), path="nb.ipynb") assert os.path.isfile(tmp_ipynb) @@ -1866,12 +1839,12 @@ def test_timestamp_is_correct_after_reload_978(tmp_path, python_notebook): cm = jupytext.TextFileContentsManager() cm.root_dir = str(tmp_path) - ipynb_file = tmp_path / "nb.ipynb" + ipynb_py_R_jl_file = tmp_path / "nb.ipynb" py_file = tmp_path / "nb.py" # 1. Save the paired notebook cm.save(notebook_model(nb), path="nb.ipynb") - assert ipynb_file.exists() + assert ipynb_py_R_jl_file.exists() assert py_file.exists() # and reload to get the original timestamp diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/unit/test_escape_magics.py b/tests/unit/test_escape_magics.py index d6bd6de56..e4c6585d3 100644 --- a/tests/unit/test_escape_magics.py +++ b/tests/unit/test_escape_magics.py @@ -2,7 +2,7 @@ from nbformat.v4.nbbase import new_code_cell, new_notebook import jupytext -from jupytext.compare import compare, compare_notebooks +from jupytext.compare import compare, compare_notebooks, notebook_model from jupytext.magics import ( _PYTHON_MAGIC_ASSIGN, comment_magic, @@ -11,8 +11,6 @@ unesc, ) -from ..utils import notebook_model - def test_unesc(): assert unesc("# comment", "python") == "comment" diff --git a/tests/unit/test_formats.py b/tests/unit/test_formats.py index 286171351..796b25ef7 100644 --- a/tests/unit/test_formats.py +++ b/tests/unit/test_formats.py @@ -16,18 +16,14 @@ validate_one_format, ) -from ..utils import list_notebooks - -@pytest.mark.parametrize("nb_file", list_notebooks("python")) -def test_guess_format_light(nb_file): - with open(nb_file) as stream: +def test_guess_format_light(python_file): + with open(python_file) as stream: assert guess_format(stream.read(), ext=".py")[0] == "light" -@pytest.mark.parametrize("nb_file", list_notebooks("percent")) -def test_guess_format_percent(nb_file): - with open(nb_file) as stream: +def test_guess_format_percent(percent_file): + with open(percent_file) as stream: assert guess_format(stream.read(), ext=".py")[0] == "percent" @@ -57,9 +53,8 @@ def test_guess_format_simple_hydrogen_with_magic( assert guess_format(nb, ext=".py")[0] == "hydrogen" -@pytest.mark.parametrize("nb_file", list_notebooks("sphinx")) -def test_guess_format_sphinx(nb_file): - with open(nb_file) as stream: +def test_guess_format_sphinx(sphinx_file): + with open(sphinx_file) as stream: assert guess_format(stream.read(), ext=".py")[0] == "sphinx" diff --git a/tests/unit/test_pep8.py b/tests/unit/test_pep8.py index f29a1479d..4150e883d 100644 --- a/tests/unit/test_pep8.py +++ b/tests/unit/test_pep8.py @@ -1,4 +1,3 @@ -import pytest from nbformat.v4.nbbase import new_code_cell, new_notebook from jupytext import read, reads, writes @@ -11,8 +10,6 @@ pep8_lines_between_cells, ) -from ..utils import list_notebooks - def test_next_instruction_is_function_or_class(): text = """@pytest.mark.parametrize('py_file', @@ -185,14 +182,6 @@ def f(x): compare(text2, text) -@pytest.mark.parametrize( - "py_file", - [ - py_file - for py_file in list_notebooks("./src/jupytext") - if py_file.endswith(".py") and "folding_markers" not in py_file - ], -) def test_no_metadata_when_py_is_pep8(py_file): """This test assumes that all Python files in the jupytext folder follow PEP8 rules""" nb = read(py_file) diff --git a/tests/utils.py b/tests/utils.py deleted file mode 100644 index e2b468c29..000000000 --- a/tests/utils.py +++ /dev/null @@ -1,75 +0,0 @@ -import itertools -import json -import re -from pathlib import Path - -from jupytext.cli import system -from jupytext.formats import JUPYTEXT_FORMATS -from jupytext.myst import is_myst_available -from jupytext.pandoc import is_pandoc_available - - -def tool_version(tool): - try: - args = tool.split(" ") - args.append("--version") - return system(*args) - except (OSError, SystemExit): # pragma: no cover - return None - - -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}" - - -def list_notebooks(path="ipynb", skip=""): - """All notebooks in the directory notebooks/path, - or in the package itself""" - if path == "ipynb": - return ( - list_notebooks("ipynb_julia", skip=skip) - + list_notebooks("ipynb_py", skip=skip) - + list_notebooks("ipynb_R", skip=skip) - ) - - nb_path = Path(__file__).parent / "data" / "notebooks" / "inputs" - - if path == "ipynb_all": - return itertools.chain( - *( - list_notebooks(folder.name, skip=skip) - for folder in nb_path.iterdir() - if folder.name.startswith("ipynb_") - ) - ) - - if path == "all": - return itertools.chain( - *(list_notebooks(folder.name, skip=skip) for folder in nb_path.iterdir()) - ) - - if path.startswith("."): - nb_path = Path(__file__).parent / ".." / path - else: - nb_path = nb_path / path - - if skip: - skip_re = re.compile(".*" + skip + ".*") - return [ - str(nb_file) - for nb_file in nb_path.iterdir() - if nb_file.is_file() and not skip_re.match(nb_file.name) - ] - - return [str(nb_file) for nb_file in nb_path.iterdir() if nb_file.is_file()] - - -def notebook_model(nb): - """Return a notebook model, with content a dictionary rather than a notebook object""" - return dict(type="notebook", content=json.loads(json.dumps(nb)))