summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/llnl/util/link_tree.py
blob: af7012692fabf8cfd51e5bf2ab5bbce840754e24 (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
# Copyright 2013-2021 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
from llnl.util.filesystem import working_dir, mkdirp, touchp
from llnl.util.link_tree import LinkTree
from spack.stage import Stage


@pytest.fixture()
def stage():
    """Creates a stage with the directory structure for the tests."""
    s = Stage('link-tree-test')
    s.create()

    with working_dir(s.path):
        touchp('source/1')
        touchp('source/a/b/2')
        touchp('source/a/b/3')
        touchp('source/c/4')
        touchp('source/c/d/5')
        touchp('source/c/d/6')
        touchp('source/c/d/e/7')

    yield s

    s.destroy()


@pytest.fixture()
def link_tree(stage):
    """Return a properly initialized LinkTree instance."""
    source_path = os.path.join(stage.path, 'source')
    return LinkTree(source_path)


def check_file_link(filename, expected_target):
    assert os.path.isfile(filename)
    assert os.path.islink(filename)
    assert (os.path.abspath(os.path.realpath(filename)) ==
            os.path.abspath(expected_target))


def check_dir(filename):
    assert os.path.isdir(filename)


def test_merge_to_new_directory(stage, link_tree):
    with working_dir(stage.path):
        link_tree.merge('dest')

        check_file_link('dest/1',       'source/1')
        check_file_link('dest/a/b/2',   'source/a/b/2')
        check_file_link('dest/a/b/3',   'source/a/b/3')
        check_file_link('dest/c/4',     'source/c/4')
        check_file_link('dest/c/d/5',   'source/c/d/5')
        check_file_link('dest/c/d/6',   'source/c/d/6')
        check_file_link('dest/c/d/e/7', 'source/c/d/e/7')

        assert os.path.isabs(os.readlink('dest/1'))
        assert os.path.isabs(os.readlink('dest/a/b/2'))
        assert os.path.isabs(os.readlink('dest/a/b/3'))
        assert os.path.isabs(os.readlink('dest/c/4'))
        assert os.path.isabs(os.readlink('dest/c/d/5'))
        assert os.path.isabs(os.readlink('dest/c/d/6'))
        assert os.path.isabs(os.readlink('dest/c/d/e/7'))

        link_tree.unmerge('dest')

        assert not os.path.exists('dest')


def test_merge_to_new_directory_relative(stage, link_tree):
    with working_dir(stage.path):
        link_tree.merge('dest', relative=True)

        check_file_link('dest/1',       'source/1')
        check_file_link('dest/a/b/2',   'source/a/b/2')
        check_file_link('dest/a/b/3',   'source/a/b/3')
        check_file_link('dest/c/4',     'source/c/4')
        check_file_link('dest/c/d/5',   'source/c/d/5')
        check_file_link('dest/c/d/6',   'source/c/d/6')
        check_file_link('dest/c/d/e/7', 'source/c/d/e/7')

        assert not os.path.isabs(os.readlink('dest/1'))
        assert not os.path.isabs(os.readlink('dest/a/b/2'))
        assert not os.path.isabs(os.readlink('dest/a/b/3'))
        assert not os.path.isabs(os.readlink('dest/c/4'))
        assert not os.path.isabs(os.readlink('dest/c/d/5'))
        assert not os.path.isabs(os.readlink('dest/c/d/6'))
        assert not os.path.isabs(os.readlink('dest/c/d/e/7'))

        link_tree.unmerge('dest')

        assert not os.path.exists('dest')


def test_merge_to_existing_directory(stage, link_tree):
    with working_dir(stage.path):

        touchp('dest/x')
        touchp('dest/a/b/y')

        link_tree.merge('dest')

        check_file_link('dest/1',       'source/1')
        check_file_link('dest/a/b/2',   'source/a/b/2')
        check_file_link('dest/a/b/3',   'source/a/b/3')
        check_file_link('dest/c/4',     'source/c/4')
        check_file_link('dest/c/d/5',   'source/c/d/5')
        check_file_link('dest/c/d/6',   'source/c/d/6')
        check_file_link('dest/c/d/e/7', 'source/c/d/e/7')

        assert os.path.isfile('dest/x')
        assert os.path.isfile('dest/a/b/y')

        link_tree.unmerge('dest')

        assert os.path.isfile('dest/x')
        assert os.path.isfile('dest/a/b/y')

        assert not os.path.isfile('dest/1')
        assert not os.path.isfile('dest/a/b/2')
        assert not os.path.isfile('dest/a/b/3')
        assert not os.path.isfile('dest/c/4')
        assert not os.path.isfile('dest/c/d/5')
        assert not os.path.isfile('dest/c/d/6')
        assert not os.path.isfile('dest/c/d/e/7')


def test_merge_with_empty_directories(stage, link_tree):
    with working_dir(stage.path):
        mkdirp('dest/f/g')
        mkdirp('dest/a/b/h')

        link_tree.merge('dest')
        link_tree.unmerge('dest')

        assert not os.path.exists('dest/1')
        assert not os.path.exists('dest/a/b/2')
        assert not os.path.exists('dest/a/b/3')
        assert not os.path.exists('dest/c/4')
        assert not os.path.exists('dest/c/d/5')
        assert not os.path.exists('dest/c/d/6')
        assert not os.path.exists('dest/c/d/e/7')

        assert os.path.isdir('dest/a/b/h')
        assert os.path.isdir('dest/f/g')


def test_ignore(stage, link_tree):
    with working_dir(stage.path):
        touchp('source/.spec')
        touchp('dest/.spec')

        link_tree.merge('dest', ignore=lambda x: x == '.spec')
        link_tree.unmerge('dest', ignore=lambda x: x == '.spec')

        assert not os.path.exists('dest/1')
        assert not os.path.exists('dest/a')
        assert not os.path.exists('dest/c')

        assert os.path.isfile('source/.spec')
        assert os.path.isfile('dest/.spec')