Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions temporal/t.list/t.list.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
from contextlib import nullcontext

import grass.script as gs
from grass.tools import Tools

############################################################################

Expand Down Expand Up @@ -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
Comment thread
saket0187 marked this conversation as resolved.
Resolved

# Lazy import
import grass.temporal as tgis

Expand Down
11 changes: 11 additions & 0 deletions temporal/t.list/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 20 additions & 0 deletions temporal/t.list/tests/test_t_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading