summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/modules/conftest.py
blob: 12ee5c1fcd9443f898337abc5e6131308c00d1e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import pathlib

import pytest

import spack.config
import spack.modules.common
import spack.paths
import spack.spec
import spack.util.path


@pytest.fixture()
def modulefile_content(request):
    """Returns a function that generates the content of a module file as a list of lines."""
    writer_cls = getattr(request.module, "writer_cls")

    def _impl(spec_str, module_set_name="default", explicit=True):
        spec = spack.spec.Spec(spec_str).concretized()
        generator = writer_cls(spec, module_set_name, explicit)
        generator.write(overwrite=True)
        written_module = pathlib.Path(generator.layout.filename)
        content = written_module.read_text().splitlines()
        generator.remove()
        return content

    return _impl


@pytest.fixture()
def factory(request, mock_modules_root):
    """Given a spec string, returns an instance of the writer and the corresponding spec."""
    writer_cls = getattr(request.module, "writer_cls")

    def _mock(spec_string, module_set_name="default", explicit=True):
        spec = spack.spec.Spec(spec_string).concretized()
        return writer_cls(spec, module_set_name, explicit), spec

    return _mock


@pytest.fixture()
def mock_module_filename(monkeypatch, tmp_path):
    filename = tmp_path / "module"
    # Set for both module types so we can test both
    monkeypatch.setattr(spack.modules.lmod.LmodFileLayout, "filename", str(filename))
    monkeypatch.setattr(spack.modules.tcl.TclFileLayout, "filename", str(filename))
    yield str(filename)