Skip to content
Closed
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
57 changes: 57 additions & 0 deletions general/g.list/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import grass.script as gs
import pytest

TEST_MAPSETS = ["test_1", "test_2"]


@pytest.fixture
def simple_dataset(tmp_path_factory, monkeypatch):
"""Set up a GRASS session for the tests."""
tmp_path = tmp_path_factory.mktemp("simple_dataset")
project = "test_project"

# Create a test project
gs.create_project(tmp_path, project)

# Initialize the GRASS session
with gs.setup.init(tmp_path / project, env=os.environ.copy()) as session:
for key, value in session.env.items():
monkeypatch.setenv(key, value)

gs.run_command("g.region", rows=3, cols=3, env=session.env)

# Create Mock Mapsets and data in each
for mapset in TEST_MAPSETS:
gs.run_command(
"g.mapset", project=project, mapset=mapset, flags="c", env=session.env
)

# Create a raster in this mapset
gs.mapcalc(f"raster_{mapset} = int(row())", env=session.env)
gs.run_command(
"r.support",
map=f"raster_{mapset}",
title=f"Raster title {mapset}",
env=session.env,
)

# Create a vector in this mapset
gs.run_command(
"v.mkgrid",
map=f"vector_{mapset}",
grid=[10, 10],
type="point",
env=session.env,
)
gs.run_command(
"v.support",
map=f"vector_{mapset}",
map_name=f"Vector title {mapset}",
env=session.env,
)

# Set current mapset to test_1
gs.run_command("g.mapset", project=project, mapset="test_1", env=session.env)

yield session
117 changes: 117 additions & 0 deletions general/g.list/tests/g_list_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import grass.script as gs


def test_default_output(simple_dataset):
"""Test default output of g.list."""
# Current mapset only
actual = gs.read_command("g.list", type="all", env=simple_dataset.env).splitlines()
expected = ["raster_test_1", "vector_test_1"]
assert actual == expected

# All mapsets
actual = gs.read_command(
"g.list", type="all", mapset="*", env=simple_dataset.env
).splitlines()
expected = [
"raster_test_1",
"raster_test_2",
"vector_test_1",
"vector_test_2",
]
assert actual == expected


def test_data_types_output(simple_dataset):
"""Test g.list with t flag."""
# Current mapset only
actual = gs.read_command(
"g.list", type="all", flags="t", env=simple_dataset.env
).splitlines()
expected = ["raster/raster_test_1", "vector/vector_test_1"]
assert actual == expected

# All mapsets
actual = gs.read_command(
"g.list", type="all", flags="t", mapset="*", env=simple_dataset.env
).splitlines()
expected = [
"raster/raster_test_1",
"raster/raster_test_2",
"vector/vector_test_1",
"vector/vector_test_2",
]
assert actual == expected


def test_full_map_output(simple_dataset):
"""Test g.list with m flag."""
# Current mapset only
actual = gs.read_command(
"g.list", type="all", flags="m", env=simple_dataset.env
).splitlines()
expected = ["raster_test_1@test_1", "vector_test_1@test_1"]
assert actual == expected

# All mapsets
actual = gs.read_command(
"g.list", type="all", flags="m", mapset="*", env=simple_dataset.env
).splitlines()
expected = [
"raster_test_1@test_1",
"raster_test_2@test_2",
"vector_test_1@test_1",
"vector_test_2@test_2",
]
assert actual == expected


def test_human_readable_output(simple_dataset):
"""Test g.list with p flag."""
# Current mapset only
actual = gs.read_command(
"g.list", type="all", flags="p", env=simple_dataset.env
).splitlines()
expected = [
"raster files available in mapset <test_1>:",
"raster_test_1",
"no 3D raster files available in current mapset",
"vector files available in mapset <test_1>:",
"vector_test_1",
"no label files available in current mapset",
"no region definition files available in current mapset",
"no imagery group files available in current mapset",
]
for line in expected:
assert line in actual, f"Expected line not found: {line}"

# All mapsets
actual = gs.read_command(
"g.list", type="all", flags="p", mapset="*", env=simple_dataset.env
).splitlines()
expected = [
"raster files available in mapset <test_1>:",
"raster_test_1",
"raster files available in mapset <test_2>:",
"raster_test_2",
"no 3D raster files available in current mapset",
"vector files available in mapset <test_1>:",
"vector_test_1",
"vector files available in mapset <test_2>:",
"vector_test_2",
"no label files available in current mapset",
"no region definition files available in current mapset",
"no imagery group files available in current mapset",
]
for line in expected:
assert line in actual, f"Expected line not found: {line}"


def test_verbose_listing_output(simple_dataset):
"""Test g.list with f flag."""
actual = gs.read_command(
"g.list", type="all", flags="f", mapset="*", env=simple_dataset.env
).splitlines()
expected = gs.read_command(
"r.info", map="raster_test_1", env=simple_dataset.env
).splitlines()
assert expected == actual

Check failure on line 117 in general/g.list/tests/g_list_test.py

