summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/llnl/util/file_list.py
blob: 425ce641bd135d50a7ba3620ee81c6d983faa9ae (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
# 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 fnmatch
import os.path
import sys

import pytest

from llnl.util.filesystem import HeaderList, LibraryList, find, find_headers, find_libraries

import spack.paths


@pytest.fixture()
def library_list():
    """Returns an instance of LibraryList."""
    # Test all valid extensions: ['.a', '.dylib', '.so']
    libs = (
        [
            "/dir1/liblapack.a",
            "/dir2/libpython3.6.dylib",  # name may contain periods
            "/dir1/libblas.a",
            "/dir3/libz.so",
            "libmpi.so.20.10.1",  # shared object libraries may be versioned
        ]
        if sys.platform != "win32"
        else [
            "/dir1/liblapack.lib",
            "/dir2/libpython3.6.dll",
            "/dir1/libblas.lib",
            "/dir3/libz.dll",
            "libmpi.dll.20.10.1",
        ]
    )

    return LibraryList(libs)


@pytest.fixture()
def header_list():
    """Returns an instance of header list"""
    # Test all valid extensions: ['.h', '.hpp', '.hh', '.cuh']
    headers = [
        "/dir1/Python.h",
        "/dir2/date.time.h",
        "/dir1/pyconfig.hpp",
        "/dir3/core.hh",
        "pymem.cuh",
    ]
    h = HeaderList(headers)
    h.add_macro("-DBOOST_LIB_NAME=boost_regex")
    h.add_macro("-DBOOST_DYN_LINK")
    return h


# TODO: Remove below when llnl.util.filesystem.find_libraries becomes spec aware
plat_static_ext = "lib" if sys.platform == "win32" else "a"


plat_shared_ext = "dll" if sys.platform == "win32" else "so"


plat_apple_shared_ext = "dylib"


class TestLibraryList:
    def test_repr(self, library_list):
        x = eval(repr(library_list))
        assert library_list == x

    def test_joined_and_str(self, library_list):
        s1 = library_list.joined()
        expected = " ".join(
            [
                "/dir1/liblapack.%s" % plat_static_ext,
                "/dir2/libpython3.6.%s"
                % (plat_apple_shared_ext if sys.platform != "win32" else "dll"),
                "/dir1/libblas.%s" % plat_static_ext,
                "/dir3/libz.%s" % plat_shared_ext,
                "libmpi.%s.20.10.1" % plat_shared_ext,
            ]
        )
        assert s1 == expected

        s2 = str(library_list)
        assert s1 == s2

        s3 = library_list.joined(";")
        expected = ";".join(
            [
                "/dir1/liblapack.%s" % plat_static_ext,
                "/dir2/libpython3.6.%s"
                % (plat_apple_shared_ext if sys.platform != "win32" else "dll"),
                "/dir1/libblas.%s" % plat_static_ext,
                "/dir3/libz.%s" % plat_shared_ext,
                "libmpi.%s.20.10.1" % plat_shared_ext,
            ]
        )
        assert s3 == expected

    def test_flags(self, library_list):
        search_flags = library_list.search_flags
        assert "-L/dir1" in search_flags
        assert "-L/dir2" in search_flags
        assert "-L/dir3" in search_flags
        assert isinstance(search_flags, str)
        assert search_flags == "-L/dir1 -L/dir2 -L/dir3"

        link_flags = library_list.link_flags
        assert "-llapack" in link_flags
        assert "-lpython3.6" in link_flags
        assert "-lblas" in link_flags
        assert "-lz" in link_flags
        assert "-lmpi" in link_flags
        assert isinstance(link_flags, str)
        assert link_flags == "-llapack -lpython3.6 -lblas -lz -lmpi"

        ld_flags = library_list.ld_flags
        assert isinstance(ld_flags, str)
        assert ld_flags == search_flags + " " + link_flags

    def test_paths_manipulation(self, library_list):
        names = library_list.names
        assert names == ["lapack", "python3.6", "blas", "z", "mpi"]

        directories = library_list.directories
        assert directories == ["/dir1", "/dir2", "/dir3"]

    def test_get_item(self, library_list):
        a = library_list[0]
        assert a == "/dir1/liblapack.%s" % plat_static_ext

        b = library_list[:]
        assert type(b) is type(library_list)
        assert library_list == b
        assert library_list is not b

    def test_add(self, library_list):
        pylist = [
            "/dir1/liblapack.%s" % plat_static_ext,  # removed from the final list
            "/dir2/libmpi.%s" % plat_shared_ext,
            "/dir4/libnew.%s" % plat_static_ext,
        ]
        another = LibraryList(pylist)
        both = library_list + another
        assert len(both) == 7

        # Invariant
        assert both == both + both

        # Always produce an instance of LibraryList
        assert type(library_list + pylist) is type(library_list)
        assert type(pylist + library_list) is type(library_list)


class TestHeaderList:
    def test_repr(self, header_list):
        x = eval(repr(header_list))
        assert header_list == x

    def test_joined_and_str(self, header_list):
        s1 = header_list.joined()
        expected = " ".join(
            [
                "/dir1/Python.h",
                "/dir2/date.time.h",
                "/dir1/pyconfig.hpp",
                "/dir3/core.hh",
                "pymem.cuh",
            ]
        )
        assert s1 == expected

        s2 = str(header_list)
        assert s1 == s2

        s3 = header_list.joined(";")
        expected = ";".join(
            [
                "/dir1/Python.h",
                "/dir2/date.time.h",
                "/dir1/pyconfig.hpp",
                "/dir3/core.hh",
                "pymem.cuh",
            ]
        )
        assert s3 == expected

    def test_flags(self, header_list):
        include_flags = header_list.include_flags
        assert "-I/dir1" in include_flags
        assert "-I/dir2" in include_flags
        assert "-I/dir3" in include_flags
        assert isinstance(include_flags, str)
        assert include_flags == "-I/dir1 -I/dir2 -I/dir3"

        macros = header_list.macro_definitions
        assert "-DBOOST_LIB_NAME=boost_regex" in macros
        assert "-DBOOST_DYN_LINK" in macros
        assert isinstance(macros, str)
        assert macros == "-DBOOST_LIB_NAME=boost_regex -DBOOST_DYN_LINK"

        cpp_flags = header_list.cpp_flags
        assert isinstance(cpp_flags, str)
        assert cpp_flags == include_flags + " " + macros

    def test_paths_manipulation(self, header_list):
        names = header_list.names
        assert names == ["Python", "date.time", "pyconfig", "core", "pymem"]

        directories = header_list.directories
        assert directories == ["/dir1", "/dir2", "/dir3"]

    def test_get_item(self, header_list):
        a = header_list[0]
        assert a == "/dir1/Python.h"

        b = header_list[:]
        assert type(b) is type(header_list)
        assert header_list == b
        assert header_list is not b

    def test_add(self, header_list):
        pylist = [
            "/dir1/Python.h",  # removed from the final list
            "/dir2/pyconfig.hpp",
            "/dir4/date.time.h",
        ]
        another = HeaderList(pylist)
        h = header_list + another
        assert len(h) == 7

        # Invariant : l == l + l
        assert h == h + h

        # Always produce an instance of HeaderList
        assert type(header_list + pylist) is type(header_list)
        assert type(pylist + header_list) is type(header_list)


#: Directory where the data for the test below is stored
search_dir = os.path.join(spack.paths.test_path, "data", "directory_search")


@pytest.mark.parametrize(
    "lib_list,kwargs",
    [
        (["liba"], {"shared": True, "recursive": True}),
        (["liba"], {"shared": False, "recursive": True}),
        (["libc", "liba"], {"shared": True, "recursive": True}),
        (["liba", "libc"], {"shared": False, "recursive": True}),
        (["libc", "libb", "liba"], {"shared": True, "recursive": True}),
        (["liba", "libb", "libc"], {"shared": False, "recursive": True}),
    ],
)
def test_library_type_search(lib_list, kwargs):
    results = find_libraries(lib_list, search_dir, **kwargs)
    assert len(results) != 0
    for result in results:
        lib_type_ext = plat_shared_ext
        if not kwargs["shared"]:
            lib_type_ext = plat_static_ext
        assert result.endswith(lib_type_ext) or (
            kwargs["shared"] and result.endswith(plat_apple_shared_ext)
        )


@pytest.mark.parametrize(
    "search_fn,search_list,root,kwargs",
    [
        (find_libraries, "liba", search_dir, {"recursive": True}),
        (find_libraries, ["liba"], search_dir, {"recursive": True}),
        (find_libraries, "libb", search_dir, {"recursive": True}),
        (find_libraries, ["libc"], search_dir, {"recursive": True}),
        (find_libraries, ["libc", "liba"], search_dir, {"recursive": True}),
        (find_libraries, ["liba", "libc"], search_dir, {"recursive": True}),
        (find_libraries, ["libc", "libb", "liba"], search_dir, {"recursive": True}),
        (find_libraries, ["liba", "libc"], search_dir, {"recursive": True}),
        (
            find_libraries,
            ["libc", "libb", "liba"],
            search_dir,
            {"recursive": True, "shared": False},
        ),
        (find_headers, "a", search_dir, {"recursive": True}),
        (find_headers, ["a"], search_dir, {"recursive": True}),
        (find_headers, "b", search_dir, {"recursive": True}),
        (find_headers, ["c"], search_dir, {"recursive": True}),
        (find_headers, ["c", "a"], search_dir, {"recursive": True}),
        (find_headers, ["a", "c"], search_dir, {"recursive": True}),
        (find_headers, ["c", "b", "a"], search_dir, {"recursive": True}),
        (find_headers, ["a", "c"], search_dir, {"recursive": True}),
        (find_libraries, ["liba", "libd"], os.path.join(search_dir, "b"), {"recursive": False}),
        (find_headers, ["b", "d"], os.path.join(search_dir, "b"), {"recursive": False}),
    ],
)
def test_searching_order(search_fn, search_list, root, kwargs):
    # Test search
    result = search_fn(search_list, root, **kwargs)

    # The tests are set-up so that something is always found
    assert len(result) != 0

    # Now reverse the result and start discarding things
    # as soon as you have matches. In the end the list should
    # be emptied.
    rlist = list(reversed(result))

    # At this point make sure the search list is a sequence
    if isinstance(search_list, str):
        search_list = [search_list]

    # Discard entries in the order they appear in search list
    for x in search_list:
        try:
            while fnmatch.fnmatch(rlist[-1], x) or x in rlist[-1]:
                rlist.pop()
        except IndexError:
            # List is empty
            pass

    # List should be empty here
    assert len(rlist) == 0


@pytest.mark.parametrize(
    "root,search_list,kwargs,expected",
    [
        (
            search_dir,
            "*/*bar.tx?",
            {"recursive": False},
            [
                os.path.join(search_dir, os.path.join("a", "foobar.txt")),
                os.path.join(search_dir, os.path.join("b", "bar.txp")),
                os.path.join(search_dir, os.path.join("c", "bar.txt")),
            ],
        ),
        (
            search_dir,
            "*/*bar.tx?",
            {"recursive": True},
            [
                os.path.join(search_dir, os.path.join("a", "foobar.txt")),
                os.path.join(search_dir, os.path.join("b", "bar.txp")),
                os.path.join(search_dir, os.path.join("c", "bar.txt")),
            ],
        ),
    ],
)
def test_find_with_globbing(root, search_list, kwargs, expected):
    matches = find(root, search_list, **kwargs)
    assert sorted(matches) == sorted(expected)