| 1 |
#!/usr/bin/env python3 |
| 2 |
# (C) Copyright 2020 ECMWF. |
| 3 |
# |
| 4 |
# This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
# In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
# granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
# nor does it submit to any jurisdiction. |
| 9 |
|
| 10 |
import os |
| 11 |
import sys |
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
import pyfakefs # noqa registers a fixture called "fs" with pytest |
| 15 |
import pytest |
| 16 |
|
| 17 |
import findlibs |
| 18 |
|
| 19 |
pkg_name = "foobar" |
| 20 |
extension = findlibs.EXTENSIONS.get(sys.platform, ".so") |
| 21 |
libname = f"lib{pkg_name}{extension}" |
| 22 |
|
| 23 |
conda_prefix = "/test/conda/prefix" |
| 24 |
os.environ["CONDA_PREFIX"] = conda_prefix |
| 25 |
env_variable_location = os.environ[f"{pkg_name}_HOME"] = "/test/environment/variable" |
| 26 |
ld_library_location = os.environ["LD_LIBRARY_PATH"] = "/test/ld_library/" |
| 27 |
|
| 28 |
|
| 29 |
# A list of test locations in order of precedence |
| 30 |
# test_find checks that if a file is placed in any of these locations then it is found |
| 31 |
test_locations = [ |
| 32 |
sys.prefix, |
| 33 |
conda_prefix, |
| 34 |
env_variable_location, |
| 35 |
"/", |
| 36 |
"/usr/", |
| 37 |
"/usr/local/", |
| 38 |
"/opt/", |
| 39 |
"/opt/homebrew/", |
| 40 |
os.path.expanduser("~/.local"), |
| 41 |
] |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.parametrize("location", test_locations) |
| 45 |
def test_find(fs, location): |
| 46 |
libpath = Path(location) / "lib" / libname |
| 47 |
print(f"creating {libpath}") |
| 48 |
fs.create_file(libpath) |
| 49 |
assert findlibs.find(pkg_name) == str(libpath) |