Debian Codegraph @

tests/test_config_file.py

raw
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 sys
11 from pathlib import Path
12
13 import pyfakefs # noqa registers a fixture called "fs" with pytest
14 import pytest
15
16 import findlibs
17
18 extension = findlibs.EXTENSIONS.get(sys.platform, ".so")
19 testlib_path = f"/usr/lib/libtest{extension}"
20 config_paths = [
21 str(Path(p).expanduser())
22 for p in ["~/.config/findlibs/findlibs.conf", "~/.findlibs"]
23 ]
24
25
26 def test_no_config_file(fs):
27 "Check that findlibs works with no config file"
28 fs.create_file(testlib_path)
29 assert findlibs.find("test") == testlib_path
30
31
32 def test_both_config_files(fs):
33 "Check that it throws an error if two config files are present"
34 for p in config_paths:
35 fs.create_file(p)
36 with pytest.raises(ValueError):
37 findlibs._get_paths_from_config()
38
39
40 def test_relative_path(fs):
41 "Check that it throws an error on relative paths"
42 fs.create_file(
43 config_paths[0],
44 contents="""
45 [Paths]
46 relative/path
47 """,
48 )
49
50 with pytest.raises(ValueError):
51 findlibs._get_paths_from_config()
52
53
54 def test_file(fs):
55 "Check that it throws an error when files are included"
56 fs.create_file(
57 config_paths[0],
58 contents="""
59 [Paths]
60 /path/to/file.so
61 """,
62 )
63
64 with pytest.raises(ValueError):
65 findlibs._get_paths_from_config()
66
67
68 def test_empty_config_file(fs):
69 "Check that it works with an empty config file"
70 fs.create_file(testlib_path)
71 fs.create_file(config_paths[0])
72 assert findlibs.find("test") == testlib_path
73
74
75 def test_empty_search_paths(fs):
76 "Check that it works with an empty config file"
77 fs.create_file(testlib_path)
78 fs.create_file(
79 config_paths[0],
80 contents="""
81 [Paths]
82 """,
83 )
84 assert findlibs.find("test") == testlib_path
85
86
87 # Parametrised tests over
88 # search_dir: a path you could put into .findlibs.yml
89 # testlib_path: a concrete test path to check we can find tings in search_dir
90 @pytest.mark.parametrize(
91 "search_dir,testlib_path",
92 [
93 ("/test/usr", f"/test/usr/lib/libtest{extension}"), # absolute paths
94 ("/test/usr", f"/test/usr/lib64/libtest{extension}"), # lib64 aswell as lib
95 (
96 "~/.local",
97 Path(f"~/.local/lib/libtest{extension}").expanduser(),
98 ), # ~ expansion
99 (
100 "$HOME/.local",
101 Path(f"~/.local/lib/libtest{extension}").expanduser(),
102 ), # $HOME expansion
103 ],
104 )
105 def test_config(fs, search_dir, testlib_path):
106 fs.create_file(testlib_path)
107 fs.create_file(
108 config_paths[0],
109 contents=f"""
110 [Paths]
111 {search_dir}
112 """,
113 )
114 assert findlibs.find("test") == str(testlib_path)