diff --git a/temporal/t.list/t.list.py b/temporal/t.list/t.list.py index 4a38e22fecf..858942fa74e 100755 --- a/temporal/t.list/t.list.py +++ b/temporal/t.list/t.list.py @@ -99,6 +99,7 @@ from contextlib import nullcontext import grass.script as gs +from grass.tools import Tools ############################################################################ @@ -149,6 +150,27 @@ def main(): elif not separator: # output_format == "plain" separator = "|" + tools = Tools() + + conn = tools.t_connect(flags="p", format="json") + + db_exists = conn["driver"] and conn["database"] + + # if no connection is found + if not db_exists: + if output_format == "plain": + gs.message( + _("No temporal database exists in this mapset. No datasets to list.") + ) + with ( + open(outpath, "w") + if (outpath and outpath != "-") + else nullcontext(sys.stdout) + ) as out_file: + if output_format == "json": + out_file.write(json.dumps([], indent=4) + "\n") + return + # Lazy import import grass.temporal as tgis diff --git a/temporal/t.list/tests/conftest.py b/temporal/t.list/tests/conftest.py index bc1493fee6f..3ba6be95ed4 100644 --- a/temporal/t.list/tests/conftest.py +++ b/temporal/t.list/tests/conftest.py @@ -56,3 +56,14 @@ def space_time_dataset(tmp_path_factory): name=dataset_name, map_names=names, ) + + +@pytest.fixture(scope="module") +def empty_session(tmp_path_factory): + """Start an isolated session with no temporal database.""" + tmp_path = tmp_path_factory.mktemp("empty_time_series") + project = tmp_path / "empty_project" + gs.create_project(project) + + with gs.setup.init(project, env=os.environ.copy()) as session: + yield SimpleNamespace(session=session) diff --git a/temporal/t.list/tests/test_t_list.py b/temporal/t.list/tests/test_t_list.py index 68887c2c643..ae8d31486cd 100644 --- a/temporal/t.list/tests/test_t_list.py +++ b/temporal/t.list/tests/test_t_list.py @@ -131,3 +131,23 @@ def test_colhead_validation(space_time_dataset, output_format): assert status == 0 else: assert status != 0 + + +def test_t_list_empty_database(empty_session): + """Verify that t.list handles a missing TGIS database without creating one.""" + tools = Tools(session=empty_session.session) + + json_result = tools.t_list(type="strds", format="json") + assert json_result.returncode == 0 + assert json_result.stdout.strip() == "[]" + + csv_result = tools.t_list(type="strds", format="csv") + assert csv_result is None + + plain_result = tools.t_list(type="strds", format="plain") + assert plain_result is None + + # Ensure no connection is established + result = tools.t_connect(flags="p", format="json") + db_exist = result["driver"] and result["database"] + assert not db_exist