소스 검색

grass.jupyter: Test init and session (#2247)

Adds test for Jupyter init, session, and finish using pytest.

Uses temporary script to test automatic (implicit) cleanup with weakref.finalize (always needed).

Uses temporary script for the standard test because the session restores the global state cleanly, namely the global variables in the Python library (temporary solution).
Vaclav Petras 3 년 전
부모
커밋
f9ee75ccef
1개의 변경된 파일63개의 추가작업 그리고 0개의 파일을 삭제
  1. 63 0
      python/grass/jupyter/tests/grass_jupyter_session_test.py

+ 63 - 0
python/grass/jupyter/tests/grass_jupyter_session_test.py

@@ -0,0 +1,63 @@
+"""Test session functions in grass.jupyter"""
+
+import subprocess
+import os
+import sys
+
+
+# All init tests change the global environment, but we use a separate process
+# only when it is necessary.
+# Ideally, the functions would support env parameter and the tests
+# would mostly use that.
+def run_in_subprocess(file):
+    """Run function in a separate process
+    The function must take a Queue and put its result there.
+    The result is then returned from this function.
+    """
+    process = subprocess.run(
+        [sys.executable, os.fspath(file)], stdout=subprocess.PIPE, text=True, check=True
+    )
+    return process.stdout
+
+
+def test_init_finish(tmp_path):
+    """Check that init function works with an explicit session finish"""
+    location = "test"
+    script = f"""
+import os
+import grass.script as gs
+import grass.jupyter as gj
+gs.core._create_location_xy("{tmp_path}", "{location}")
+session = gj.init("{tmp_path / location}")
+gs.read_command("g.region", flags="p")
+print(os.environ["GISRC"])
+session.finish()
+"""
+    file = tmp_path / "script.py"
+    file.write_text(script)
+    session_file = run_in_subprocess(file)
+    assert session_file, "Expected something from the subprocess"
+    session_file = session_file.strip()
+    assert "\n" not in session_file, "Expected a file name from the subprocess"
+    assert not os.path.exists(session_file), f"Session file {session_file} not deleted"
+
+
+def test_init_with_auto_finish(tmp_path):
+    """Check that init function works with an implicit session finish"""
+    location = "test"
+    script = f"""
+import os
+import grass.script as gs
+import grass.jupyter as gj
+gs.core._create_location_xy("{tmp_path}", "{location}")
+session = gj.init("{tmp_path / location}")
+print(os.environ["GISRC"])
+"""
+
+    file = tmp_path / "script.py"
+    file.write_text(script)
+    session_file = run_in_subprocess(file)
+    assert session_file, "Expected something from the subprocess"
+    session_file = session_file.strip()
+    assert "\n" not in session_file, "Expected a file name from the subprocess"
+    assert not os.path.exists(session_file), f"Session file {session_file} not deleted"