summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/gc.py
blob: 9defb9d99c68affaf1956f0bebe4fd461f79f17c (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
# 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 pytest

import spack.environment as ev
import spack.main
import spack.spec

gc = spack.main.SpackCommand("gc")
add = spack.main.SpackCommand("add")
install = spack.main.SpackCommand("install")
find = spack.main.SpackCommand("find")

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


@pytest.mark.db
def test_gc_without_build_dependency(config, mutable_database):
    output = gc("-yb")
    assert "There are no unused specs." in output

    output = gc("-y")
    assert "There are no unused specs." in output


@pytest.mark.db
def test_gc_with_build_dependency(config, mutable_database):
    s = spack.spec.Spec("simple-inheritance")
    s.concretize()
    s.package.do_install(fake=True, explicit=True)

    output = gc("-yb")
    assert "There are no unused specs." in output

    output = gc("-y")
    assert "Successfully uninstalled cmake" in output


@pytest.mark.db
def test_gc_with_environment(config, mutable_database, mutable_mock_env_path):
    s = spack.spec.Spec("simple-inheritance")
    s.concretize()
    s.package.do_install(fake=True, explicit=True)

    e = ev.create("test_gc")
    with e:
        add("cmake")
        install()
        assert "cmake" in find()
        output = gc("-y")
    assert "Restricting garbage collection" in output
    assert "There are no unused specs" in output


@pytest.mark.db
def test_gc_with_build_dependency_in_environment(config, mutable_database, mutable_mock_env_path):
    s = spack.spec.Spec("simple-inheritance")
    s.concretize()
    s.package.do_install(fake=True, explicit=True)

    e = ev.create("test_gc")
    with e:
        add("simple-inheritance")
        install()
        assert "simple-inheritance" in find()
        output = gc("-yb")
    assert "Restricting garbage collection" in output
    assert "There are no unused specs" in output

    with e:
        assert "simple-inheritance" in find()
        output = gc("-y")
    assert "Restricting garbage collection" in output
    assert "Successfully uninstalled cmake" in output


@pytest.mark.db
def test_gc_except_any_environments(config, mutable_database, mutable_mock_env_path):
    s = spack.spec.Spec("simple-inheritance")
    s.concretize()
    s.package.do_install(fake=True, explicit=True)

    assert "zmpi" in find()

    e = ev.create("test_gc")
    with e:
        add("simple-inheritance")
        install()
        assert "simple-inheritance" in find()

    output = gc("-yE")
    assert "Restricting garbage collection" not in output
    assert "Successfully uninstalled zmpi" in output
    assert "zmpi" not in find()

    with e:
        output = gc("-yE")
    assert "Restricting garbage collection" not in output
    assert "There are no unused specs" not in find()


@pytest.mark.db
def test_gc_except_specific_environments(config, mutable_database, mutable_mock_env_path):
    s = spack.spec.Spec("simple-inheritance")
    s.concretize()
    s.package.do_install(fake=True, explicit=True)

    assert "zmpi" in find()

    e = ev.create("test_gc")
    with e:
        add("simple-inheritance")
        install()
        assert "simple-inheritance" in find()

    output = gc("-ye", "test_gc")
    assert "Restricting garbage collection" not in output
    assert "Successfully uninstalled zmpi" in output
    assert "zmpi" not in find()


@pytest.mark.db
def test_gc_except_nonexisting_dir_env(config, mutable_database, mutable_mock_env_path, tmpdir):
    output = gc("-ye", tmpdir.strpath, fail_on_error=False)
    assert "No such environment" in output
    gc.returncode == 1


@pytest.mark.db
def test_gc_except_specific_dir_env(config, mutable_database, mutable_mock_env_path, tmpdir):
    s = spack.spec.Spec("simple-inheritance")
    s.concretize()
    s.package.do_install(fake=True, explicit=True)

    assert "zmpi" in find()

    e = ev.create_in_dir(tmpdir.strpath)
    with e:
        add("simple-inheritance")
        install()
        assert "simple-inheritance" in find()

    output = gc("-ye", tmpdir.strpath)
    assert "Restricting garbage collection" not in output
    assert "Successfully uninstalled zmpi" in output
    assert "zmpi" not in find()