fork/mons

git clone https://git.julia.blue/fork/mons

coloursofnoiseMerge branch 'version-parsing' into mainc43b1d6

misc-fixes
2.5 KiB80 linesraw
1import errno
2import os
3import pathlib
4import sys
5from importlib import reload
6
7import pytest
8
9import mons.install
10from mons.mons import cli as mons_cli
11from mons.version import Version
12
13
14def pytest_collection_modifyitems(session, config, items):
15    module = pathlib.Path(os.path.dirname(__file__))
16    for item in items:
17        if module == item.path.parent:
18            item.add_marker(pytest.mark.slow_integration_test)
19
20
21# We only need to log this warning once for each test run
22log_test_install_warning = True
23
24
25@pytest.fixture(scope="function")
26def test_install_path(pytestconfig: pytest.Config, cache: pytest.Cache):
27    # prioritize cmdline arg so that it can be changed
28    install_path: str = pytestconfig.getoption("mons_test_install", cache.get("mons/test_install", None))  # type: ignore
29
30    if not install_path:
31        global log_test_install_warning
32        if log_test_install_warning:
33            log_test_install_warning = True
34            pytest.skip(
35                "Test install not configured. Use the `--mons-test-install` cmdline flag to provide a test install."
36            )
37        pytest.skip()
38
39    if os.path.exists(install_path):
40        install_path = os.path.abspath(install_path)
41        cache.set("mons/test_install", install_path)
42        return install_path
43    else:
44        raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), install_path)
45
46
47@pytest.fixture(scope="function")
48def test_install(runner, test_name, test_install_path, monkeypatch):
49    # use a stable version number for default branch detection
50    monkeypatch.setattr(
51        mons.install,
52        "parse_exe",
53        lambda *args: (Version(1, 4, 0, 0), Version(1, 4030, 0), "FNA"),
54    )
55
56    install_name = f"_pytest_{test_name}"
57    result = runner.invoke(mons_cli, ["add", install_name, test_install_path])
58    if result.exit_code != 0:
59        pytest.skip(result.output)
60    return install_name
61
62
63@pytest.fixture(autouse=True)
64def set_env(monkeypatch):
65    monkeypatch.setenv("MONS_DEBUG", "true")
66
67
68@pytest.fixture(autouse=True)
69def isolated_filesystem(monkeypatch: pytest.MonkeyPatch, tmp_path):
70    """Isolate all referenced files to temp folder"""
71    user_dirs = ["config", "cache", "data"]
72    ret = dict()
73    for dir in user_dirs:
74        monkeypatch.setattr(
75            f"mons.config.PlatformDirs.user_{dir}_dir", os.path.join(tmp_path, dir)
76        )
77        ret[f"{dir}_dir"] = os.path.join(tmp_path, dir)
78    # reload the config module to re-assign global constants
79    reload(sys.modules["mons.config"])
80    return ret