Skip to content

Commit 191f39f

Browse files
authored
tests: Use more env for session isolation (OSGeo#5969)
In clear cases, pass a copy of os.environ to grass.script.setup.init instead of modifying the os.environ (and modifying it for all following tests). There are couple complex cases which are not covered here (global/subprocess tests of grass.script.init, GridModule, grass.jupyter).
1 parent 4c35d19 commit 191f39f

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

general/g.mapset/tests/conftest.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from types import SimpleNamespace
23

34
import grass.script as gs
@@ -13,18 +14,21 @@ def simple_dataset(tmp_path_factory):
1314
project = "test"
1415

1516
# Create a test project
16-
gs.create_project(tmp_path, project)
17+
gs.create_project(tmp_path / project)
1718

1819
# Initialize the GRASS session
19-
with gs.setup.init(tmp_path / project):
20+
with gs.setup.init(tmp_path / project, env=os.environ.copy()) as session:
2021
# Create Mock Mapsets
2122
for mapset in TEST_MAPSETS:
22-
gs.run_command("g.mapset", project=project, mapset=mapset, flags="c")
23+
gs.run_command(
24+
"g.mapset", project=project, mapset=mapset, flags="c", env=session.env
25+
)
2326

2427
# Set current mapset to test1
25-
gs.run_command("g.mapset", project=project, mapset="test1")
28+
gs.run_command("g.mapset", project=project, mapset="test1", env=session.env)
2629

2730
yield SimpleNamespace(
31+
session=session,
2832
mapsets=TEST_MAPSETS,
2933
project=project,
3034
current_mapset="test1",

general/g.mapset/tests/g_mapset_test.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
def test_plain_list_output(simple_dataset):
55
"""Test g.mapset with list flag and plain format"""
66
mapsets = simple_dataset.mapsets
7-
text = gs.read_command("g.mapset", format="plain", flags="l")
7+
text = gs.read_command(
8+
"g.mapset", format="plain", flags="l", env=simple_dataset.session.env
9+
)
810
parsed_list = text.strip().split()
911

1012
assert len(parsed_list) == len(mapsets)
@@ -14,14 +16,18 @@ def test_plain_list_output(simple_dataset):
1416

1517
def test_plain_print_output(simple_dataset):
1618
"""Test g.mapset with print flag and plain format"""
17-
text = gs.read_command("g.mapset", format="plain", flags="p")
19+
text = gs.read_command(
20+
"g.mapset", format="plain", flags="p", env=simple_dataset.session.env
21+
)
1822
assert text.strip() == simple_dataset.current_mapset
1923

2024

2125
def test_json_list_output(simple_dataset):
2226
"""Test g.mapset with list flag and JSON format"""
2327
mapsets = simple_dataset.mapsets
24-
data = gs.parse_command("g.mapset", format="json", flags="l")
28+
data = gs.parse_command(
29+
"g.mapset", format="json", flags="l", env=simple_dataset.session.env
30+
)
2531
assert list(data.keys()) == ["project", "mapsets"]
2632
assert isinstance(data["mapsets"], list)
2733
assert len(data["mapsets"]) == len(mapsets)
@@ -31,7 +37,9 @@ def test_json_list_output(simple_dataset):
3137

3238
def test_json_print_output(simple_dataset):
3339
"""Test g.mapset with print flag and JSON format"""
34-
data = gs.parse_command("g.mapset", format="json", flags="p")
40+
data = gs.parse_command(
41+
"g.mapset", format="json", flags="p", env=simple_dataset.session.env
42+
)
3543
assert list(data.keys()) == ["project", "mapset"]
3644
assert data["mapset"] == simple_dataset.current_mapset
3745
assert data["project"] == simple_dataset.project

general/g.mapsets/tests/conftest.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Fixture for ReprojectionRenderer test with simple GRASS location, raster, vector.
66
"""
77

8+
import os
89
from types import SimpleNamespace
910

1011
import grass.script as gs
@@ -23,13 +24,30 @@ def simple_dataset(tmp_path_factory):
2324
project_name = "test"
2425
project = tmp_path / project_name
2526
gs.create_project(project)
26-
with gs.setup.init(project):
27-
gs.run_command("g.proj", flags="c", epsg=26917)
28-
gs.run_command("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10)
27+
with gs.setup.init(project, env=os.environ.copy()) as session:
28+
gs.run_command("g.proj", flags="c", epsg=26917, env=session.env)
29+
gs.run_command(
30+
"g.region",
31+
s=0,
32+
n=80,
33+
w=0,
34+
e=120,
35+
b=0,
36+
t=50,
37+
res=10,
38+
res3=10,
39+
env=session.env,
40+
)
2941
# Create Mock Mapsets
3042
for mapset in TEST_MAPSETS:
31-
gs.run_command("g.mapset", project=project_name, mapset=mapset, flags="c")
43+
gs.run_command(
44+
"g.mapset",
45+
project=project_name,
46+
mapset=mapset,
47+
flags="c",
48+
env=session.env,
49+
)
3250

3351
yield SimpleNamespace(
34-
mapsets=TEST_MAPSETS, accessible_mapsets=ACCESSIBLE_MAPSETS
52+
session=session, mapsets=TEST_MAPSETS, accessible_mapsets=ACCESSIBLE_MAPSETS
3553
)

general/g.mapsets/tests/g_mapsets_list_format_test.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ def _check_parsed_list(mapsets, text, sep="|"):
3939
def test_plain_list_output(simple_dataset, separator):
4040
"""Test that the separators are properly applied with list flag"""
4141
mapsets = simple_dataset.mapsets
42-
text = gs.read_command("g.mapsets", format="plain", separator=separator, flags="l")
42+
text = gs.read_command(
43+
"g.mapsets",
44+
format="plain",
45+
separator=separator,
46+
flags="l",
47+
env=simple_dataset.session.env,
48+
)
4349
_check_parsed_list(mapsets, text, gutils.separator(separator))
4450

4551

@@ -51,13 +57,21 @@ def test_plain_list_output(simple_dataset, separator):
5157
def test_plain_print_output(simple_dataset, separator):
5258
"""Test that the separators are properly applied with print flag"""
5359
mapsets = simple_dataset.accessible_mapsets
54-
text = gs.read_command("g.mapsets", format="plain", separator=separator, flags="p")
60+
text = gs.read_command(
61+
"g.mapsets",
62+
format="plain",
63+
separator=separator,
64+
flags="p",
65+
env=simple_dataset.session.env,
66+
)
5567
_check_parsed_list(mapsets, text, gutils.separator(separator))
5668

5769

5870
def test_json_list_output(simple_dataset):
5971
"""Check list of mapsets in JSON format"""
60-
text = gs.read_command("g.mapsets", format="json", flags="l")
72+
text = gs.read_command(
73+
"g.mapsets", format="json", flags="l", env=simple_dataset.session.env
74+
)
6175
data = json.loads(text)
6276
assert list(data.keys()) == ["mapsets"]
6377
assert isinstance(data["mapsets"], list)
@@ -68,7 +82,9 @@ def test_json_list_output(simple_dataset):
6882

6983
def test_json_print_output(simple_dataset):
7084
"""Check search path mapsets in JSON format"""
71-
text = gs.read_command("g.mapsets", format="json", flags="p")
85+
text = gs.read_command(
86+
"g.mapsets", format="json", flags="p", env=simple_dataset.session.env
87+
)
7288
data = json.loads(text)
7389
assert list(data.keys()) == ["mapsets"]
7490
assert isinstance(data["mapsets"], list)

python/grass/script/setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ class SessionHandle:
456456
457457
import grass.script as gs
458458
459-
with gs.setup.init("~/grassdata/nc_spm_08/user1", env=os.environ.copy()):
459+
with gs.setup.init(
460+
"~/grassdata/nc_spm_08/user1", env=os.environ.copy()
461+
) as session:
460462
# ... use GRASS modules here with env parameter
461463
gs.run_command("g.region", flags="p", env=session.env)
462464
# session ends automatically here, global environment was never modified

python/grass/script/tests/grass_script_core_get_commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test grass.script.core.get_commands function"""
22

3+
import os
34
import sys
45

56
import pytest
@@ -42,6 +43,6 @@ def test_in_session(tmp_path):
4243
# TODO: Use env=os.environ.copy()
4344
project = tmp_path / "project"
4445
gs.create_project(project)
45-
with gs.setup.init(project) as session:
46+
with gs.setup.init(project, env=os.environ.copy()) as session:
4647
executables_set, scripts_dict = gs.get_commands(env=session.env)
4748
common_test_code(executables_set, scripts_dict)

0 commit comments

Comments
 (0)