summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/load.py
blob: 1aa220b570eed87b3c8183e27ee56e9d306bbdf9 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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 os
import re

import pytest

import spack.spec
import spack.user_environment as uenv
from spack.main import SpackCommand

load = SpackCommand("load")
unload = SpackCommand("unload")
install = SpackCommand("install")
location = SpackCommand("location")

pytestmark = pytest.mark.not_on_windows("does not run on windows")


def test_manpath_trailing_colon(
    install_mockery, mock_fetch, mock_archive, mock_packages, working_env
):
    """Test that the commands generated by load add the MANPATH prefix
    inspections. Also test that Spack correctly preserves the default/existing
    manpath search path via a trailing colon"""
    install("mpileaks")

    sh_out = load("--sh", "--only", "package", "mpileaks")
    lines = sh_out.split("\n")
    assert any(re.match(r"export MANPATH=.*:;", ln) for ln in lines)

    os.environ["MANPATH"] = "/tmp/man:"

    sh_out = load("--sh", "--only", "package", "mpileaks")
    lines = sh_out.split("\n")
    assert any(re.match(r"export MANPATH=.*:/tmp/man:;", ln) for ln in lines)


def test_load(install_mockery, mock_fetch, mock_archive, mock_packages):
    """Test that the commands generated by load add the specified prefix
    inspections. Also test that Spack records loaded specs by hash in the
    user environment.

    CMAKE_PREFIX_PATH is the only prefix inspection guaranteed for fake
    packages, since it keys on the prefix instead of a subdir."""
    install_out = install("mpileaks", output=str, fail_on_error=False)
    print("spack install mpileaks")
    print(install_out)
    mpileaks_spec = spack.spec.Spec("mpileaks").concretized()

    sh_out = load("--sh", "--only", "package", "mpileaks")
    csh_out = load("--csh", "--only", "package", "mpileaks")

    # Test prefix inspections
    sh_out_test = "export CMAKE_PREFIX_PATH=%s" % mpileaks_spec.prefix
    csh_out_test = "setenv CMAKE_PREFIX_PATH %s" % mpileaks_spec.prefix
    assert sh_out_test in sh_out
    assert csh_out_test in csh_out

    # Test hashes recorded properly
    hash_test_replacements = (uenv.spack_loaded_hashes_var, mpileaks_spec.dag_hash())
    sh_hash_test = "export %s=%s" % hash_test_replacements
    csh_hash_test = "setenv %s %s" % hash_test_replacements
    assert sh_hash_test in sh_out
    assert csh_hash_test in csh_out


def test_load_recursive(install_mockery, mock_fetch, mock_archive, mock_packages):
    """Test that the '-r' option to the load command prepends dependency prefix
    inspections in post-order"""
    install("mpileaks")
    mpileaks_spec = spack.spec.Spec("mpileaks").concretized()

    sh_out = load("--sh", "mpileaks")
    csh_out = load("--csh", "mpileaks")

    # Test prefix inspections
    prefix_test_replacement = ":".join(
        reversed([s.prefix for s in mpileaks_spec.traverse(order="post")])
    )

    sh_prefix_test = "export CMAKE_PREFIX_PATH=%s" % prefix_test_replacement
    csh_prefix_test = "setenv CMAKE_PREFIX_PATH %s" % prefix_test_replacement
    assert sh_prefix_test in sh_out
    assert csh_prefix_test in csh_out

    # Test spack records loaded hashes properly
    hash_test_replacement = (
        uenv.spack_loaded_hashes_var,
        ":".join(reversed([s.dag_hash() for s in mpileaks_spec.traverse(order="post")])),
    )
    sh_hash_test = "export %s=%s" % hash_test_replacement
    csh_hash_test = "setenv %s %s" % hash_test_replacement
    assert sh_hash_test in sh_out
    assert csh_hash_test in csh_out


def test_load_includes_run_env(install_mockery, mock_fetch, mock_archive, mock_packages):
    """Tests that environment changes from the package's
    `setup_run_environment` method are added to the user environment in
    addition to the prefix inspections"""
    install("mpileaks")

    sh_out = load("--sh", "mpileaks")
    csh_out = load("--csh", "mpileaks")

    assert "export FOOBAR=mpileaks" in sh_out
    assert "setenv FOOBAR mpileaks" in csh_out


def test_load_first(install_mockery, mock_fetch, mock_archive, mock_packages):
    """Test with and without the --first option"""
    install("libelf@0.8.12")
    install("libelf@0.8.13")

    # Now there are two versions of libelf, which should cause an error
    out = load("--sh", "libelf", fail_on_error=False)
    assert "matches multiple packages" in out
    assert "Use a more specific spec" in out

    # Using --first should avoid the error condition
    load("--sh", "--first", "libelf")


def test_load_fails_no_shell(install_mockery, mock_fetch, mock_archive, mock_packages):
    """Test that spack load prints an error message without a shell."""
    install("mpileaks")

    out = load("mpileaks", fail_on_error=False)
    assert "To set up shell support" in out


def test_unload(install_mockery, mock_fetch, mock_archive, mock_packages, working_env):
    """Tests that any variables set in the user environment are undone by the
    unload command"""
    install("mpileaks")
    mpileaks_spec = spack.spec.Spec("mpileaks").concretized()

    # Set so unload has something to do
    os.environ["FOOBAR"] = "mpileaks"
    os.environ[uenv.spack_loaded_hashes_var] = "%s:%s" % (mpileaks_spec.dag_hash(), "garbage")

    sh_out = unload("--sh", "mpileaks")
    csh_out = unload("--csh", "mpileaks")

    assert "unset FOOBAR" in sh_out
    assert "unsetenv FOOBAR" in csh_out

    assert "export %s=garbage" % uenv.spack_loaded_hashes_var in sh_out
    assert "setenv %s garbage" % uenv.spack_loaded_hashes_var in csh_out


def test_unload_fails_no_shell(
    install_mockery, mock_fetch, mock_archive, mock_packages, working_env
):
    """Test that spack unload prints an error message without a shell."""
    install("mpileaks")
    mpileaks_spec = spack.spec.Spec("mpileaks").concretized()
    os.environ[uenv.spack_loaded_hashes_var] = mpileaks_spec.dag_hash()

    out = unload("mpileaks", fail_on_error=False)
    assert "To set up shell support" in out