From ae40963c7977b927996544988da3b999e5cb9913 Mon Sep 17 00:00:00 2001
From: Frederik Hennig <frederik.hennig@fau.de>
Date: Sat, 30 Nov 2024 20:49:29 +0100
Subject: [PATCH] Add fixture for sample config modules. Add test cases for
 CLI.

---
 conftest.py                                  |  9 +++
 pytest.ini                                   |  2 +-
 src/pystencilssfg/config.py                  |  1 +
 tests/{generator => }/data/project_config.py |  0
 tests/generator/test_config.py               | 13 ++--
 tests/generator_scripts/scripts/Variables.py |  3 +-
 tests/integration/test_cli.py                | 68 ++++++++++++++++++++
 7 files changed, 84 insertions(+), 12 deletions(-)
 rename tests/{generator => }/data/project_config.py (100%)
 create mode 100644 tests/integration/test_cli.py

diff --git a/conftest.py b/conftest.py
index e1d0cdd..1c85902 100644
--- a/conftest.py
+++ b/conftest.py
@@ -1,4 +1,5 @@
 import pytest
+from os import path
 
 
 @pytest.fixture(autouse=True)
@@ -9,3 +10,11 @@ def prepare_composer(doctest_namespace):
 
     sfg = SfgComposer(SfgContext())
     doctest_namespace["sfg"] = sfg
+
+
+DATA_DIR = path.join(path.split(__file__)[0], "tests/data")
+
+
+@pytest.fixture
+def sample_config_module():
+    return path.join(DATA_DIR, "project_config.py")
diff --git a/pytest.ini b/pytest.ini
index 94a3a6c..5d05ec3 100644
--- a/pytest.ini
+++ b/pytest.ini
@@ -3,6 +3,6 @@ testpaths = src/pystencilssfg tests/
 python_files = "test_*.py"
 #   Need to ignore the generator scripts, otherwise they would be executed
 #   during test collection
-addopts = --doctest-modules --ignore=tests/generator_scripts/scripts
+addopts = --doctest-modules --ignore=tests/generator_scripts/scripts --ignore=--ignore=tests/data
 
 doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
diff --git a/src/pystencilssfg/config.py b/src/pystencilssfg/config.py
index 9cbb5c5..8562e0e 100644
--- a/src/pystencilssfg/config.py
+++ b/src/pystencilssfg/config.py
@@ -316,6 +316,7 @@ class CommandLineParameters:
         h_ext = None
         src_ext = None
 
+        extensions = tuple(ext.strip() for ext in extensions)
         extensions = tuple((ext[1:] if ext[0] == "." else ext) for ext in extensions)
 
         HEADER_FILE_EXTENSIONS = {"h", "hpp", "hxx", "h++", "cuh"}
diff --git a/tests/generator/data/project_config.py b/tests/data/project_config.py
similarity index 100%
rename from tests/generator/data/project_config.py
rename to tests/data/project_config.py
diff --git a/tests/generator/test_config.py b/tests/generator/test_config.py
index 66b0a9f..a696d7b 100644
--- a/tests/generator/test_config.py
+++ b/tests/generator/test_config.py
@@ -9,9 +9,6 @@ from pystencilssfg.config import (
 )
 
 
-DATA_DIR = path.join(path.split(__file__)[0], "data")
-
-
 def test_defaults():
     cfg = SfgConfig()
 
@@ -57,7 +54,7 @@ def test_override():
     assert cfg1.clang_format.binary == "bogus"
 
 
-def test_from_commandline():
+def test_from_commandline(sample_config_module):
     from argparse import ArgumentParser
 
     parser = ArgumentParser()
@@ -74,9 +71,8 @@ def test_from_commandline():
     assert cfg.extensions.header == "h++"
     assert cfg.extensions.impl == "c++"
 
-    config_module = path.join(DATA_DIR, "project_config.py")
     args = parser.parse_args(
-        ["--sfg-output-dir", "gen_sources", "--sfg-config-module", config_module]
+        ["--sfg-output-dir", "gen_sources", "--sfg-config-module", sample_config_module]
     )
     cli_args = CommandLineParameters(args)
     cfg = cli_args.get_config()
@@ -95,16 +91,15 @@ def test_from_commandline():
     assert cli_args.configuration_module.magic_number == 0xCAFE
 
 
-def test_generator_config(monkeypatch, tmp_path):
+def test_generator_config(monkeypatch, tmp_path, sample_config_module):
     import sys
 
-    config_module = path.join(DATA_DIR, "project_config.py")
     args = [
         "genscript.py",
         "--sfg-file-extensions",
         ".c++,.h++",
         "--sfg-config-module",
-        config_module,
+        sample_config_module,
         "test1",
         "test2",
     ]
diff --git a/tests/generator_scripts/scripts/Variables.py b/tests/generator_scripts/scripts/Variables.py
index 9fd4e00..bcc85e4 100644
--- a/tests/generator_scripts/scripts/Variables.py
+++ b/tests/generator_scripts/scripts/Variables.py
@@ -1,7 +1,6 @@
-import sympy as sp
 from pystencils import TypedSymbol, fields, kernel
 
-from pystencilssfg import SourceFileGenerator, SfgConfiguration
+from pystencilssfg import SourceFileGenerator
 
 with SourceFileGenerator() as sfg:
     α = TypedSymbol("alpha", "float32")
diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py
new file mode 100644
index 0000000..485c43e
--- /dev/null
+++ b/tests/integration/test_cli.py
@@ -0,0 +1,68 @@
+import subprocess
+
+
+def test_list_files():
+    output_dir = "/my/output/directory"
+    args = [
+        "sfg-cli",
+        "list-files",
+        "--sfg-output-dir",
+        output_dir,
+        "--sfg-file-extensions",
+        "cu, cuh",
+        "genscript.py",
+    ]
+
+    result = subprocess.run(args, capture_output=True, text=True)
+
+    assert result.returncode == 0
+    assert (
+        result.stdout
+        == "/my/output/directory/genscript.cuh /my/output/directory/genscript.cu\n"
+    )
+
+
+def test_list_files_headeronly():
+    output_dir = "/my/output/directory"
+    args = [
+        "sfg-cli",
+        "list-files",
+        "--sfg-output-dir",
+        output_dir,
+        "--sfg-output-mode",
+        "header-only",
+        "genscript.py",
+    ]
+
+    result = subprocess.run(args, capture_output=True, text=True)
+
+    assert result.returncode == 0
+    assert result.stdout == "/my/output/directory/genscript.hpp\n"
+
+
+def test_list_files_with_config_module(sample_config_module):
+    args = [
+        "sfg-cli",
+        "list-files",
+        "--sfg-config-module",
+        sample_config_module,
+        "genscript.py",
+    ]
+
+    result = subprocess.run(args, capture_output=True, text=True)
+
+    assert result.returncode == 0
+    assert (
+        result.stdout
+        == "generated_sources/genscript.hpp generated_sources/genscript.cpp\n"
+    )
+
+
+def test_make_find_module(tmp_path):
+    args = ["sfg-cli", "cmake", "make-find-module"]
+
+    result = subprocess.run(args, cwd=str(tmp_path))
+    assert result.returncode == 0
+
+    expected_path = tmp_path / "FindPystencilsSfg.cmake"
+    assert expected_path.exists()
-- 
GitLab