Skip to content

Commit 1af2bfc

Browse files
authored
Add check_nbconvert function and run tests for appropriate images (#2266)
1 parent 57ce6e3 commit 1af2bfc

File tree

7 files changed

+117
-72
lines changed

7 files changed

+117
-72
lines changed

tests/by_image/all-spark-notebook/test_spark_notebooks.py

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
import logging
4+
from pathlib import Path
5+
6+
import pytest # type: ignore
7+
8+
from tests.shared_checks.nbconvert_check import check_nbconvert
9+
from tests.utils.tracked_container import TrackedContainer
10+
11+
LOGGER = logging.getLogger(__name__)
12+
THIS_DIR = Path(__file__).parent.resolve()
13+
14+
15+
@pytest.mark.flaky(retries=3, delay=1)
16+
@pytest.mark.parametrize(
17+
"test_file",
18+
["local_sparkR"],
19+
)
20+
@pytest.mark.parametrize("output_format", ["pdf", "html", "markdown"])
21+
def test_spark_r_nbconvert(
22+
container: TrackedContainer, test_file: str, output_format: str
23+
) -> None:
24+
host_data_file = THIS_DIR / "data" / f"{test_file}.ipynb"
25+
logs = check_nbconvert(
26+
container, host_data_file, "markdown", execute=True, no_warnings=False
27+
)
28+
29+
warnings = TrackedContainer.get_warnings(logs)
30+
assert len(warnings) == 1
31+
assert "Using incubator modules: jdk.incubator.vector" in warnings[0]

tests/by_image/minimal-notebook/test_nbconvert.py

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,17 @@
55

66
import pytest # type: ignore
77

8+
from tests.shared_checks.nbconvert_check import check_nbconvert
89
from tests.utils.tracked_container import TrackedContainer
910

1011
LOGGER = logging.getLogger(__name__)
1112
THIS_DIR = Path(__file__).parent.resolve()
1213

1314

1415
@pytest.mark.parametrize("test_file", ["notebook_math", "notebook_svg"])
15-
@pytest.mark.parametrize("output_format", ["pdf", "html"])
16+
@pytest.mark.parametrize("output_format", ["pdf", "html", "markdown"])
1617
def test_nbconvert(
1718
container: TrackedContainer, test_file: str, output_format: str
1819
) -> None:
19-
"""Check if nbconvert is able to convert a notebook file"""
20-
host_data_dir = THIS_DIR / "data"
21-
cont_data_dir = "/home/jovyan/data"
22-
output_dir = "/tmp"
23-
LOGGER.info(
24-
f"Test that the example notebook {test_file} can be converted to {output_format} ..."
25-
)
26-
command = [
27-
"jupyter",
28-
"nbconvert",
29-
f"{cont_data_dir}/{test_file}.ipynb",
30-
"--output-dir",
31-
output_dir,
32-
"--to",
33-
output_format,
34-
]
35-
logs = container.run_and_wait(
36-
timeout=30,
37-
volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro"}},
38-
command=command,
39-
)
40-
expected_file = f"{output_dir}/{test_file}.{output_format}"
41-
assert expected_file in logs, f"Expected file {expected_file} not generated"
20+
host_data_file = THIS_DIR / "data" / f"{test_file}.ipynb"
21+
check_nbconvert(container, host_data_file, output_format, execute=False)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
import logging
4+
from pathlib import Path
5+
6+
import pytest # type: ignore
7+
8+
from tests.shared_checks.nbconvert_check import check_nbconvert
9+
from tests.utils.tracked_container import TrackedContainer
10+
11+
LOGGER = logging.getLogger(__name__)
12+
THIS_DIR = Path(__file__).parent.resolve()
13+
14+
15+
@pytest.mark.flaky(retries=3, delay=1)
16+
@pytest.mark.parametrize(
17+
"test_file",
18+
["issue_1168", "local_pyspark"],
19+
)
20+
@pytest.mark.parametrize("output_format", ["pdf", "html", "markdown"])
21+
def test_spark_nbconvert(
22+
container: TrackedContainer, test_file: str, output_format: str
23+
) -> None:
24+
host_data_file = THIS_DIR / "data" / f"{test_file}.ipynb"
25+
logs = check_nbconvert(
26+
container, host_data_file, "markdown", execute=True, no_warnings=False
27+
)
28+
29+
warnings = TrackedContainer.get_warnings(logs)
30+
assert len(warnings) == 1
31+
assert "Using incubator modules: jdk.incubator.vector" in warnings[0]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) Jupyter Development Team.
2+
# Distributed under the terms of the Modified BSD License.
3+
import logging
4+
from pathlib import Path
5+
6+
from tests.utils.tracked_container import TrackedContainer
7+
8+
LOGGER = logging.getLogger(__name__)
9+
10+
11+
def check_nbconvert(
12+
container: TrackedContainer,
13+
host_data_file: Path,
14+
output_format: str,
15+
*,
16+
execute: bool,
17+
no_warnings: bool = True,
18+
) -> str:
19+
"""Check if nbconvert is able to convert a notebook file"""
20+
cont_data_file = "/home/jovyan/data/" + host_data_file.name
21+
22+
output_dir = "/tmp"
23+
LOGGER.info(
24+
f"Test that the example notebook {host_data_file.name} can be converted to {output_format} ..."
25+
)
26+
command = [
27+
"jupyter",
28+
"nbconvert",
29+
cont_data_file,
30+
"--output-dir",
31+
output_dir,
32+
"--to",
33+
output_format,
34+
]
35+
if execute:
36+
conversion_timeout_ms = 5000
37+
command += [
38+
"--execute",
39+
f"--ExecutePreprocessor.timeout={conversion_timeout_ms}",
40+
]
41+
logs = container.run_and_wait(
42+
timeout=60,
43+
volumes={str(host_data_file): {"bind": cont_data_file, "mode": "ro"}},
44+
command=command,
45+
no_warnings=no_warnings,
46+
)
47+
output_ext = "md" if output_format == "markdown" else output_format
48+
expected_file = f"{output_dir}/{host_data_file.stem}.{output_ext}"
49+
assert expected_file in logs, f"Expected file {expected_file} not generated"
50+
51+
return logs

0 commit comments

Comments
 (0)