Skip to content

Commit fa0ab92

Browse files
committed
wip: improve testing, delegate to schema, remove postcheck in favour of a separate setter for modulepath
1 parent 4dddadf commit fa0ab92

File tree

3 files changed

+65
-52
lines changed

3 files changed

+65
-52
lines changed

stackinator/recipe.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ def __init__(self, args):
6969
if modules_path.is_file():
7070
with modules_path.open() as fid:
7171
self.modules = yaml.load(fid, Loader=yaml.Loader)
72-
schema.ModulesValidator(pathlib.Path(self.mount)).validate(self.modules)
72+
schema.ModulesValidator.validate(self.modules)
73+
74+
# Note:
75+
# modules root should match MODULEPATH set by envvars and used by uenv view "modules"
76+
# so we enforce that the user does not override it in modules.yaml
77+
self.modules["modules"].setdefault("default", {}).setdefault("roots", {}).setdefault(
78+
"tcl", (self.mount / "modules").as_posix()
79+
)
7380

7481
# DEPRECATED field `config:modules`
7582
if "modules" in self.config:

stackinator/schema.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ def __init__(self, name: str, errors: list[jsonschema.ValidationError]):
6464

6565

6666
class SchemaValidator:
67-
def __init__(self, schema_filepath: pathlib.Path, precheck=None, postcheck=None, postcheck_args=()):
67+
def __init__(self, schema_filepath: pathlib.Path, precheck=None):
6868
self._validator = validator(json.load(open(schema_filepath)))
6969
self._precheck = precheck
70-
self._postcheck = postcheck
71-
self._postcheck_args = postcheck_args
7270

7371
def validate(self, instance: dict):
7472
if self._precheck:
@@ -79,9 +77,6 @@ def validate(self, instance: dict):
7977
if len(errors) != 0:
8078
raise ValidationError(self._validator.schema.get("title", "no-title"), errors)
8179

82-
if self._postcheck:
83-
self._postcheck(instance, *self._postcheck_args)
84-
8580

8681
def check_config_version(instance):
8782
rversion = instance.get("version", 1)
@@ -117,18 +112,4 @@ def check_config_version(instance):
117112
CompilersValidator = SchemaValidator(prefix / "schema/compilers.json")
118113
EnvironmentsValidator = SchemaValidator(prefix / "schema/environments.json")
119114
CacheValidator = SchemaValidator(prefix / "schema/cache.json")
120-
121-
122-
def modules_constraints(instance: dict, mount: pathlib.Path):
123-
# Note:
124-
# modules root should match MODULEPATH set by envvars and used by uenv view "modules"
125-
# so we enforce that the user does not override it in modules.yaml
126-
instance["modules"].setdefault("default", {}).setdefault("roots", {}).setdefault(
127-
"tcl", (mount / "modules").as_posix()
128-
)
129-
130-
131-
def ModulesValidator(mountpoint):
132-
return SchemaValidator(
133-
prefix / "schema/modules.json", None, postcheck=modules_constraints, postcheck_args=(mountpoint,)
134-
)
115+
ModulesValidator = SchemaValidator(prefix / "schema/modules.json")

unittests/test_schema.py

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -201,49 +201,74 @@ def test_recipe_environments_yaml(recipe_paths):
201201
schema.EnvironmentsValidator.validate(raw)
202202

203203

204-
def test_modules_yaml():
205-
instance = yaml.load(
204+
@pytest.mark.parametrize(
205+
"recipe",
206+
[
206207
dedent(
207208
"""
208209
modules: {}
209210
"""
210211
),
211-
Loader=yaml.Loader,
212-
)
213-
schema.ModulesValidator(pathlib.Path("/my/mount/point")).validate(
214-
instance,
215-
)
216-
217-
assert instance["modules"]["default"]["roots"]["tcl"] == "/my/mount/point/modules"
218-
assert not instance["modules"]["default"]["arch_folder"]
219-
220-
instance = yaml.load(
221212
dedent(
222213
"""
223214
modules:
224215
default:
225216
arch_folder: false
226217
"""
227218
),
228-
Loader=yaml.Loader,
229-
)
230-
schema.ModulesValidator(pathlib.Path("/my/mount/point")).validate(
231-
instance,
232-
)
219+
dedent(
220+
"""
221+
modules:
222+
# Paths tomodules: check when creating modules for all module sets
223+
prefix_insmodules:pections:
224+
bin:
225+
- PATH
226+
lib:
227+
- LD_LIBRARY_PATH
228+
lib64:
229+
- LD_LIBRARY_PATH
233230
234-
assert instance["modules"]["default"]["roots"]["tcl"] == "/my/mount/point/modules"
231+
default:
232+
arch_folder: false
233+
# Where to install modules
234+
tcl:
235+
all:
236+
autoload: none
237+
hash_length: 0
238+
exclude_implicits: true
239+
exclude: []
240+
projections:
241+
all: '{name}/{version}'
242+
"""
243+
),
244+
],
245+
)
246+
def test_valid_modules_yaml(recipe):
247+
instance = yaml.load(recipe, Loader=yaml.Loader)
248+
schema.ModulesValidator.validate(instance)
235249
assert not instance["modules"]["default"]["arch_folder"]
236250

251+
252+
@pytest.mark.parametrize(
253+
"recipe",
254+
[
255+
dedent(
256+
"""
257+
modules:
258+
default:
259+
arch_folder: true
260+
"""
261+
),
262+
dedent(
263+
"""
264+
modules:
265+
default:
266+
roots:
267+
tcl: /user/path/modules
268+
"""
269+
),
270+
],
271+
)
272+
def test_invalid_modules_yaml(recipe):
237273
with pytest.raises(Exception):
238-
schema.ModulesValidator(pathlib.Path("/my/mount/point")).validate(
239-
yaml.load(
240-
dedent(
241-
"""
242-
modules:
243-
default:
244-
arch_folder: true
245-
"""
246-
),
247-
Loader=yaml.Loader,
248-
),
249-
)
274+
schema.ModulesValidator.validate(yaml.load(recipe, Loader=yaml.Loader))

0 commit comments

Comments
 (0)