2023-08-07 15:39:44 +03:00
|
|
|
import shutil
|
2020-11-20 09:14:50 +03:00
|
|
|
import pytest
|
|
|
|
from subprocess import check_call
|
|
|
|
import os
|
2020-11-22 02:55:55 +03:00
|
|
|
import sys
|
2022-09-16 21:30:49 +03:00
|
|
|
from pathlib import Path
|
2020-11-22 02:55:55 +03:00
|
|
|
|
2020-11-20 09:14:50 +03:00
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2020-11-23 14:54:47 +03:00
|
|
|
def example_netstandard(tmpdir_factory):
|
|
|
|
return build_example(tmpdir_factory, "netstandard20")
|
|
|
|
|
2021-01-31 16:05:17 +03:00
|
|
|
|
2020-11-23 14:54:47 +03:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def example_netcore(tmpdir_factory):
|
2022-07-08 02:28:02 +03:00
|
|
|
return build_example(tmpdir_factory, "net60")
|
2020-11-23 14:54:47 +03:00
|
|
|
|
2021-01-31 16:05:17 +03:00
|
|
|
|
2020-11-23 14:54:47 +03:00
|
|
|
def build_example(tmpdir_factory, framework):
|
2022-09-16 21:30:49 +03:00
|
|
|
out = Path(tmpdir_factory.mktemp(f"example-{framework}"))
|
2023-08-07 16:21:26 +03:00
|
|
|
proj_path = Path(__file__).parent.parent / "example" / "example.csproj"
|
2020-11-20 09:14:50 +03:00
|
|
|
|
2022-09-16 21:30:49 +03:00
|
|
|
check_call(["dotnet", "build", str(proj_path), "-o", str(out), "-f", framework])
|
2020-11-20 09:14:50 +03:00
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
2023-08-07 15:39:44 +03:00
|
|
|
def test_mono(example_netstandard: Path):
|
2020-11-20 09:14:50 +03:00
|
|
|
from clr_loader import get_mono
|
|
|
|
|
2021-01-31 16:05:17 +03:00
|
|
|
mono = get_mono()
|
2022-09-16 21:30:49 +03:00
|
|
|
asm = mono.get_assembly(example_netstandard / "example.dll")
|
2020-11-20 09:14:50 +03:00
|
|
|
|
|
|
|
run_tests(asm)
|
|
|
|
|
|
|
|
|
2023-08-07 15:39:44 +03:00
|
|
|
def test_mono_debug(example_netstandard: Path):
|
2021-11-17 00:58:01 +03:00
|
|
|
from clr_loader import get_mono
|
|
|
|
|
|
|
|
mono = get_mono(
|
|
|
|
debug=True,
|
|
|
|
jit_options=[
|
|
|
|
"--debugger-agent=address=0.0.0.0:5831,transport=dt_socket,server=y"
|
|
|
|
],
|
|
|
|
)
|
2022-09-16 21:30:49 +03:00
|
|
|
asm = mono.get_assembly(example_netstandard / "example.dll")
|
2021-11-17 00:58:01 +03:00
|
|
|
|
|
|
|
run_tests(asm)
|
|
|
|
|
2023-08-07 15:39:44 +03:00
|
|
|
|
|
|
|
def test_mono_signal_chaining(example_netstandard: Path):
|
2023-01-03 15:08:04 +03:00
|
|
|
from clr_loader import get_mono
|
|
|
|
|
|
|
|
mono = get_mono(set_signal_chaining=True)
|
|
|
|
asm = mono.get_assembly(example_netstandard / "example.dll")
|
|
|
|
|
|
|
|
run_tests(asm)
|
|
|
|
|
2023-08-07 15:39:44 +03:00
|
|
|
|
|
|
|
def test_mono_set_dir(example_netstandard: Path):
|
2023-01-03 15:08:04 +03:00
|
|
|
from clr_loader import get_mono
|
|
|
|
|
|
|
|
mono = get_mono(assembly_dir="/usr/lib", config_dir="/etc")
|
|
|
|
asm = mono.get_assembly(example_netstandard / "example.dll")
|
|
|
|
|
|
|
|
run_tests(asm)
|
2021-11-17 00:58:01 +03:00
|
|
|
|
2023-08-07 15:39:44 +03:00
|
|
|
|
|
|
|
def test_coreclr(example_netcore: Path):
|
2020-11-20 09:14:50 +03:00
|
|
|
from clr_loader import get_coreclr
|
|
|
|
|
2022-09-16 21:30:49 +03:00
|
|
|
coreclr = get_coreclr(runtime_config=example_netcore / "example.runtimeconfig.json")
|
|
|
|
asm = coreclr.get_assembly(example_netcore / "example.dll")
|
|
|
|
|
|
|
|
run_tests(asm)
|
|
|
|
|
|
|
|
|
2023-08-07 16:21:26 +03:00
|
|
|
def test_coreclr_properties(example_netcore: Path):
|
|
|
|
from multiprocessing import get_context
|
|
|
|
|
|
|
|
p = get_context("spawn").Process(
|
|
|
|
target=_do_test_coreclr_autogenerated_runtimeconfig,
|
|
|
|
args=(example_netstandard,),
|
|
|
|
kwargs=dict(properties=dict(APP_CONTEXT_BASE_DIRECTORY=str(example_netcore))),
|
|
|
|
)
|
|
|
|
p.start()
|
|
|
|
p.join()
|
|
|
|
p.close()
|
|
|
|
|
|
|
|
|
2023-08-07 15:39:44 +03:00
|
|
|
def test_coreclr_autogenerated_runtimeconfig(example_netstandard: Path):
|
2023-01-03 14:57:47 +03:00
|
|
|
from multiprocessing import get_context
|
|
|
|
|
|
|
|
p = get_context("spawn").Process(
|
|
|
|
target=_do_test_coreclr_autogenerated_runtimeconfig, args=(example_netstandard,)
|
|
|
|
)
|
|
|
|
p.start()
|
|
|
|
p.join()
|
|
|
|
p.close()
|
|
|
|
|
|
|
|
|
2023-08-07 16:21:26 +03:00
|
|
|
def _do_test_coreclr_autogenerated_runtimeconfig(
|
|
|
|
example_netstandard: Path, **properties
|
|
|
|
):
|
2022-09-16 21:30:49 +03:00
|
|
|
from clr_loader import get_coreclr
|
|
|
|
|
2023-08-07 16:21:26 +03:00
|
|
|
coreclr = get_coreclr(properties=properties)
|
2022-09-16 21:30:49 +03:00
|
|
|
asm = coreclr.get_assembly(example_netstandard / "example.dll")
|
2020-11-20 09:14:50 +03:00
|
|
|
|
|
|
|
run_tests(asm)
|
|
|
|
|
|
|
|
|
2021-01-31 16:05:17 +03:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform != "win32", reason=".NET Framework only exists on Windows"
|
|
|
|
)
|
2023-08-07 15:39:44 +03:00
|
|
|
def test_netfx(example_netstandard: Path):
|
|
|
|
from clr_loader import get_netfx
|
|
|
|
|
|
|
|
netfx = get_netfx()
|
|
|
|
asm = netfx.get_assembly(example_netstandard / "example.dll")
|
|
|
|
|
|
|
|
run_tests(asm)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform != "win32", reason=".NET Framework only exists on Windows"
|
|
|
|
)
|
|
|
|
def test_netfx_chinese_path(example_netstandard: Path, tmpdir_factory):
|
2020-11-22 02:55:55 +03:00
|
|
|
from clr_loader import get_netfx
|
|
|
|
|
2023-08-07 15:39:44 +03:00
|
|
|
tmp_path = Path(tmpdir_factory.mktemp("example-中国"))
|
|
|
|
shutil.copytree(example_netstandard, tmp_path, dirs_exist_ok=True)
|
|
|
|
|
2020-11-22 02:55:55 +03:00
|
|
|
netfx = get_netfx()
|
2020-11-23 14:54:47 +03:00
|
|
|
asm = netfx.get_assembly(os.path.join(example_netstandard, "example.dll"))
|
2020-11-22 02:55:55 +03:00
|
|
|
|
|
|
|
run_tests(asm)
|
|
|
|
|
|
|
|
|
2020-11-20 09:14:50 +03:00
|
|
|
def run_tests(asm):
|
|
|
|
func = asm.get_function("Example.TestClass", "Test")
|
|
|
|
test_data = b"testy mctestface"
|
|
|
|
res = func(test_data)
|
|
|
|
assert res == len(test_data)
|