summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/package_class.py
blob: 279693a529b81bacc0c5b1ab528b4663921e865b (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# 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)

"""Test class methods on Package objects.

This doesn't include methods on package *instances* (like do_install(),
etc.).  Only methods like ``possible_dependencies()`` that deal with the
static DSL metadata for packages.
"""

import os
import shutil

import pytest

import llnl.util.filesystem as fs

import spack.deptypes as dt
import spack.install_test
import spack.package_base
import spack.repo
from spack.build_systems.generic import Package
from spack.installer import InstallError


@pytest.fixture(scope="module")
def mpi_names(mock_repo_path):
    return [spec.name for spec in mock_repo_path.providers_for("mpi")]


@pytest.fixture()
def mpileaks_possible_deps(mock_packages, mpi_names):
    possible = {
        "callpath": set(["dyninst"] + mpi_names),
        "low-priority-provider": set(),
        "dyninst": set(["libdwarf", "libelf"]),
        "fake": set(),
        "intel-parallel-studio": set(),
        "libdwarf": set(["libelf"]),
        "libelf": set(),
        "mpich": set(),
        "mpich2": set(),
        "mpileaks": set(["callpath"] + mpi_names),
        "multi-provider-mpi": set(),
        "zmpi": set(["fake"]),
    }
    return possible


def test_possible_dependencies(mock_packages, mpileaks_possible_deps):
    pkg_cls = spack.repo.PATH.get_pkg_class("mpileaks")
    expanded_possible_deps = pkg_cls.possible_dependencies(expand_virtuals=True)
    assert mpileaks_possible_deps == expanded_possible_deps
    assert {
        "callpath": {"dyninst", "mpi"},
        "dyninst": {"libdwarf", "libelf"},
        "libdwarf": {"libelf"},
        "libelf": set(),
        "mpi": set(),
        "mpileaks": {"callpath", "mpi"},
    } == pkg_cls.possible_dependencies(expand_virtuals=False)


def test_possible_direct_dependencies(mock_packages, mpileaks_possible_deps):
    pkg_cls = spack.repo.PATH.get_pkg_class("mpileaks")
    deps = pkg_cls.possible_dependencies(transitive=False, expand_virtuals=False)
    assert {"callpath": set(), "mpi": set(), "mpileaks": {"callpath", "mpi"}} == deps


def test_possible_dependencies_virtual(mock_packages, mpi_names):
    expected = dict(
        (name, set(spack.repo.PATH.get_pkg_class(name).dependencies)) for name in mpi_names
    )

    # only one mock MPI has a dependency
    expected["fake"] = set()

    assert expected == spack.package_base.possible_dependencies("mpi", transitive=False)


def test_possible_dependencies_missing(mock_packages):
    pkg_cls = spack.repo.PATH.get_pkg_class("missing-dependency")
    missing = {}
    pkg_cls.possible_dependencies(transitive=True, missing=missing)
    assert {"this-is-a-missing-dependency"} == missing["missing-dependency"]


def test_possible_dependencies_with_deptypes(mock_packages):
    dtbuild1 = spack.repo.PATH.get_pkg_class("dtbuild1")

    assert {
        "dtbuild1": {"dtrun2", "dtlink2"},
        "dtlink2": set(),
        "dtrun2": set(),
    } == dtbuild1.possible_dependencies(depflag=dt.LINK | dt.RUN)

    assert {
        "dtbuild1": {"dtbuild2", "dtlink2"},
        "dtbuild2": set(),
        "dtlink2": set(),
    } == dtbuild1.possible_dependencies(depflag=dt.BUILD)

    assert {"dtbuild1": {"dtlink2"}, "dtlink2": set()} == dtbuild1.possible_dependencies(
        depflag=dt.LINK
    )


def test_possible_dependencies_with_multiple_classes(mock_packages, mpileaks_possible_deps):
    pkgs = ["dt-diamond", "mpileaks"]
    expected = mpileaks_possible_deps.copy()
    expected.update(
        {
            "dt-diamond": set(["dt-diamond-left", "dt-diamond-right"]),
            "dt-diamond-left": set(["dt-diamond-bottom"]),
            "dt-diamond-right": set(["dt-diamond-bottom"]),
            "dt-diamond-bottom": set(),
        }
    )

    assert expected == spack.package_base.possible_dependencies(*pkgs)


def setup_install_test(source_paths, test_root):
    """
    Set up the install test by creating sources and install test roots.

    The convention used here is to create an empty file if the path name
    ends with an extension otherwise, a directory is created.
    """
    fs.mkdirp(test_root)
    for path in source_paths:
        if os.path.splitext(path)[1]:
            fs.touchp(path)
        else:
            fs.mkdirp(path)


@pytest.mark.parametrize(
    "spec,sources,extras,expect",
    [
        (
            "a",
            ["example/a.c"],  # Source(s)
            ["example/a.c"],  # Extra test source
            ["example/a.c"],
        ),  # Test install dir source(s)
        (
            "b",
            ["test/b.cpp", "test/b.hpp", "example/b.txt"],  # Source(s)
            ["test"],  # Extra test source
            ["test/b.cpp", "test/b.hpp"],
        ),  # Test install dir source
        (
            "c",
            ["examples/a.py", "examples/b.py", "examples/c.py", "tests/d.py"],
            ["examples/b.py", "tests"],
            ["examples/b.py", "tests/d.py"],
        ),
    ],
)
def test_cache_extra_sources(install_mockery, spec, sources, extras, expect):
    """Test the package's cache extra test sources helper function."""
    s = spack.spec.Spec(spec).concretized()
    s.package.spec.concretize()

    source_path = s.package.stage.source_path
    srcs = [fs.join_path(source_path, src) for src in sources]
    test_root = spack.install_test.install_test_root(s.package)
    setup_install_test(srcs, test_root)

    emsg_dir = "Expected {0} to be a directory"
    emsg_file = "Expected {0} to be a file"
    for src in srcs:
        assert os.path.exists(src), "Expected {0} to exist".format(src)
        if os.path.splitext(src)[1]:
            assert os.path.isfile(src), emsg_file.format(src)
        else:
            assert os.path.isdir(src), emsg_dir.format(src)

    spack.install_test.cache_extra_test_sources(s.package, extras)

    src_dests = [fs.join_path(test_root, src) for src in sources]
    exp_dests = [fs.join_path(test_root, e) for e in expect]
    poss_dests = set(src_dests) | set(exp_dests)

    msg = "Expected {0} to{1} exist"
    for pd in poss_dests:
        if pd in exp_dests:
            assert os.path.exists(pd), msg.format(pd, "")
            if os.path.splitext(pd)[1]:
                assert os.path.isfile(pd), emsg_file.format(pd)
            else:
                assert os.path.isdir(pd), emsg_dir.format(pd)
        else:
            assert not os.path.exists(pd), msg.format(pd, " not")

    # Perform a little cleanup
    shutil.rmtree(os.path.dirname(source_path))


def test_cache_extra_sources_fails(install_mockery):
    s = spack.spec.Spec("a").concretized()
    s.package.spec.concretize()

    with pytest.raises(InstallError) as exc_info:
        spack.install_test.cache_extra_test_sources(s.package, ["/a/b", "no-such-file"])

    errors = str(exc_info.value)
    assert "'/a/b') must be relative" in errors
    assert "'no-such-file') for the copy does not exist" in errors


def test_package_exes_and_libs():
    with pytest.raises(spack.error.SpackError, match="defines both"):

        class BadDetectablePackage(spack.package.Package):
            executables = ["findme"]
            libraries = ["libFindMe.a"]


def test_package_url_and_urls():
    class URLsPackage(spack.package.Package):
        url = "https://www.example.com/url-package-1.0.tgz"
        urls = ["https://www.example.com/archive"]

    s = spack.spec.Spec("a")
    with pytest.raises(ValueError, match="defines both"):
        URLsPackage(s)


def test_package_license():
    class LicensedPackage(spack.package.Package):
        extendees = None  # currently a required attribute for is_extension()
        license_files = None

    s = spack.spec.Spec("a")
    pkg = LicensedPackage(s)
    assert pkg.global_license_file is None

    pkg.license_files = ["license.txt"]
    assert os.path.basename(pkg.global_license_file) == pkg.license_files[0]


class BaseTestPackage(Package):
    extendees = None  # currently a required attribute for is_extension()


def test_package_version_fails():
    s = spack.spec.Spec("a")
    pkg = BaseTestPackage(s)
    with pytest.raises(ValueError, match="does not have a concrete version"):
        pkg.version()


def test_package_tester_fails():
    s = spack.spec.Spec("a")
    pkg = BaseTestPackage(s)
    with pytest.raises(ValueError, match="without concrete version"):
        pkg.tester()


def test_package_fetcher_fails():
    s = spack.spec.Spec("a")
    pkg = BaseTestPackage(s)
    with pytest.raises(ValueError, match="without concrete version"):
        pkg.fetcher


def test_package_no_extendees():
    s = spack.spec.Spec("a")
    pkg = BaseTestPackage(s)
    assert pkg.extendee_args is None


def test_package_test_no_compilers(mock_packages, monkeypatch, capfd):
    def compilers(compiler, arch_spec):
        return None

    monkeypatch.setattr(spack.compilers, "compilers_for_spec", compilers)

    s = spack.spec.Spec("a")
    pkg = BaseTestPackage(s)
    pkg.test_requires_compiler = True
    pkg.do_test()
    error = capfd.readouterr()[1]
    assert "Skipping tests for package" in error
    assert "test requires missing compiler" in error


# TODO (post-34236): Remove when remove deprecated run_test(), etc.
@pytest.mark.parametrize(
    "msg,installed,purpose,expected",
    [
        ("do-nothing", False, "test: echo", "do-nothing"),
        ("not installed", True, "test: echo not installed", "expected in prefix"),
    ],
)
def test_package_run_test_install(
    install_mockery_mutable_config, mock_fetch, capfd, msg, installed, purpose, expected
):
    """Confirm expected outputs from run_test for installed/not installed exe."""
    s = spack.spec.Spec("trivial-smoke-test").concretized()
    pkg = s.package

    pkg.run_test(
        "echo", msg, expected=[expected], installed=installed, purpose=purpose, work_dir="."
    )
    output = capfd.readouterr()[0]
    assert expected in output


# TODO (post-34236): Remove when remove deprecated run_test(), etc.
@pytest.mark.parametrize(
    "skip,failures,status",
    [
        (True, 0, str(spack.install_test.TestStatus.SKIPPED)),
        (False, 1, str(spack.install_test.TestStatus.FAILED)),
    ],
)
def test_package_run_test_missing(
    install_mockery_mutable_config, mock_fetch, capfd, skip, failures, status
):
    """Confirm expected results from run_test for missing exe when skip or not."""
    s = spack.spec.Spec("trivial-smoke-test").concretized()
    pkg = s.package

    pkg.run_test("no-possible-program", skip_missing=skip)
    output = capfd.readouterr()[0]
    assert len(pkg.tester.test_failures) == failures
    assert status in output


# TODO (post-34236): Remove when remove deprecated run_test(), etc.
def test_package_run_test_fail_fast(install_mockery_mutable_config, mock_fetch):
    """Confirm expected exception when run_test with fail_fast enabled."""
    s = spack.spec.Spec("trivial-smoke-test").concretized()
    pkg = s.package

    with spack.config.override("config:fail_fast", True):
        with pytest.raises(spack.install_test.TestFailure, match="Failed to find executable"):
            pkg.run_test("no-possible-program")