summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/tengine.py
blob: aa7fdac9d2c36765b7d2b44fce7204337aa1ab61 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2013-2024 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 pytest

import spack.config
import spack.tengine as tengine
from spack.util.path import canonicalize_path


class TestContext:
    class A(tengine.Context):
        @tengine.context_property
        def foo(self):
            return 1

    class B(tengine.Context):
        @tengine.context_property
        def bar(self):
            return 2

    class C(A, B):
        @tengine.context_property
        def foobar(self):
            return 3

        @tengine.context_property
        def foo(self):
            return 10

    def test_to_dict(self):
        """Tests that all the context properties in a hierarchy are considered
        when building the context dictionary.
        """

        # A derives directly from Context
        a = TestContext.A()
        d = a.to_dict()

        assert len(d) == 1
        assert "foo" in d
        assert d["foo"] == 1

        # So does B
        b = TestContext.B()
        d = b.to_dict()

        assert len(d) == 1
        assert "bar" in d
        assert d["bar"] == 2

        # C derives from both and overrides 'foo'
        c = TestContext.C()
        d = c.to_dict()

        assert len(d) == 3
        for x in ("foo", "bar", "foobar"):
            assert x in d

        assert d["foo"] == 10
        assert d["bar"] == 2
        assert d["foobar"] == 3


@pytest.mark.usefixtures("config")
class TestTengineEnvironment:
    def test_template_retrieval(self):
        """Tests the template retrieval mechanism hooked into config files"""
        # Check the directories are correct
        template_dirs = spack.config.get("config:template_dirs")
        template_dirs = tuple([canonicalize_path(x) for x in template_dirs])
        assert len(template_dirs) == 3

        env = tengine.make_environment(template_dirs)

        # Retrieve a.txt, which resides in the second
        # template directory specified in the mock configuration
        template = env.get_template("a.txt")
        text = template.render({"word": "world"})
        assert "Hello world!" == text

        # Retrieve b.txt, which resides in the third
        # template directory specified in the mock configuration
        template = env.get_template("b.txt")
        text = template.render({"word": "world"})
        assert "Howdy world!" == text