Skip to content

Commit 686739b

Browse files
committed
add test
1 parent a8b2da7 commit 686739b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/scyjava/_stubs/_genstubs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def generate_stubs(
6969
stub generator wrote to it (and therefore inaccurate).
7070
"""
7171
import jpype
72+
73+
# FIXME: either remove the _JImportLoader from sys.meta_path after this is done
74+
# (if it wasn't there to begin with), or replace the import_module calls below
75+
# with a more direct JPackage call
7276
import jpype.imports
7377

7478
startJVM = jpype.startJVM

tests/test_stubgen.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from typing import TYPE_CHECKING
5+
from unittest.mock import patch
6+
7+
import jpype
8+
import jpype.imports
9+
10+
import scyjava
11+
from scyjava._stubs import _cli
12+
13+
if TYPE_CHECKING:
14+
from pathlib import Path
15+
16+
import pytest
17+
18+
19+
def test_stubgen(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
20+
monkeypatch.setattr(
21+
sys,
22+
"argv",
23+
[
24+
"scyjava-stubgen",
25+
"org.scijava:parsington:3.1.0",
26+
"--output-dir",
27+
str(tmp_path),
28+
],
29+
)
30+
_cli.main()
31+
32+
# remove the `jpype.imports` magic from the import system if present
33+
mp = [x for x in sys.meta_path if not isinstance(x, jpype.imports._JImportLoader)]
34+
monkeypatch.setattr(sys, "meta_path", mp)
35+
36+
# add tmp_path to the import path
37+
monkeypatch.setattr(sys, "path", [str(tmp_path)])
38+
39+
# first cleanup to make sure we are not importing from the cache
40+
sys.modules.pop("org", None)
41+
sys.modules.pop("org.scijava", None)
42+
sys.modules.pop("org.scijava.parsington", None)
43+
# make sure the stubgen command works and that we can now impmort stuff
44+
45+
with patch.object(scyjava._jvm, "start_jvm") as mock_start_jvm:
46+
from org.scijava.parsington import Function
47+
48+
assert Function is not None
49+
assert repr(Function) == "<scyjava class 'org.scijava.parsington.Function'>"
50+
# ensure that no calls to start_jvm were made
51+
mock_start_jvm.assert_not_called()
52+
53+
# only after instantiating the class should we have a call to start_jvm
54+
func = Function(1)
55+
mock_start_jvm.assert_called_once()
56+
assert isinstance(func, jpype.JObject)

0 commit comments

Comments
 (0)