|
| 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