|
| 1 | +from pathlib import Path |
| 2 | +from subprocess import CalledProcessError, check_output |
| 3 | +from unittest.mock import call, mock_open, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from nbclient.cli import NbClientApp |
| 8 | + |
| 9 | +current_dir = Path(__file__).parent.absolute() |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture() |
| 13 | +def jupyterapp(): |
| 14 | + with patch("nbclient.cli.JupyterApp.initialize") as mocked: |
| 15 | + yield mocked |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture() |
| 19 | +def client(): |
| 20 | + with patch("nbclient.cli.NotebookClient", autospec=True) as mocked: |
| 21 | + yield mocked |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture() |
| 25 | +def writer(): |
| 26 | + with patch("nbformat.write", autospec=True) as mocked: |
| 27 | + yield mocked |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture() |
| 31 | +def reader(): |
| 32 | + with patch("nbformat.read", autospec=True, return_value="nb") as mocked: |
| 33 | + yield mocked |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture() |
| 37 | +def path_open(): |
| 38 | + opener = mock_open() |
| 39 | + |
| 40 | + def mocked_open(self, *args, **kwargs): |
| 41 | + return opener(self, *args, **kwargs) |
| 42 | + |
| 43 | + with patch("nbclient.cli.Path.open", mocked_open): |
| 44 | + yield opener |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "input_names", [("Other Comms",), ("Other Comms.ipynb",), ("Other Comms", "HelloWorld.ipynb")] |
| 49 | +) |
| 50 | +@pytest.mark.parametrize("relative", [False, True]) |
| 51 | +@pytest.mark.parametrize("inplace", [False, True]) |
| 52 | +def test_mult(input_names, relative, inplace, jupyterapp, client, reader, writer, path_open): |
| 53 | + paths = [current_dir / "files" / name for name in input_names] |
| 54 | + if relative: |
| 55 | + paths = [p.relative_to(Path.cwd()) for p in paths] |
| 56 | + |
| 57 | + c = NbClientApp(notebooks=[str(p) for p in paths], kernel_name="python3", inplace=inplace) |
| 58 | + c.initialize() |
| 59 | + |
| 60 | + # add suffix if needed |
| 61 | + paths = [p.with_suffix(".ipynb") for p in paths] |
| 62 | + |
| 63 | + assert path_open.mock_calls[::3] == [call(p) for p in paths] |
| 64 | + assert reader.call_count == len(paths) |
| 65 | + # assert reader.mock_calls == [call(p, as_version=4) for p in paths] |
| 66 | + |
| 67 | + expected = [] |
| 68 | + for p in paths: |
| 69 | + expected.extend( |
| 70 | + [ |
| 71 | + call( |
| 72 | + "nb", |
| 73 | + timeout=c.timeout, |
| 74 | + startup_timeout=c.startup_timeout, |
| 75 | + skip_cells_with_tag=c.skip_cells_with_tag, |
| 76 | + allow_errors=c.allow_errors, |
| 77 | + kernel_name=c.kernel_name, |
| 78 | + resources={"metadata": {"path": p.parent.absolute()}}, |
| 79 | + ), |
| 80 | + call().execute(), |
| 81 | + ] |
| 82 | + ) |
| 83 | + |
| 84 | + assert client.mock_calls == expected |
| 85 | + |
| 86 | + if inplace: |
| 87 | + assert writer.mock_calls == [call("nb", p) for p in paths] |
| 88 | + else: |
| 89 | + writer.assert_not_called() |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize( |
| 93 | + "input_names", [("Other Comms",), ("Other Comms.ipynb",), ("Other Comms", "HelloWorld.ipynb")] |
| 94 | +) |
| 95 | +@pytest.mark.parametrize("relative", [False, True]) |
| 96 | +@pytest.mark.parametrize("output_base", ["thing", "thing.ipynb", "{notebook_name}-new.ipynb"]) |
| 97 | +def test_output(input_names, relative, output_base, jupyterapp, client, reader, writer, path_open): |
| 98 | + paths = [current_dir / "files" / name for name in input_names] |
| 99 | + if relative: |
| 100 | + paths = [p.relative_to(Path.cwd()) for p in paths] |
| 101 | + |
| 102 | + c = NbClientApp( |
| 103 | + notebooks=[str(p) for p in paths], kernel_name="python3", output_base=output_base |
| 104 | + ) |
| 105 | + |
| 106 | + if len(paths) != 1 and "{notebook_name}" not in output_base: |
| 107 | + with pytest.raises(ValueError) as e: |
| 108 | + c.initialize() |
| 109 | + assert "If passing multiple" in str(e.value) |
| 110 | + return |
| 111 | + |
| 112 | + c.initialize() |
| 113 | + |
| 114 | + # add suffix if needed |
| 115 | + paths = [p.with_suffix(".ipynb") for p in paths] |
| 116 | + |
| 117 | + assert path_open.mock_calls[::3] == [call(p) for p in paths] |
| 118 | + assert reader.call_count == len(paths) |
| 119 | + |
| 120 | + expected = [] |
| 121 | + for p in paths: |
| 122 | + expected.extend( |
| 123 | + [ |
| 124 | + call( |
| 125 | + "nb", |
| 126 | + timeout=c.timeout, |
| 127 | + startup_timeout=c.startup_timeout, |
| 128 | + skip_cells_with_tag=c.skip_cells_with_tag, |
| 129 | + allow_errors=c.allow_errors, |
| 130 | + kernel_name=c.kernel_name, |
| 131 | + resources={"metadata": {"path": p.parent.absolute()}}, |
| 132 | + ), |
| 133 | + call().execute(), |
| 134 | + ] |
| 135 | + ) |
| 136 | + |
| 137 | + assert client.mock_calls == expected |
| 138 | + |
| 139 | + assert writer.mock_calls == [ |
| 140 | + call( |
| 141 | + "nb", |
| 142 | + (p.parent / output_base.format(notebook_name=p.with_suffix("").name)).with_suffix( |
| 143 | + ".ipynb" |
| 144 | + ), |
| 145 | + ) |
| 146 | + for p in paths |
| 147 | + ] |
| 148 | + |
| 149 | + |
| 150 | +def test_bad_output_dir(jupyterapp, client, reader, writer, path_open): |
| 151 | + input_names = ["Other Comms"] |
| 152 | + output_base = "thing/thing" |
| 153 | + |
| 154 | + paths = [current_dir / "files" / name for name in input_names] |
| 155 | + |
| 156 | + c = NbClientApp( |
| 157 | + notebooks=[str(p) for p in paths], kernel_name="python3", output_base=output_base |
| 158 | + ) |
| 159 | + |
| 160 | + with pytest.raises(ValueError) as e: |
| 161 | + c.initialize() |
| 162 | + |
| 163 | + assert "Cannot write to directory" in str(e.value) |
| 164 | + |
| 165 | + |
| 166 | +# simple runner from command line |
| 167 | +def test_cli_simple(): |
| 168 | + path = current_dir / "files" / "Other Comms" |
| 169 | + |
| 170 | + with pytest.raises(CalledProcessError): |
| 171 | + check_output(["jupyter-execute", "--output", "thing/thing", str(path)]) # noqa: S603, S607 |
| 172 | + |
| 173 | + |
| 174 | +def test_no_notebooks(jupyterapp): |
| 175 | + c = NbClientApp(notebooks=[], kernel_name="python3") |
| 176 | + |
| 177 | + with pytest.raises(SystemExit): |
| 178 | + c.initialize() |
0 commit comments