cli_test.py 2.54 KB
Newer Older
mashun1's avatar
veros  
mashun1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import sys
import filecmp
import fnmatch
import pkg_resources
import subprocess
from textwrap import dedent

from click.testing import CliRunner
import pytest

import veros.cli

SETUPS = (
    "acc",
    "acc_basic",
    "global_4deg",
    "global_1deg",
    "global_flexible",
    "north_atlantic",
)


@pytest.fixture(scope="module")
def runner():
    return CliRunner()


@pytest.mark.parametrize("setup", SETUPS)
def test_veros_copy_setup(setup, runner, tmpdir):
    result = runner.invoke(veros.cli.veros_copy_setup.cli, [setup, "--to", os.path.join(tmpdir, setup)])
    assert result.exit_code == 0, setup
    assert not result.output

    outpath = os.path.join(tmpdir, setup)
    srcpath = pkg_resources.resource_filename("veros", f"setups/{setup}")
    ignore = [
        f
        for f in os.listdir(srcpath)
        if any(fnmatch.fnmatch(f, pattern) for pattern in veros.cli.veros_copy_setup.IGNORE_PATTERNS)
    ]

    comparer = filecmp.dircmp(outpath, srcpath, ignore=ignore)
    assert not comparer.left_only and not comparer.right_only

    with open(os.path.join(outpath, f"{setup}.py"), "r") as f:
        setup_content = f.read()

    assert "VEROS_VERSION" in setup_content


def test_veros_run(runner, tmpdir):
    from veros import runtime_settings as rs

    setup = "acc"

    with runner.isolated_filesystem(tmpdir):
        result = runner.invoke(veros.cli.veros_copy_setup.cli, [setup])

        old_rs = {key: getattr(rs, key) for key in rs.__settings__}
        object.__setattr__(rs, "__locked__", False)

        try:
            result = runner.invoke(
                veros.cli.veros_run.cli, [os.path.join(setup, f"{setup}.py"), "--backend", rs.backend]
            )
        finally:
            # restore old settings
            for key, val in old_rs.items():
                object.__setattr__(rs, key, val)

        assert result.exit_code == 0


def test_import_isolation(tmpdir):
    TEST_KERNEL = dedent(
        """
    import sys
    import veros.cli

    for mod in sys.modules:
        print(mod)
    """
    )

    tmpfile = tmpdir / "isolation.py"
    with open(tmpfile, "w") as f:
        f.write(TEST_KERNEL)

    proc = subprocess.run([sys.executable, tmpfile], check=True, capture_output=True, text=True)

    imported_modules = proc.stdout.split()
    veros_modules = [mod for mod in imported_modules if mod.startswith("veros.")]

    for mod in veros_modules:
        assert mod.startswith("veros.cli") or mod == "veros._version"

    # make sure using the CLI does not initialize MPI
    assert "mpi4py" not in imported_modules