summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/dev_build.py
blob: 85199eddd66da27b9c18d7583cf24b70354bf85e (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# 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 pytest

import llnl.util.filesystem as fs

import spack.build_environment
import spack.environment as ev
import spack.error
import spack.spec
import spack.store
from spack.main import SpackCommand

dev_build = SpackCommand("dev-build")
install = SpackCommand("install")
env = SpackCommand("env")

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


def test_dev_build_basics(tmpdir, install_mockery):
    spec = spack.spec.Spec(f"dev-build-test-install@0.0.0 dev_path={tmpdir}").concretized()

    assert "dev_path" in spec.variants

    with tmpdir.as_cwd():
        with open(spec.package.filename, "w") as f:
            f.write(spec.package.original_string)

        dev_build("dev-build-test-install@0.0.0")

    assert spec.package.filename in os.listdir(spec.prefix)
    with open(os.path.join(spec.prefix, spec.package.filename), "r") as f:
        assert f.read() == spec.package.replacement_string

    assert os.path.exists(str(tmpdir))


def test_dev_build_before(tmpdir, install_mockery):
    spec = spack.spec.Spec(f"dev-build-test-install@0.0.0 dev_path={tmpdir}").concretized()

    with tmpdir.as_cwd():
        with open(spec.package.filename, "w") as f:
            f.write(spec.package.original_string)

        dev_build("-b", "edit", "dev-build-test-install@0.0.0")

        assert spec.package.filename in os.listdir(os.getcwd())
        with open(spec.package.filename, "r") as f:
            assert f.read() == spec.package.original_string

    assert not os.path.exists(spec.prefix)


def test_dev_build_until(tmpdir, install_mockery):
    spec = spack.spec.Spec(f"dev-build-test-install@0.0.0 dev_path={tmpdir}").concretized()

    with tmpdir.as_cwd():
        with open(spec.package.filename, "w") as f:
            f.write(spec.package.original_string)

        dev_build("-u", "edit", "dev-build-test-install@0.0.0")

        assert spec.package.filename in os.listdir(os.getcwd())
        with open(spec.package.filename, "r") as f:
            assert f.read() == spec.package.replacement_string

    assert not os.path.exists(spec.prefix)
    assert not spack.store.STORE.db.query(spec, installed=True)


def test_dev_build_until_last_phase(tmpdir, install_mockery):
    # Test that we ignore the last_phase argument if it is already last
    spec = spack.spec.Spec(f"dev-build-test-install@0.0.0 dev_path={tmpdir}").concretized()

    with tmpdir.as_cwd():
        with open(spec.package.filename, "w") as f:
            f.write(spec.package.original_string)

        dev_build("-u", "install", "dev-build-test-install@0.0.0")

        assert spec.package.filename in os.listdir(os.getcwd())
        with open(spec.package.filename, "r") as f:
            assert f.read() == spec.package.replacement_string

    assert os.path.exists(spec.prefix)
    assert spack.store.STORE.db.query(spec, installed=True)
    assert os.path.exists(str(tmpdir))


def test_dev_build_before_until(tmpdir, install_mockery, capsys):
    spec = spack.spec.Spec(f"dev-build-test-install@0.0.0 dev_path={tmpdir}").concretized()

    with tmpdir.as_cwd():
        with open(spec.package.filename, "w") as f:
            f.write(spec.package.original_string)

        with pytest.raises(SystemExit):
            dev_build("-u", "edit", "-b", "edit", "dev-build-test-install@0.0.0")

        bad_phase = "phase_that_does_not_exist"
        not_allowed = "is not a valid phase"
        not_installed = "was not installed"
        out = dev_build("-u", bad_phase, "dev-build-test-install@0.0.0", fail_on_error=False)
        assert bad_phase in out
        assert not_allowed in out
        assert not_installed in out

        out = dev_build("-b", bad_phase, "dev-build-test-install@0.0.0", fail_on_error=False)
        assert bad_phase in out
        assert not_allowed in out
        assert not_installed in out


def print_spack_cc(*args):
    # Eat arguments and print environment variable to test
    print(os.environ.get("CC", ""))


# `module unload cray-libsci` in test environment causes failure
# It does not fail for actual installs
# build_environment.py imports module directly, so we monkeypatch it there
# rather than in module_cmd
def mock_module_noop(*args):
    pass


def test_dev_build_drop_in(tmpdir, mock_packages, monkeypatch, install_mockery, working_env):
    monkeypatch.setattr(os, "execvp", print_spack_cc)
    monkeypatch.setattr(spack.build_environment, "module", mock_module_noop)

    with tmpdir.as_cwd():
        output = dev_build("-b", "edit", "--drop-in", "sh", "dev-build-test-install@0.0.0")
        assert "lib/spack/env" in output


def test_dev_build_fails_already_installed(tmpdir, install_mockery):
    spec = spack.spec.Spec("dev-build-test-install@0.0.0 dev_path=%s" % tmpdir)
    spec.concretize()

    with tmpdir.as_cwd():
        with open(spec.package.filename, "w") as f:
            f.write(spec.package.original_string)

        dev_build("dev-build-test-install@0.0.0")
        output = dev_build("dev-build-test-install@0.0.0", fail_on_error=False)
        assert "Already installed in %s" % spec.prefix in output


def test_dev_build_fails_no_spec():
    output = dev_build(fail_on_error=False)
    assert "requires a package spec argument" in output


def test_dev_build_fails_multiple_specs(mock_packages):
    output = dev_build("libelf", "libdwarf", fail_on_error=False)
    assert "only takes one spec" in output


def test_dev_build_fails_nonexistent_package_name(mock_packages):
    output = ""

    try:
        dev_build("no_such_package")
        assert False, "no exception was raised!"
    except spack.repo.UnknownPackageError as e:
        output = e.message

    assert "Package 'no_such_package' not found" in output


def test_dev_build_fails_no_version(mock_packages):
    output = dev_build("dev-build-test-install", fail_on_error=False)
    assert "dev-build spec must have a single, concrete version" in output


def test_dev_build_env(tmpdir, install_mockery, mutable_mock_env_path):
    """Test Spack does dev builds for packages in develop section of env."""
    # setup dev-build-test-install package for dev build
    build_dir = tmpdir.mkdir("build")
    spec = spack.spec.Spec("dev-build-test-install@0.0.0 dev_path=%s" % build_dir)
    spec.concretize()

    with build_dir.as_cwd():
        with open(spec.package.filename, "w") as f:
            f.write(spec.package.original_string)

    # setup environment
    envdir = tmpdir.mkdir("env")
    with envdir.as_cwd():
        with open("spack.yaml", "w") as f:
            f.write(
                f"""\
spack:
  specs:
  - dev-build-test-install@0.0.0

  develop:
    dev-build-test-install:
      spec: dev-build-test-install@0.0.0
      path: {os.path.relpath(str(build_dir), start=str(envdir))}
"""
            )
        env("create", "test", "./spack.yaml")
        with ev.read("test"):
            install()

    assert spec.package.filename in os.listdir(spec.prefix)
    with open(os.path.join(spec.prefix, spec.package.filename), "r") as f:
        assert f.read() == spec.package.replacement_string


def test_dev_build_env_version_mismatch(tmpdir, install_mockery, mutable_mock_env_path):
    """Test Spack constraints concretization by develop specs."""
    # setup dev-build-test-install package for dev build
    build_dir = tmpdir.mkdir("build")
    spec = spack.spec.Spec("dev-build-test-install@0.0.0 dev_path=%s" % tmpdir)
    spec.concretize()

    with build_dir.as_cwd():
        with open(spec.package.filename, "w") as f:
            f.write(spec.package.original_string)

    # setup environment
    envdir = tmpdir.mkdir("env")
    with envdir.as_cwd():
        with open("spack.yaml", "w") as f:
            f.write(
                f"""\
spack:
  specs:
  - dev-build-test-install@0.0.0

  develop:
    dev-build-test-install:
      spec: dev-build-test-install@1.1.1
      path: {build_dir}
"""
            )

        env("create", "test", "./spack.yaml")
        with ev.read("test"):
            with pytest.raises((RuntimeError, spack.error.UnsatisfiableSpecError)):
                install()


def test_dev_build_multiple(tmpdir, install_mockery, mutable_mock_env_path, mock_fetch):
    """Test spack install with multiple developer builds

    Test that only the root needs to be specified in the environment
    Test that versions known only from the dev specs are included in the solve,
    even if they come from a non-root
    """
    # setup dev-build-test-install package for dev build
    # Wait to concretize inside the environment to set dev_path on the specs;
    # without the environment, the user would need to set dev_path for both the
    # root and dependency if they wanted a dev build for both.
    leaf_dir = tmpdir.mkdir("leaf")
    leaf_spec = spack.spec.Spec("dev-build-test-install@=1.0.0")  # non-existing version
    leaf_pkg_cls = spack.repo.PATH.get_pkg_class(leaf_spec.name)
    with leaf_dir.as_cwd():
        with open(leaf_pkg_cls.filename, "w") as f:
            f.write(leaf_pkg_cls.original_string)

    # setup dev-build-test-dependent package for dev build
    # don't concretize outside environment -- dev info will be wrong
    root_dir = tmpdir.mkdir("root")
    root_spec = spack.spec.Spec("dev-build-test-dependent@0.0.0")
    root_pkg_cls = spack.repo.PATH.get_pkg_class(root_spec.name)
    with root_dir.as_cwd():
        with open(root_pkg_cls.filename, "w") as f:
            f.write(root_pkg_cls.original_string)

    # setup environment
    envdir = tmpdir.mkdir("env")
    with envdir.as_cwd():
        with open("spack.yaml", "w") as f:
            f.write(
                f"""\
spack:
  specs:
  - dev-build-test-dependent@0.0.0

  develop:
    dev-build-test-install:
      path: {leaf_dir}
      spec: dev-build-test-install@=1.0.0
    dev-build-test-dependent:
      spec: dev-build-test-dependent@0.0.0
      path: {root_dir}
"""
            )

        env("create", "test", "./spack.yaml")
        with ev.read("test"):
            # Do concretization inside environment for dev info
            # These specs are the source of truth to compare against the installs
            leaf_spec.concretize()
            root_spec.concretize()

            # Do install
            install()

    for spec in (leaf_spec, root_spec):
        assert spec.package.filename in os.listdir(spec.prefix)
        with open(os.path.join(spec.prefix, spec.package.filename), "r") as f:
            assert f.read() == spec.package.replacement_string


def test_dev_build_env_dependency(tmpdir, install_mockery, mock_fetch, mutable_mock_env_path):
    """
    Test non-root specs in an environment are properly marked for dev builds.
    """
    # setup dev-build-test-install package for dev build
    build_dir = tmpdir.mkdir("build")
    spec = spack.spec.Spec("dependent-of-dev-build@0.0.0")
    dep_spec = spack.spec.Spec("dev-build-test-install")

    with build_dir.as_cwd():
        dep_pkg_cls = spack.repo.PATH.get_pkg_class(dep_spec.name)
        with open(dep_pkg_cls.filename, "w") as f:
            f.write(dep_pkg_cls.original_string)

    # setup environment
    envdir = tmpdir.mkdir("env")
    with envdir.as_cwd():
        with open("spack.yaml", "w") as f:
            f.write(
                f"""\
spack:
  specs:
  - dependent-of-dev-build@0.0.0

  develop:
    dev-build-test-install:
      spec: dev-build-test-install@0.0.0
      path: {os.path.relpath(str(build_dir), start=str(envdir))}
"""
            )
        env("create", "test", "./spack.yaml")
        with ev.read("test"):
            # concretize in the environment to get the dev build info
            # equivalent to setting dev_build and dev_path variants
            # on all specs above
            spec.concretize()
            dep_spec.concretize()
            install()

    # Ensure that both specs installed properly
    assert dep_spec.package.filename in os.listdir(dep_spec.prefix)
    assert os.path.exists(spec.prefix)

    # Ensure variants set properly; ensure build_dir is absolute and normalized
    for dep in (dep_spec, spec["dev-build-test-install"]):
        assert dep.satisfies("dev_path=%s" % build_dir)
    assert spec.satisfies("^dev_path=*")


@pytest.mark.parametrize("test_spec", ["dev-build-test-install", "dependent-of-dev-build"])
def test_dev_build_rebuild_on_source_changes(
    test_spec, tmpdir, install_mockery, mutable_mock_env_path, mock_fetch
):
    """Test dev builds rebuild on changes to source code.

    ``test_spec = dev-build-test-install`` tests rebuild for changes to package
    ``test_spec = dependent-of-dev-build`` tests rebuild for changes to dep
    """
    # setup dev-build-test-install package for dev build
    build_dir = tmpdir.mkdir("build")
    spec = spack.spec.Spec("dev-build-test-install@0.0.0 dev_path=%s" % build_dir)
    spec.concretize()

    def reset_string():
        with build_dir.as_cwd():
            with open(spec.package.filename, "w") as f:
                f.write(spec.package.original_string)

    reset_string()

    # setup environment
    envdir = tmpdir.mkdir("env")
    with envdir.as_cwd():
        with open("spack.yaml", "w") as f:
            f.write(
                f"""\
spack:
  specs:
  - {test_spec}@0.0.0

  develop:
    dev-build-test-install:
      spec: dev-build-test-install@0.0.0
      path: {build_dir}
"""
            )

        env("create", "test", "./spack.yaml")
        with ev.read("test"):
            install()

            reset_string()  # so the package will accept rebuilds

            fs.touch(os.path.join(str(build_dir), "test"))
            output = install()

    assert f"Installing {test_spec}" in output