summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/view.py
blob: eaf6999868910cbbc9662833ccffc65ba1e8638b (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# 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 os.path

import pytest

import spack.util.spack_yaml as s_yaml
from spack.main import SpackCommand
from spack.spec import Spec

extensions = SpackCommand("extensions")
install = SpackCommand("install")
view = SpackCommand("view")

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


def create_projection_file(tmpdir, projection):
    if "projections" not in projection:
        projection = {"projections": projection}

    projection_file = tmpdir.mkdir("projection").join("projection.yaml")
    projection_file.write(s_yaml.dump(projection))
    return projection_file


@pytest.mark.parametrize("cmd", ["hardlink", "symlink", "hard", "add", "copy", "relocate"])
def test_view_link_type(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery, cmd
):
    install("libdwarf")
    viewpath = str(tmpdir.mkdir("view_{0}".format(cmd)))
    view(cmd, viewpath, "libdwarf")
    package_prefix = os.path.join(viewpath, "libdwarf")
    assert os.path.exists(package_prefix)

    # Check that we use symlinks for and only for the appropriate subcommands
    is_link_cmd = cmd in ("symlink", "add")
    assert os.path.islink(package_prefix) == is_link_cmd


@pytest.mark.parametrize("add_cmd", ["hardlink", "symlink", "hard", "add", "copy", "relocate"])
def test_view_link_type_remove(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery, add_cmd
):
    install("needs-relocation")
    viewpath = str(tmpdir.mkdir("view_{0}".format(add_cmd)))
    view(add_cmd, viewpath, "needs-relocation")
    bindir = os.path.join(viewpath, "bin")
    assert os.path.exists(bindir)

    view("remove", viewpath, "needs-relocation")
    assert not os.path.exists(bindir)


@pytest.mark.parametrize("cmd", ["hardlink", "symlink", "hard", "add", "copy", "relocate"])
def test_view_projections(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery, cmd
):
    install("libdwarf@20130207")

    viewpath = str(tmpdir.mkdir("view_{0}".format(cmd)))
    view_projection = {"projections": {"all": "{name}-{version}"}}
    projection_file = create_projection_file(tmpdir, view_projection)
    view(cmd, viewpath, "--projection-file={0}".format(projection_file), "libdwarf")

    package_prefix = os.path.join(viewpath, "libdwarf-20130207/libdwarf")
    assert os.path.exists(package_prefix)

    # Check that we use symlinks for and only for the appropriate subcommands
    is_symlink_cmd = cmd in ("symlink", "add")
    assert os.path.islink(package_prefix) == is_symlink_cmd


def test_view_multiple_projections(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery
):
    install("libdwarf@20130207")
    install("extendee@1.0%gcc")

    viewpath = str(tmpdir.mkdir("view"))
    view_projection = s_yaml.syaml_dict(
        [("extendee", "{name}-{compiler.name}"), ("all", "{name}-{version}")]
    )

    projection_file = create_projection_file(tmpdir, view_projection)
    view("add", viewpath, "--projection-file={0}".format(projection_file), "libdwarf", "extendee")

    libdwarf_prefix = os.path.join(viewpath, "libdwarf-20130207/libdwarf")
    extendee_prefix = os.path.join(viewpath, "extendee-gcc/bin")
    assert os.path.exists(libdwarf_prefix)
    assert os.path.exists(extendee_prefix)


def test_view_multiple_projections_all_first(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery
):
    install("libdwarf@20130207")
    install("extendee@1.0%gcc")

    viewpath = str(tmpdir.mkdir("view"))
    view_projection = s_yaml.syaml_dict(
        [("all", "{name}-{version}"), ("extendee", "{name}-{compiler.name}")]
    )

    projection_file = create_projection_file(tmpdir, view_projection)
    view("add", viewpath, "--projection-file={0}".format(projection_file), "libdwarf", "extendee")

    libdwarf_prefix = os.path.join(viewpath, "libdwarf-20130207/libdwarf")
    extendee_prefix = os.path.join(viewpath, "extendee-gcc/bin")
    assert os.path.exists(libdwarf_prefix)
    assert os.path.exists(extendee_prefix)


def test_view_external(tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery):
    install("externaltool")
    viewpath = str(tmpdir.mkdir("view"))
    output = view("symlink", viewpath, "externaltool")
    assert "Skipping external package: externaltool" in output


def test_view_extension(tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery):
    install("extendee")
    install("extension1@1.0")
    install("extension1@2.0")
    install("extension2@1.0")
    viewpath = str(tmpdir.mkdir("view"))
    view("symlink", viewpath, "extension1@1.0")
    all_installed = extensions("--show", "installed", "extendee")
    assert "extension1@1.0" in all_installed
    assert "extension1@2.0" in all_installed
    assert "extension2@1.0" in all_installed
    assert os.path.exists(os.path.join(viewpath, "bin", "extension1"))


def test_view_extension_remove(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery
):
    install("extendee")
    install("extension1@1.0")
    viewpath = str(tmpdir.mkdir("view"))
    view("symlink", viewpath, "extension1@1.0")
    view("remove", viewpath, "extension1@1.0")
    all_installed = extensions("--show", "installed", "extendee")
    assert "extension1@1.0" in all_installed
    assert not os.path.exists(os.path.join(viewpath, "bin", "extension1"))


def test_view_extension_conflict(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery
):
    install("extendee")
    install("extension1@1.0")
    install("extension1@2.0")
    viewpath = str(tmpdir.mkdir("view"))
    view("symlink", viewpath, "extension1@1.0")
    output = view("symlink", viewpath, "extension1@2.0")
    assert "Package conflict detected" in output


def test_view_extension_conflict_ignored(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery
):
    install("extendee")
    install("extension1@1.0")
    install("extension1@2.0")
    viewpath = str(tmpdir.mkdir("view"))
    view("symlink", viewpath, "extension1@1.0")
    view("symlink", viewpath, "-i", "extension1@2.0")
    with open(os.path.join(viewpath, "bin", "extension1"), "r") as fin:
        assert fin.read() == "1.0"


def test_view_fails_with_missing_projections_file(tmpdir):
    viewpath = str(tmpdir.mkdir("view"))
    projection_file = os.path.join(str(tmpdir), "nonexistent")
    with pytest.raises(SystemExit):
        view("symlink", "--projection-file", projection_file, viewpath, "foo")


@pytest.mark.parametrize("with_projection", [False, True])
@pytest.mark.parametrize("cmd", ["symlink", "copy"])
def test_view_files_not_ignored(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery, cmd, with_projection
):
    spec = Spec("view-not-ignored").concretized()
    pkg = spec.package
    pkg.do_install()
    pkg.assert_installed(spec.prefix)

    install("view-dir-file")  # Arbitrary package to add noise

    viewpath = str(tmpdir.mkdir("view_{0}".format(cmd)))

    if with_projection:
        proj = str(tmpdir.join("proj.yaml"))
        with open(proj, "w") as f:
            f.write('{"projections":{"all":"{name}"}}')
        prefix_in_view = os.path.join(viewpath, "view-not-ignored")
        args = ["--projection-file", proj]
    else:
        prefix_in_view = viewpath
        args = []

    view(cmd, *(args + [viewpath, "view-not-ignored", "view-dir-file"]))
    pkg.assert_installed(prefix_in_view)

    view("remove", viewpath, "view-not-ignored")
    pkg.assert_not_installed(prefix_in_view)