Skip to content
Snippets Groups Projects
Commit 9744c66e authored by Frederik Hennig's avatar Frederik Hennig
Browse files

move cmdline tests to generator script test suite

parent d67c98ed
No related branches found
No related tags found
1 merge request!2Refactor Configuration System & Extend Documentation
Pipeline #70696 passed
...@@ -3,6 +3,10 @@ testpaths = src/pystencilssfg tests/ ...@@ -3,6 +3,10 @@ testpaths = src/pystencilssfg tests/
python_files = "test_*.py" python_files = "test_*.py"
# Need to ignore the generator scripts, otherwise they would be executed # Need to ignore the generator scripts, otherwise they would be executed
# during test collection # during test collection
addopts = --doctest-modules --ignore=tests/generator_scripts/scripts --ignore=--ignore=tests/data addopts =
--doctest-modules
--ignore=tests/generator_scripts/scripts
--ignore=tests/generator_scripts/config
--ignore=tests/data
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
...@@ -5,6 +5,7 @@ from .config import SfgConfig, CommandLineParameters, OutputMode, GLOBAL_NAMESPA ...@@ -5,6 +5,7 @@ from .config import SfgConfig, CommandLineParameters, OutputMode, GLOBAL_NAMESPA
from .context import SfgContext from .context import SfgContext
from .composer import SfgComposer from .composer import SfgComposer
from .emission import AbstractEmitter, OutputSpec from .emission import AbstractEmitter, OutputSpec
from .exceptions import SfgException
class SourceFileGenerator: class SourceFileGenerator:
...@@ -33,6 +34,13 @@ class SourceFileGenerator: ...@@ -33,6 +34,13 @@ class SourceFileGenerator:
import __main__ import __main__
if not hasattr(__main__, "__file__"):
raise SfgException(
"Invalid execution environment: "
"It seems that you are trying to run the `SourceFileGenerator` in an environment "
"without a valid entry point, such as a REPL or a multiprocessing fork."
)
scriptpath = __main__.__file__ scriptpath = __main__.__file__
scriptname = path.split(scriptpath)[1] scriptname = path.split(scriptpath)[1]
basename = path.splitext(scriptname)[0] basename = path.splitext(scriptname)[0]
......
...@@ -89,32 +89,3 @@ def test_from_commandline(sample_config_module): ...@@ -89,32 +89,3 @@ def test_from_commandline(sample_config_module):
assert cli_args.configuration_module is not None assert cli_args.configuration_module is not None
assert cli_args.configuration_module.magic_string == "Spam and eggs" assert cli_args.configuration_module.magic_string == "Spam and eggs"
assert cli_args.configuration_module.magic_number == 0xCAFE assert cli_args.configuration_module.magic_number == 0xCAFE
def test_generator_config(monkeypatch, tmp_path, sample_config_module):
import sys
args = [
"genscript.py",
"--sfg-file-extensions",
".c++,.h++",
"--sfg-config-module",
sample_config_module,
"test1",
"test2",
]
monkeypatch.setattr(sys, "argv", args)
cfg = SfgConfig(output_directory=str(tmp_path.absolute()))
with SourceFileGenerator(cfg) as sfg:
ctx = sfg.context
assert ctx.outer_namespace == "myproject"
assert ctx.argv == ["test1", "test2"]
assert isinstance(ctx.project_info, dict)
assert ctx.project_info == {
"use_openmp": True,
"use_cuda": True,
"float_format": "float32",
}
from pystencilssfg import SfgConfig
def configure_sfg(cfg: SfgConfig):
cfg.outer_namespace = "myproject"
cfg.codestyle.indent_width = 3
def project_info():
return {
"use_openmp": True,
"use_cuda": True,
"float_format": "float32",
}
from pystencilssfg import SourceFileGenerator
with SourceFileGenerator() as sfg:
ctx = sfg.context
assert ctx.outer_namespace == "myproject"
assert ctx.codestyle.indent_width == 3
assert not ctx.argv
assert isinstance(ctx.project_info, dict)
assert ctx.project_info == {
"use_openmp": True,
"use_cuda": True,
"float_format": "float32",
}
from pystencilssfg import SourceFileGenerator
with SourceFileGenerator(keep_unknown_argv=True) as sfg:
ctx = sfg.context
assert ctx.argv == ["--precision", "float32", "test1", "test2"]
from pystencilssfg import SourceFileGenerator
with SourceFileGenerator() as sfg:
...
...@@ -9,6 +9,7 @@ import subprocess ...@@ -9,6 +9,7 @@ import subprocess
THIS_DIR = path.split(__file__)[0] THIS_DIR = path.split(__file__)[0]
SCRIPTS_DIR = path.join(THIS_DIR, "scripts") SCRIPTS_DIR = path.join(THIS_DIR, "scripts")
CONFIG_DIR = path.join(THIS_DIR, "config")
EXPECTED_DIR = path.join(THIS_DIR, "expected") EXPECTED_DIR = path.join(THIS_DIR, "expected")
...@@ -30,6 +31,12 @@ class ScriptInfo: ...@@ -30,6 +31,12 @@ class ScriptInfo:
Output files will all be placed in the ``out`` folder. Output files will all be placed in the ``out`` folder.
""" """
args: tuple[str, ...] = ()
"""Command-line arguments to be passed to the generator script"""
should_fail: bool = False
"""Whether the exeuction of this script should fail."""
compilable_output: str | None = None compilable_output: str | None = None
"""File extension of the output file that can be compiled. """File extension of the output file that can be compiled.
...@@ -50,6 +57,37 @@ When adding new generator scripts to the `scripts` directory, ...@@ -50,6 +57,37 @@ When adding new generator scripts to the `scripts` directory,
do not forget to include them here. do not forget to include them here.
""" """
SCRIPTS = [ SCRIPTS = [
ScriptInfo.make(
"TestConfigModule",
("h++", "c++"),
args=(
"--sfg-file-extensions",
".c++,.h++",
"--sfg-config-module",
path.join(CONFIG_DIR, "TestConfigModule_cfg.py"),
),
),
ScriptInfo.make(
"TestIllegalArgs",
("h++", "c++"),
args=(
"--sfg-file-extensionss",
".c++,.h++",
),
should_fail=True
),
ScriptInfo.make(
"TestExtraCommandLineArgs",
("h++", "c++"),
args=(
"--sfg-file-extensions",
".c++,.h++",
"--precision",
"float32",
"test1",
"test2"
),
),
ScriptInfo.make("Structural", ("hpp", "cpp")), ScriptInfo.make("Structural", ("hpp", "cpp")),
ScriptInfo.make("SimpleJacobi", ("hpp", "cpp"), compilable_output="cpp"), ScriptInfo.make("SimpleJacobi", ("hpp", "cpp"), compilable_output="cpp"),
ScriptInfo.make("SimpleClasses", ("hpp", "cpp")), ScriptInfo.make("SimpleClasses", ("hpp", "cpp")),
...@@ -75,12 +113,17 @@ def test_generator_script(script_info: ScriptInfo): ...@@ -75,12 +113,17 @@ def test_generator_script(script_info: ScriptInfo):
shutil.rmtree(output_dir) shutil.rmtree(output_dir)
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
args = ["python", script_file, "--sfg-output-dir", output_dir] args = ["python", script_file, "--sfg-output-dir", output_dir] + list(script_info.args)
result = subprocess.run(args) result = subprocess.run(args)
if script_info.should_fail:
if result.returncode == 0:
pytest.fail(f"Generator script {script_name} was supposed to fail, but didn't.")
return
if result.returncode != 0: if result.returncode != 0:
raise AssertionError(f"Generator script {script_name} failed.") pytest.fail(f"Generator script {script_name} failed.")
# Check generated files # Check generated files
expected_files = set( expected_files = set(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment