Skip to content
Open
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
12 changes: 12 additions & 0 deletions python/grass/script/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ def scan(gisbase, directory):
for directory in ("bin", "scripts"):
scan(gisbase, directory)

# Add addon path if it exists
if env.get("GRASS_ADDON_BASE"):
addon_base = env["GRASS_ADDON_BASE"]
scan(addon_base, "bin")
if not sys.platform.startswith("win"):
scan(addon_base, "scripts")

if env.get("GRASS_ADDON_PATH"):
for path in env["GRASS_ADDON_PATH"].split(os.pathsep):
if path:
scan(path, "")

return set(cmd), scripts


Expand Down
30 changes: 29 additions & 1 deletion python/grass/script/tests/grass_script_core_get_commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
import sys

import stat
import pytest

import grass.script as gs
Expand Down Expand Up @@ -65,3 +65,31 @@ def test_in_session(tmp_path):
with gs.setup.init(project, env=os.environ.copy()) as session:
executables_set, scripts_dict = gs.get_commands(env=session.env)
common_test_code(executables_set, scripts_dict)


def test_addon_path_multiple_dirs(tmp_path):
"""Test that get_commands scans multiple directories in GRASS_ADDON_PATH"""

test_dirs = [
(tmp_path / "addon_a", "my_custom_cmd_a"),
(tmp_path / "addon_b", "my_custom_cmd_b"),
]

ext = ".py" if sys.platform == "win32" else ""

for path, cmd_name in test_dirs:
path.mkdir()
file_path = path / f"{cmd_name}{ext}"
file_path.write_text(
"#!/usr/bin/env python3\nprint('Test ADDON PATH')", encoding="utf-8"
)
file_path.chmod(stat.S_IRWXU)

new_path = os.pathsep.join(str(p) for p, _ in test_dirs)
env = os.environ.copy()
env["GRASS_ADDON_PATH"] = new_path

executables_set, _ = gs.get_commands(env=env)

assert "my_custom_cmd_a" in executables_set
assert "my_custom_cmd_b" in executables_set
12 changes: 4 additions & 8 deletions scripts/g.extension/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
import pytest

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


# Using module-scoped fixture to avoid overhead of downloading addons repo.
@pytest.fixture(scope="module")
def tools(tmp_path_factory):
"""Tools with modified addon base directory (scope: module)"""
def session(tmp_path_factory):
"""Session with modified addon base directory (scope: module)"""
tmp_path = tmp_path_factory.mktemp("g_extension_tests")
gs.create_project(tmp_path / "test")
env = os.environ.copy()
env["GRASS_ADDON_BASE"] = str(tmp_path / "addons")
with (
gs.setup.init(tmp_path / "test", env=env) as session,
Tools(session=session, consistent_return_value=True) as tools,
):
yield tools
with gs.setup.init(tmp_path / "test", env=env) as session:
yield session
13 changes: 10 additions & 3 deletions scripts/g.extension/tests/g_extension_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
"""Test g.extension"""

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


# This should cover both C and Python tools.
@pytest.mark.parametrize("name", ["r.stream.distance", "r.lake.series"])
def test_install(tools, name):
def test_install(session, name):
"""Test that a tools installs and gives help message"""
tools.g_extension(extension=name)
assert tools.call_cmd([name, "--help"]).stderr
with Tools(session=session, consistent_return_value=True) as tools:
tools.g_extension(extension=name)
assert tools.call_cmd([name, "--help"]).stderr
# Check that the installed extension is in the get_commands() list
# i.e., GRASS_ADDON_BASE is accessible or not.
commands = gs.get_commands(env=session.env)[0]
assert name in commands, f"Extension {name} not found in get_commands() list"
Loading