From 5e27d1a9ace29b816410546f237a62c999395307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sat, 14 Dec 2024 20:02:51 -0500 Subject: [PATCH] pytest: Allow default test file naming patterns inside tests folders (#4784) * pytest: Allow default test file naming patterns inside tests folders * tests: Patch environment variables in xy_session fixture for grassTask test * Update test_script_task.py Co-authored-by: Markus Neteler * Extract expected value to variable --------- Co-authored-by: Markus Neteler --- pyproject.toml | 2 +- python/grass/script/tests/test_script_task.py | 47 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f14ec703ce2..358ab047112 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -397,7 +397,7 @@ ignore = [ [tool.pytest.ini_options] minversion = "6.0" -python_files = "*/tests/*_test.py" +python_files = "*/tests/*_test.py */tests/test_*.py" addopts = """ --ignore-glob='dist.*' --ignore-glob='bin.*' diff --git a/python/grass/script/tests/test_script_task.py b/python/grass/script/tests/test_script_task.py index cae8735827e..aff10500d7c 100644 --- a/python/grass/script/tests/test_script_task.py +++ b/python/grass/script/tests/test_script_task.py @@ -1,11 +1,54 @@ +import os + +import pytest + +import grass.script as gs from grass.script.task import grassTask -def test_mapcalc_simple_e_name(): +@pytest.fixture +def xy_session_patched_env(tmp_path, monkeypatch): + """Active session in an XY location (scope: function), patching env vars directly. + + This allows functions not accepting an env dictionary argument to work in tests""" + location = "xy_test" + gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access + with gs.setup.init(tmp_path / location, env=os.environ.copy()) as session: + for key, value in session.env.items(): + monkeypatch.setenv(key, value) + yield session + + +def test_mapcalc_simple_e_name(xy_session_patched_env): gt = grassTask("r.mapcalc.simple") assert gt.get_param("e")["name"] == "e" -def test_mapcalc_simple_expession_name(): +def test_mapcalc_simple_expression_name(xy_session_patched_env): gt = grassTask("r.mapcalc.simple") assert gt.get_param("expression")["name"] == "expression" + + +def test_d_vect_from_bin(xy_session_patched_env): + """Tests that a module installed in "$GISBASE/bin can be used with grassTask""" + task = grassTask("d.vect") + task.get_param("map")["value"] = "map_name" + task.get_flag("i")["value"] = True + task.get_param("layer")["value"] = 1 + task.get_param("label_bcolor")["value"] = "red" + # the default parameter display is added automatically + actual = " ".join(task.get_cmd()) + expected = "d.vect -i map=map_name layer=1 display=shape label_bcolor=red" + assert actual == expected + + +def test_v_clip_from_scripts(xy_session_patched_env): + """Tests that a module installed in "$GISBASE/scripts can be used with grassTask""" + task = grassTask("v.clip") + task.get_param("input")["value"] = "map_name" + task.get_flag("r")["value"] = True + task.get_param("clip")["value"] = "clip_map_name" + task.get_param("output")["value"] = "output_map_name" + actual = " ".join(task.get_cmd()) + expected = "v.clip -r input=map_name clip=clip_map_name output=output_map_name" + assert actual == expected