View workflow job for this annotation

GitHub Actions / macOS build

test_verbose_listing_output AssertionError: assert [' +---------... |', ...] == ['-----------...---', '', ...] At index 0 diff: ' +----------------------------------------------------------------------------+' != '----------------------------------------------' Right contains 21 more items, first extra item: '' Full diff: [ + ' ' - '----------------------------------------------', + '+----------------------------------------------------------------------------+', ? + +++++++++++++++++++++++++++++++ - 'raster files available in mapset <test_1>:', - 'raster_test_1 Raster title test_1', + ' | Map: raster_test_1 Date: Fri Jun 20 05:48:16 ' + '2025 |', + ' | Mapset: test_1 Login of Creator: ' + 'runner |', + ' | Project: ' + 'test_project |', + ' | DataBase: ' + '/private/var/folders/y6/nj790rtn62lfktb1sh__79hc0000gn/T/pytest- |', + ' | Title: Raster title ' + 'test_1 |', + ' | Timestamp: ' + 'none |', + ' ' + '|----------------------------------------------------------------------------|', + ' ' + '| ' + '|', + ' | Type of Map: raster Number of Categories: ' + '0 |', + ' | Data Type: CELL Semantic label: ' + '(none) |', + ' | Rows: ' + '1 |', + ' | Columns: ' + '1 |', + ' | Total Cells: ' + '1 |', + ' | Projection: ' + 'x,y |', + ' | N: 1 S: 0 Res: ' + '1 |', + ' | E: 1 W: 0 Res: ' + '1 |', + ' | Range of data: min = 1 max = ' + '1 |', + ' ' + '|

Check failure on line 117 in general/g.list/tests/g_list_test.py

View workflow job for this annotation

GitHub Actions / pytest (ubuntu-22.04, 3.13)

test_verbose_listing_output AssertionError: assert [' +---------... |', ...] == ['-----------...est_1>:', ...] At index 0 diff: ' +----------------------------------------------------------------------------+' != '----------------------------------------------' Right contains 21 more items, first extra item: '' Full diff: [ + ' ' - '----------------------------------------------', + '+----------------------------------------------------------------------------+', ? + +++++++++++++++++++++++++++++++ + ' | Map: raster_test_1 Date: Fri Jun 20 05:49:30 ' + '2025 |', + ' | Mapset: test_1 Login of Creator: ' + 'runner |', + ' | Project: ' + 'test_project |', + ' | DataBase: ' + '/tmp/pytest-of-runner/pytest-0/popen-gw0/simple_dataset4 |', + ' | Title: Raster title ' + 'test_1 |', + ' | Timestamp: ' + 'none |', + ' ' + '|----------------------------------------------------------------------------|', + ' ' + '| ' + '|', + ' | Type of Map: raster Number of Categories: ' + '0 |', + ' | Data Type: CELL Semantic label: ' + '(none) |', + ' | Rows: ' + '1 |', + ' | Columns: ' + '1 |', + ' | Total Cells: ' + '1 |', + ' | Projection: ' + 'x,y |', + ' | N: 1 S: 0 Res: ' + '1 |', + ' | E: 1 W: 0 Res: ' + '1 |', + ' | Range of data: min = 1 max = ' + '1 |', + ' ' + '| ' + '|', + ' | Data ' + 'Description:

Check failure on line 117 in general/g.list/tests/g_list_test.py

View workflow job for this annotation

GitHub Actions / pytest (ubuntu-22.04, 3.9)

test_verbose_listing_output AssertionError: assert [' +---------... |', ...] == ['-----------...est_1>:', ...] At index 0 diff: ' +----------------------------------------------------------------------------+' != '----------------------------------------------' Right contains 21 more items, first extra item: '' Full diff: [ + ' ' - '----------------------------------------------', + '+----------------------------------------------------------------------------+', ? + +++++++++++++++++++++++++++++++ + ' | Map: raster_test_1 Date: Fri Jun 20 05:49:41 ' + '2025 |', + ' | Mapset: test_1 Login of Creator: ' + 'runner |', + ' | Project: ' + 'test_project |', + ' | DataBase: ' + '/tmp/pytest-of-runner/pytest-0/popen-gw0/simple_dataset4 |', + ' | Title: Raster title ' + 'test_1 |', + ' | Timestamp: ' + 'none |', + ' ' + '|----------------------------------------------------------------------------|', + ' ' + '| ' + '|', + ' | Type of Map: raster Number of Categories: ' + '0 |', + ' | Data Type: CELL Semantic label: ' + '(none) |', + ' | Rows: ' + '1 |', + ' | Columns: ' + '1 |', + ' | Total Cells: ' + '1 |', + ' | Projection: ' + 'x,y |', + ' | N: 1 S: 0 Res: ' + '1 |', + ' | E: 1 W: 0 Res: ' + '1 |', + ' | Range of data: min = 1 max = ' + '1 |', + ' ' + '| ' + '|', + ' | Data ' + 'Description:
Loading