summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/deprecate.py
blob: 6669be57fe05afc0cd55ccfd2316dd577300adf7 (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
# 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.store
from spack.database import InstallStatuses
from spack.main import SpackCommand

install = SpackCommand("install")
uninstall = SpackCommand("uninstall")
deprecate = SpackCommand("deprecate")
find = SpackCommand("find")

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


def test_deprecate(mock_packages, mock_archive, mock_fetch, install_mockery):
    install("libelf@0.8.13")
    install("libelf@0.8.10")

    all_installed = spack.store.STORE.db.query()
    assert len(all_installed) == 2

    deprecate("-y", "libelf@0.8.10", "libelf@0.8.13")

    non_deprecated = spack.store.STORE.db.query()
    all_available = spack.store.STORE.db.query(installed=any)
    assert all_available == all_installed
    assert non_deprecated == spack.store.STORE.db.query("libelf@0.8.13")


def test_deprecate_fails_no_such_package(mock_packages, mock_archive, mock_fetch, install_mockery):
    """Tests that deprecating a spec that is not installed fails.

    Tests that deprecating without the ``-i`` option in favor of a spec that
    is not installed fails."""
    output = deprecate("-y", "libelf@0.8.10", "libelf@0.8.13", fail_on_error=False)
    assert "Spec 'libelf@0.8.10' matches no installed packages" in output

    install("libelf@0.8.10")

    output = deprecate("-y", "libelf@0.8.10", "libelf@0.8.13", fail_on_error=False)
    assert "Spec 'libelf@0.8.13' matches no installed packages" in output


def test_deprecate_install(mock_packages, mock_archive, mock_fetch, install_mockery):
    """Tests that the ```-i`` option allows us to deprecate in favor of a spec
    that is not yet installed."""
    install("libelf@0.8.10")

    to_deprecate = spack.store.STORE.db.query()
    assert len(to_deprecate) == 1

    deprecate("-y", "-i", "libelf@0.8.10", "libelf@0.8.13")

    non_deprecated = spack.store.STORE.db.query()
    deprecated = spack.store.STORE.db.query(installed=InstallStatuses.DEPRECATED)
    assert deprecated == to_deprecate
    assert len(non_deprecated) == 1
    assert non_deprecated[0].satisfies("libelf@0.8.13")


def test_deprecate_deps(mock_packages, mock_archive, mock_fetch, install_mockery):
    """Test that the deprecate command deprecates all dependencies properly."""
    install("libdwarf@20130729 ^libelf@0.8.13")
    install("libdwarf@20130207 ^libelf@0.8.10")

    new_spec = spack.spec.Spec("libdwarf@20130729^libelf@0.8.13").concretized()
    old_spec = spack.spec.Spec("libdwarf@20130207^libelf@0.8.10").concretized()

    all_installed = spack.store.STORE.db.query()

    deprecate("-y", "-d", "libdwarf@20130207", "libdwarf@20130729")

    non_deprecated = spack.store.STORE.db.query()
    all_available = spack.store.STORE.db.query(installed=any)
    deprecated = spack.store.STORE.db.query(installed=InstallStatuses.DEPRECATED)

    assert all_available == all_installed
    assert sorted(all_available) == sorted(deprecated + non_deprecated)

    assert sorted(non_deprecated) == sorted(list(new_spec.traverse()))
    assert sorted(deprecated) == sorted(list(old_spec.traverse()))


def test_uninstall_deprecated(mock_packages, mock_archive, mock_fetch, install_mockery):
    """Tests that we can still uninstall deprecated packages."""
    install("libelf@0.8.13")
    install("libelf@0.8.10")

    deprecate("-y", "libelf@0.8.10", "libelf@0.8.13")

    non_deprecated = spack.store.STORE.db.query()

    uninstall("-y", "libelf@0.8.10")

    assert spack.store.STORE.db.query() == spack.store.STORE.db.query(installed=any)
    assert spack.store.STORE.db.query() == non_deprecated


def test_deprecate_already_deprecated(mock_packages, mock_archive, mock_fetch, install_mockery):
    """Tests that we can re-deprecate a spec to change its deprecator."""
    install("libelf@0.8.13")
    install("libelf@0.8.12")
    install("libelf@0.8.10")

    deprecated_spec = spack.spec.Spec("libelf@0.8.10").concretized()

    deprecate("-y", "libelf@0.8.10", "libelf@0.8.12")

    deprecator = spack.store.STORE.db.deprecator(deprecated_spec)
    assert deprecator == spack.spec.Spec("libelf@0.8.12").concretized()

    deprecate("-y", "libelf@0.8.10", "libelf@0.8.13")

    non_deprecated = spack.store.STORE.db.query()
    all_available = spack.store.STORE.db.query(installed=any)
    assert len(non_deprecated) == 2
    assert len(all_available) == 3

    deprecator = spack.store.STORE.db.deprecator(deprecated_spec)
    assert deprecator == spack.spec.Spec("libelf@0.8.13").concretized()


def test_deprecate_deprecator(mock_packages, mock_archive, mock_fetch, install_mockery):
    """Tests that when a deprecator spec is deprecated, its deprecatee specs
    are updated to point to the new deprecator."""
    install("libelf@0.8.13")
    install("libelf@0.8.12")
    install("libelf@0.8.10")

    first_deprecated_spec = spack.spec.Spec("libelf@0.8.10").concretized()
    second_deprecated_spec = spack.spec.Spec("libelf@0.8.12").concretized()
    final_deprecator = spack.spec.Spec("libelf@0.8.13").concretized()

    deprecate("-y", "libelf@0.8.10", "libelf@0.8.12")

    deprecator = spack.store.STORE.db.deprecator(first_deprecated_spec)
    assert deprecator == second_deprecated_spec

    deprecate("-y", "libelf@0.8.12", "libelf@0.8.13")

    non_deprecated = spack.store.STORE.db.query()
    all_available = spack.store.STORE.db.query(installed=any)
    assert len(non_deprecated) == 1
    assert len(all_available) == 3

    first_deprecator = spack.store.STORE.db.deprecator(first_deprecated_spec)
    assert first_deprecator == final_deprecator
    second_deprecator = spack.store.STORE.db.deprecator(second_deprecated_spec)
    assert second_deprecator == final_deprecator


def test_concretize_deprecated(mock_packages, mock_archive, mock_fetch, install_mockery):
    """Tests that the concretizer throws an error if we concretize to a
    deprecated spec"""
    install("libelf@0.8.13")
    install("libelf@0.8.10")

    deprecate("-y", "libelf@0.8.10", "libelf@0.8.13")

    spec = spack.spec.Spec("libelf@0.8.10")
    with pytest.raises(spack.spec.SpecDeprecatedError):
        spec.concretize()