From 99aed9b0ca099488dce7a580da957b583b7b2560 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Sun, 8 Mar 2026 11:33:17 +0530 Subject: [PATCH 1/8] add pytest-based test for rasterExists function --- python/grass/CMakeLists.txt | 1 + python/grass/pytest/__init__.py | 0 python/grass/pytest/assertions.py | 7 +++++++ python/grass/pytest/conftest.py | 12 ++++++++++++ python/grass/pytest/tests/test_assertions.py | 9 +++++++++ 5 files changed, 29 insertions(+) create mode 100644 python/grass/pytest/__init__.py create mode 100644 python/grass/pytest/assertions.py create mode 100644 python/grass/pytest/conftest.py create mode 100644 python/grass/pytest/tests/test_assertions.py diff --git a/python/grass/CMakeLists.txt b/python/grass/CMakeLists.txt index b089cb76160..3c8a6ca2aee 100644 --- a/python/grass/CMakeLists.txt +++ b/python/grass/CMakeLists.txt @@ -22,6 +22,7 @@ set(PYDIRS semantic_label temporal temporal/ply + testing tools utils) diff --git a/python/grass/pytest/__init__.py b/python/grass/pytest/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/python/grass/pytest/assertions.py b/python/grass/pytest/assertions.py new file mode 100644 index 00000000000..bf92fa2b627 --- /dev/null +++ b/python/grass/pytest/assertions.py @@ -0,0 +1,7 @@ +import grass.script as gs + + +def rasterExists(map_name, env=None): + """Check if a raster map exists in the given mapset.""" + result = gs.find_file(map_name, element="cell", env=env) + return bool(result["name"]) \ No newline at end of file diff --git a/python/grass/pytest/conftest.py b/python/grass/pytest/conftest.py new file mode 100644 index 00000000000..b403847d416 --- /dev/null +++ b/python/grass/pytest/conftest.py @@ -0,0 +1,12 @@ +import os +import pytest +import grass.script as gs + + +@pytest.fixture +def session(tmp_path): + project = tmp_path / "test_proj" + gs.create_project(project) + + with gs.setup.init(project, env=os.environ.copy()) as session: + yield session.env \ No newline at end of file diff --git a/python/grass/pytest/tests/test_assertions.py b/python/grass/pytest/tests/test_assertions.py new file mode 100644 index 00000000000..daf14da9b6c --- /dev/null +++ b/python/grass/pytest/tests/test_assertions.py @@ -0,0 +1,9 @@ +import grass.script as gs +from grass.pytest.assertions import rasterExists + + +def test_rasterExists(session): + gs.run_command("r.mapcalc", expression="test_map = 1", env=session) + + assert rasterExists("test_map", env=session) + assert not rasterExists("missing_map", env=session) \ No newline at end of file From 40d203c8aee42b69e207409fd68eb80d9ed9f4ee Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Sun, 8 Mar 2026 17:40:30 +0530 Subject: [PATCH 2/8] register pytest --- python/grass/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/grass/CMakeLists.txt b/python/grass/CMakeLists.txt index 3c8a6ca2aee..eb2fb2a1064 100644 --- a/python/grass/CMakeLists.txt +++ b/python/grass/CMakeLists.txt @@ -18,11 +18,11 @@ set(PYDIRS pygrass/shell pygrass/tests pygrass/vector + pytest script semantic_label temporal temporal/ply - testing tools utils) From dfe905ab07b8f19fb5cbd6fa98db3ac7f62c1ac9 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Sun, 8 Mar 2026 17:45:10 +0530 Subject: [PATCH 3/8] imporove code quality --- python/grass/pytest/assertions.py | 2 +- python/grass/pytest/conftest.py | 3 ++- python/grass/pytest/tests/test_assertions.py | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/python/grass/pytest/assertions.py b/python/grass/pytest/assertions.py index bf92fa2b627..b9e39a61c8b 100644 --- a/python/grass/pytest/assertions.py +++ b/python/grass/pytest/assertions.py @@ -4,4 +4,4 @@ def rasterExists(map_name, env=None): """Check if a raster map exists in the given mapset.""" result = gs.find_file(map_name, element="cell", env=env) - return bool(result["name"]) \ No newline at end of file + return bool(result["name"]) diff --git a/python/grass/pytest/conftest.py b/python/grass/pytest/conftest.py index b403847d416..dd7fc2c4017 100644 --- a/python/grass/pytest/conftest.py +++ b/python/grass/pytest/conftest.py @@ -9,4 +9,5 @@ def session(tmp_path): gs.create_project(project) with gs.setup.init(project, env=os.environ.copy()) as session: - yield session.env \ No newline at end of file + yield session.env + \ No newline at end of file diff --git a/python/grass/pytest/tests/test_assertions.py b/python/grass/pytest/tests/test_assertions.py index daf14da9b6c..49b874539ac 100644 --- a/python/grass/pytest/tests/test_assertions.py +++ b/python/grass/pytest/tests/test_assertions.py @@ -6,4 +6,5 @@ def test_rasterExists(session): gs.run_command("r.mapcalc", expression="test_map = 1", env=session) assert rasterExists("test_map", env=session) - assert not rasterExists("missing_map", env=session) \ No newline at end of file + assert not rasterExists("missing_map", env=session) + \ No newline at end of file From 013964999b9c76936fcfb51a345257109d0fa404 Mon Sep 17 00:00:00 2001 From: ayushjariyal <148481273+ayushjariyal@users.noreply.github.com> Date: Sun, 8 Mar 2026 17:46:50 +0530 Subject: [PATCH 4/8] Update python/grass/pytest/conftest.py Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- python/grass/pytest/conftest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/grass/pytest/conftest.py b/python/grass/pytest/conftest.py index dd7fc2c4017..b403847d416 100644 --- a/python/grass/pytest/conftest.py +++ b/python/grass/pytest/conftest.py @@ -9,5 +9,4 @@ def session(tmp_path): gs.create_project(project) with gs.setup.init(project, env=os.environ.copy()) as session: - yield session.env - \ No newline at end of file + yield session.env \ No newline at end of file From f81fe118644047bb364f4d30ad42f4fa46e69a16 Mon Sep 17 00:00:00 2001 From: ayushjariyal <148481273+ayushjariyal@users.noreply.github.com> Date: Sun, 8 Mar 2026 17:46:59 +0530 Subject: [PATCH 5/8] Update python/grass/pytest/tests/test_assertions.py Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- python/grass/pytest/tests/test_assertions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/grass/pytest/tests/test_assertions.py b/python/grass/pytest/tests/test_assertions.py index 49b874539ac..daf14da9b6c 100644 --- a/python/grass/pytest/tests/test_assertions.py +++ b/python/grass/pytest/tests/test_assertions.py @@ -6,5 +6,4 @@ def test_rasterExists(session): gs.run_command("r.mapcalc", expression="test_map = 1", env=session) assert rasterExists("test_map", env=session) - assert not rasterExists("missing_map", env=session) - \ No newline at end of file + assert not rasterExists("missing_map", env=session) \ No newline at end of file From e19fcaaad646902b290dc30214dd8a1437ff0361 Mon Sep 17 00:00:00 2001 From: ayushjariyal <148481273+ayushjariyal@users.noreply.github.com> Date: Sun, 8 Mar 2026 17:48:08 +0530 Subject: [PATCH 6/8] Update python/grass/pytest/conftest.py Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- python/grass/pytest/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/grass/pytest/conftest.py b/python/grass/pytest/conftest.py index b403847d416..4ce702ae7b1 100644 --- a/python/grass/pytest/conftest.py +++ b/python/grass/pytest/conftest.py @@ -9,4 +9,4 @@ def session(tmp_path): gs.create_project(project) with gs.setup.init(project, env=os.environ.copy()) as session: - yield session.env \ No newline at end of file + yield session.env From a8aad326b56e2a05967d1ab875adc2727ce16b7d Mon Sep 17 00:00:00 2001 From: ayushjariyal <148481273+ayushjariyal@users.noreply.github.com> Date: Sun, 8 Mar 2026 17:48:16 +0530 Subject: [PATCH 7/8] Update python/grass/pytest/tests/test_assertions.py Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- python/grass/pytest/tests/test_assertions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/grass/pytest/tests/test_assertions.py b/python/grass/pytest/tests/test_assertions.py index daf14da9b6c..0af15610e9e 100644 --- a/python/grass/pytest/tests/test_assertions.py +++ b/python/grass/pytest/tests/test_assertions.py @@ -6,4 +6,4 @@ def test_rasterExists(session): gs.run_command("r.mapcalc", expression="test_map = 1", env=session) assert rasterExists("test_map", env=session) - assert not rasterExists("missing_map", env=session) \ No newline at end of file + assert not rasterExists("missing_map", env=session) From 7df831d489212367dfa414dbcb8d059f476e2ef3 Mon Sep 17 00:00:00 2001 From: Ayush Kumar Date: Sun, 8 Mar 2026 18:40:54 +0530 Subject: [PATCH 8/8] fix errors --- python/grass/pytest/assertions.py | 14 ++++++------ python/grass/pytest/conftest.py | 24 ++++++++++---------- python/grass/pytest/tests/test_assertions.py | 18 +++++++-------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/python/grass/pytest/assertions.py b/python/grass/pytest/assertions.py index b9e39a61c8b..4171a22553e 100644 --- a/python/grass/pytest/assertions.py +++ b/python/grass/pytest/assertions.py @@ -1,7 +1,7 @@ -import grass.script as gs - - -def rasterExists(map_name, env=None): - """Check if a raster map exists in the given mapset.""" - result = gs.find_file(map_name, element="cell", env=env) - return bool(result["name"]) +import grass.script as gs + + +def rasterExists(map_name, env=None): + """Check if a raster map exists in the given mapset.""" + result = gs.find_file(map_name, element="cell", env=env) + return bool(result["name"]) diff --git a/python/grass/pytest/conftest.py b/python/grass/pytest/conftest.py index 4ce702ae7b1..afdff253e3a 100644 --- a/python/grass/pytest/conftest.py +++ b/python/grass/pytest/conftest.py @@ -1,12 +1,12 @@ -import os -import pytest -import grass.script as gs - - -@pytest.fixture -def session(tmp_path): - project = tmp_path / "test_proj" - gs.create_project(project) - - with gs.setup.init(project, env=os.environ.copy()) as session: - yield session.env +import os +import pytest +import grass.script as gs + + +@pytest.fixture +def session(tmp_path): + project = tmp_path / "test_proj" + gs.create_project(project) + + with gs.setup.init(project, env=os.environ.copy()) as session: + yield session.env diff --git a/python/grass/pytest/tests/test_assertions.py b/python/grass/pytest/tests/test_assertions.py index 0af15610e9e..23f3172305c 100644 --- a/python/grass/pytest/tests/test_assertions.py +++ b/python/grass/pytest/tests/test_assertions.py @@ -1,9 +1,9 @@ -import grass.script as gs -from grass.pytest.assertions import rasterExists - - -def test_rasterExists(session): - gs.run_command("r.mapcalc", expression="test_map = 1", env=session) - - assert rasterExists("test_map", env=session) - assert not rasterExists("missing_map", env=session) +import grass.script as gs +from grass.pytest.assertions import rasterExists + + +def test_rasterExists(session): + gs.run_command("r.mapcalc", expression="test_map = 1", env=session) + + assert rasterExists("test_map", env=session) + assert not rasterExists("missing_map", env=session)