|
| 1 | +# SPDX-FileCopyrightText: 2026 Vaclav Petras |
| 2 | +# SPDX-FileCopyrightText: GRASS Development Team |
| 3 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | +"""Tests of using grass.lib in a session set up by grass.script.setup.init |
| 5 | +
|
| 6 | +This file is meant to be executed also standalone (pytest with this file as |
| 7 | +the only argument). The tests run their code in a subprocess without the |
| 8 | +dynamic library search path variable, so finding the libraries depends only |
| 9 | +on the variables init sets in the environment of the running process and on |
| 10 | +the search path recorded in the libraries at build time. |
| 11 | +""" |
| 12 | + |
| 13 | +import sys |
| 14 | +from textwrap import dedent |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +WINDOWS = sys.platform.startswith("win") |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.usefixtures("mock_no_session") |
| 22 | +def test_grass_lib_importable_after_init( |
| 23 | + tmp_path, run_code_in_environment, clean_subprocess_environment |
| 24 | +): |
| 25 | + """Check that grass.lib modules can be imported after init""" |
| 26 | + project = tmp_path / "test" |
| 27 | + code = f""" |
| 28 | + import json |
| 29 | + import grass.script as gs |
| 30 | +
|
| 31 | + gs.create_project(r"{project}") |
| 32 | + with gs.setup.init(r"{project}"): |
| 33 | + import grass.lib.gis |
| 34 | + import grass.lib.raster |
| 35 | + print(json.dumps({{"imported": True}})) |
| 36 | + """ |
| 37 | + assert run_code_in_environment(code, env=clean_subprocess_environment)["imported"] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.xfail( |
| 41 | + WINDOWS, |
| 42 | + strict=True, |
| 43 | + reason="On Windows, the C libraries (built with a different C runtime than" |
| 44 | + " Python) do not see the environment variables set by init in the running" |
| 45 | + " process, so they do not see the session", |
| 46 | +) |
| 47 | +@pytest.mark.usefixtures("mock_no_session") |
| 48 | +def test_grass_lib_usable_after_init( |
| 49 | + tmp_path, run_code_in_environment, clean_subprocess_environment |
| 50 | +): |
| 51 | + """Check that grass.lib sees the session and works with rasters after init""" |
| 52 | + project = tmp_path / "test" |
| 53 | + code = f""" |
| 54 | + import json |
| 55 | + import grass.script as gs |
| 56 | +
|
| 57 | + gs.create_project(r"{project}") |
| 58 | + with gs.setup.init(r"{project}"): |
| 59 | + import grass.lib.gis as libgis |
| 60 | + import grass.lib.raster as libraster |
| 61 | +
|
| 62 | + libgis.G_gisinit(b"test") |
| 63 | + gs.run_command("g.region", rows=2, cols=2) |
| 64 | + gs.mapcalc("ones = 1") |
| 65 | + fd = libraster.Rast_open_old(b"ones", b"") |
| 66 | + libraster.Rast_close(fd) |
| 67 | + print(json.dumps({{"raster_opened": True}})) |
| 68 | + """ |
| 69 | + assert run_code_in_environment(code, env=clean_subprocess_environment)[ |
| 70 | + "raster_opened" |
| 71 | + ] |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.usefixtures("mock_no_session") |
| 75 | +def test_grass_lib_usable_in_subprocess_of_init( |
| 76 | + tmp_path, run_code_in_environment, clean_subprocess_environment |
| 77 | +): |
| 78 | + """Check that grass.lib works in a process started from a session of init |
| 79 | +
|
| 80 | + The session exists in the environment of the started process from its |
| 81 | + start, so the C libraries see it on all platforms, including Windows, |
| 82 | + where they do not see a session set up in their own process. |
| 83 | + """ |
| 84 | + project = tmp_path / "test" |
| 85 | + child_file = tmp_path / "child.py" |
| 86 | + child_file.write_text( |
| 87 | + dedent( |
| 88 | + """ |
| 89 | + import json |
| 90 | +
|
| 91 | + import grass.lib.gis as libgis |
| 92 | + import grass.lib.raster as libraster |
| 93 | +
|
| 94 | + libgis.G_gisinit(b"test") |
| 95 | + fd = libraster.Rast_open_old(b"ones", b"") |
| 96 | + libraster.Rast_close(fd) |
| 97 | + print(json.dumps({"raster_opened": True})) |
| 98 | + """ |
| 99 | + ) |
| 100 | + ) |
| 101 | + code = f""" |
| 102 | + import subprocess |
| 103 | + import sys |
| 104 | +
|
| 105 | + import grass.script as gs |
| 106 | +
|
| 107 | + gs.create_project(r"{project}") |
| 108 | + with gs.setup.init(r"{project}"): |
| 109 | + gs.run_command("g.region", rows=2, cols=2) |
| 110 | + gs.mapcalc("ones = 1") |
| 111 | + result = subprocess.run( |
| 112 | + [sys.executable, r"{child_file}"], |
| 113 | + capture_output=True, |
| 114 | + text=True, |
| 115 | + check=True, |
| 116 | + ) |
| 117 | + print(result.stdout) |
| 118 | + """ |
| 119 | + assert run_code_in_environment(code, env=clean_subprocess_environment)[ |
| 120 | + "raster_opened" |
| 121 | + ] |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.skipif(sys.platform != "linux", reason="Reads /proc/self/maps") |
| 125 | +@pytest.mark.usefixtures("mock_no_session") |
| 126 | +def test_session_does_not_load_libraries( |
| 127 | + tmp_path, run_code_in_environment, clean_subprocess_environment |
| 128 | +): |
| 129 | + """Check that a session does not load the C libraries into the process |
| 130 | +
|
| 131 | + The check reads /proc/self/maps, hence it runs only on Linux. |
| 132 | + """ |
| 133 | + project = tmp_path / "test" |
| 134 | + code = f""" |
| 135 | + import json |
| 136 | + import grass.script as gs |
| 137 | +
|
| 138 | + gs.create_project(r"{project}") |
| 139 | + with gs.setup.init(r"{project}"): |
| 140 | + pass |
| 141 | + with open("/proc/self/maps") as maps: |
| 142 | + mapped = [line for line in maps if "libgrass_" in line] |
| 143 | + print(json.dumps({{"mapped": mapped}})) |
| 144 | + """ |
| 145 | + assert ( |
| 146 | + run_code_in_environment(code, env=clean_subprocess_environment)["mapped"] == [] |
| 147 | + ) |
0 commit comments