stuff, fmt
This commit is contained in:
+19
-53
@@ -1,61 +1,21 @@
|
||||
import contextlib
|
||||
import importlib.util
|
||||
import io
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from IPython.core.interactiveshell import InteractiveShell
|
||||
from IPython.core.history import HistoryOutput
|
||||
from IPython.core.interactiveshell import InteractiveShell
|
||||
from IPython.utils import capture
|
||||
|
||||
import ipython_demo.events as events_module
|
||||
import ipython_demo.models as models_module
|
||||
import ipython_demo.shell as shell_module
|
||||
|
||||
PACKAGE_ROOT = Path(__file__).parents[1] / "src" / "ipython_demo"
|
||||
package = types.ModuleType("ipython_demo")
|
||||
package.__path__ = [str(PACKAGE_ROOT)]
|
||||
sys.modules["ipython_demo"] = package
|
||||
|
||||
|
||||
def load_package_module(name: str):
|
||||
path = PACKAGE_ROOT / f"{name}.py"
|
||||
spec = importlib.util.spec_from_file_location(f"ipython_demo.{name}", path)
|
||||
assert spec is not None and spec.loader is not None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = module
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
models_module = load_package_module("models")
|
||||
events_module = load_package_module("events")
|
||||
shell_module = load_package_module("shell")
|
||||
|
||||
|
||||
class ShellTests(unittest.TestCase):
|
||||
def test_init_shell_has_no_seed_escape_hatch(self):
|
||||
import inspect
|
||||
|
||||
self.assertNotIn(
|
||||
"seed", inspect.signature(shell_module.Shell.init_shell).parameters
|
||||
)
|
||||
|
||||
def test_package_import_has_no_experiment_side_effects(self):
|
||||
env = os.environ.copy()
|
||||
env["PYTHONPATH"] = str(PACKAGE_ROOT.parent)
|
||||
completed = subprocess.run(
|
||||
[sys.executable, "-c", "import ipython_demo; print('imported')"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env=env,
|
||||
check=True,
|
||||
)
|
||||
|
||||
self.assertEqual(completed.stdout, "imported\n")
|
||||
|
||||
def test_event_from_history_output_preserves_streams_and_mime(self):
|
||||
stdout = events_module.event_from_history_output(
|
||||
HistoryOutput("out_stream", {"stream": ["hello", "\n"]}),
|
||||
@@ -105,9 +65,13 @@ class ShellTests(unittest.TestCase):
|
||||
)
|
||||
|
||||
self.assertIsInstance(execution.error, models_module.CallError)
|
||||
assert execution.error
|
||||
self.assertEqual(execution.error.ename, "ValueError")
|
||||
self.assertEqual(execution.error.evalue, "boom")
|
||||
self.assertTrue(events, "no events")
|
||||
self.assertEqual(events[-1].kind, "error")
|
||||
self.assertTrue(events[-1].data, "no data in last event")
|
||||
assert events[-1].data
|
||||
self.assertEqual(events[-1].data["ename"], "ValueError")
|
||||
self.assertEqual(events[-1].data["evalue"], "boom")
|
||||
|
||||
@@ -168,16 +132,18 @@ class ShellTests(unittest.TestCase):
|
||||
"Shell.run_cell must use the shell-local output records"
|
||||
)
|
||||
|
||||
with patch.object(
|
||||
capture,
|
||||
"capture_output",
|
||||
fail_capture_output,
|
||||
with (
|
||||
patch.object(
|
||||
capture,
|
||||
"capture_output",
|
||||
fail_capture_output,
|
||||
),
|
||||
contextlib.redirect_stdout(io.StringIO()),
|
||||
):
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
result, events = wrapper.run_cell(
|
||||
"print('hello'); 42",
|
||||
call_id="call-1",
|
||||
)
|
||||
result, events = wrapper.run_cell(
|
||||
"print('hello'); 42",
|
||||
call_id="call-1",
|
||||
)
|
||||
|
||||
self.assertEqual(result.result, 42)
|
||||
self.assertEqual([event.kind for event in events], ["stdout", "execute_result"])
|
||||
|
||||
Reference in New Issue
Block a user