Coverage for tests/test_putils.py: 76.72%
94 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-14 17:01 +1100
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-14 17:01 +1100
1import os
2import sys
4import pytest
6from refcount.putils import augment_path_env, build_new_path_env, find_full_path, library_short_filename
8pkg_dir = os.path.join(os.path.dirname(__file__), "..")
9sys.path.insert(0, pkg_dir)
10if sys.platform == "win32": 10 ↛ 11line 10 didn't jump to line 11 because the condition on line 10 was never true
11 dir_path = os.path.join(pkg_dir, "tests/test_native_library/x64/Debug")
12elif sys.platform == "linux" or sys.platform == "darwin": 12 ↛ 15line 12 didn't jump to line 15 because the condition on line 12 was always true
13 dir_path = os.path.join(pkg_dir, "tests/test_native_library/build")
14else:
15 raise RuntimeError(f"Platform {sys.platform} is not yet supported")
18def test_library_short_filename_platform() -> None:
19 fname = library_short_filename("test_native_library")
20 if sys.platform == "win32": 20 ↛ 21line 20 didn't jump to line 21 because the condition on line 20 was never true
21 assert fname == "test_native_library.dll"
22 elif sys.platform == "linux": 22 ↛ 24line 22 didn't jump to line 24 because the condition on line 22 was always true
23 assert fname == "libtest_native_library.so"
24 elif sys.platform == "darwin":
25 assert fname == "libtest_native_library.dylib"
26 else:
27 raise RuntimeError(f"Platform {sys.platform} is not yet supported")
29 with pytest.raises(ValueError):
30 _ = library_short_filename(None)
33def test_find_full_path():
34 assert find_full_path(None) is None
35 if sys.platform == "win32": 35 ↛ 36line 35 didn't jump to line 36 because the condition on line 35 was never true
36 assert find_full_path("kernel32").endswith("kernel32.dll")
37 elif sys.platform == "linux": 37 ↛ 44line 37 didn't jump to line 44 because the condition on line 37 was always true
38 assert find_full_path("c") == "libc.so.6"
39 assert find_full_path("abcdefabcdefabcdef") is None
40 # below was trying to activate one of the branches in the code to increase coverage
41 # worked on my machine, but not on the CI (ubuntu-latest) returning 'libffi.so.8'
42 # I think libffilso is what is under cffi, and would be installed in the conda/venv environment. TBC.
43 # assert find_full_path('ffi').endswith('lib/libffi.so')
44 elif sys.platform == "darwin":
45 # /usr/lib/libSystem.dylib ?
46 assert find_full_path("System").endswith("libSystem.dylib")
47 assert find_full_path("abcdefabcdefabcdef") is None
48 else:
49 raise RuntimeError(f"Platform {sys.platform} is not yet supported")
52def test_library_short_filename():
53 assert library_short_filename("Blah", "linux") == "libBlah.so"
54 assert library_short_filename("Blah", "win32") == "Blah.dll"
55 assert library_short_filename("Blah", "darwin") == "libBlah.dylib"
56 with pytest.raises(NotImplementedError):
57 _ = library_short_filename("Blah", "unsupported_platform")
59 with pytest.raises(ValueError):
60 _ = library_short_filename(None, "linux")
63def test_prepend_path_env():
64 # user_dir = os.path.expanduser("~")
65 import tempfile
66 from pathlib import Path
68 with tempfile.TemporaryDirectory() as tmpdirname:
69 tmp = Path(tmpdirname)
70 subfolder = "64" # a hack to be sure the "sub"folder does indeed exist...
71 p_1, p_2 = (str(tmp / "path"), str(tmp / "nonexisting" / "path"))
72 valid_subfolder = tmp / "path" / subfolder
73 valid_subfolder.mkdir(parents=True, exist_ok=True)
74 new_path = augment_path_env(added_paths=p_1, subfolder=subfolder, to_env="PATH", prepend=True)
75 assert new_path.startswith(os.path.join(p_1, subfolder))
76 # and if both, only the one that worked is added
77 p = [p_1, p_2]
78 new_path = augment_path_env(added_paths=p, subfolder=subfolder, to_env="PATH", prepend=True)
79 assert new_path.startswith(os.path.join(p_1, subfolder))
80 assert new_path.find(os.path.join("nonexisting", "path")) == -1
82 # UT coverage Cover the case if there is no prior environment variable
83 new_path = augment_path_env(added_paths=p, subfolder=subfolder, to_env="ENV_NOT_ALREADY", prepend=False)
84 assert new_path.startswith(os.path.join(p_1, subfolder))
85 assert new_path.find(os.path.join("nonexisting", "path")) == -1
87 # UT coverage Cover the case if there is no prior environment variable,
88 # and there is no subfolder
89 new_path = augment_path_env(added_paths=p, subfolder="", to_env="ENV_NOT_ALREADY", prepend=False)
90 assert new_path.startswith(p_1)
91 assert new_path.find(os.path.join("nonexisting", "path")) == -1
94def test_win_architecture():
95 # for the sake of UT coverage:
96 from refcount.putils import _win_architecture
98 warch = _win_architecture()
99 if sys.platform == "win32": 99 ↛ 100line 99 didn't jump to line 100 because the condition on line 99 was never true
100 assert warch == "64" or warch == "32"
101 else:
102 assert warch == ""
105def test_build_new_path_env():
106 import tempfile
107 from pathlib import Path
109 with tempfile.TemporaryDirectory() as tmpdirname:
110 tmp = Path(tmpdirname)
111 subfolder = "64" # a hack to be sure the "sub"folder does indeed exist...
112 p_1 = str(tmp / "path")
113 valid_subfolder = tmp / "path" / subfolder
114 valid_subfolder.mkdir(parents=True, exist_ok=True)
115 os.environ["TEST_PATH_ENV"] = p_1
116 # s = build_new_path_env(from_env='TEST_PATH_ENV', to_env='PATH', platform='win32')
117 # expected = str(tmp / "path" / subfolder)
118 # assert s.endswith(expected)
119 s = build_new_path_env(from_env="TEST_PATH_ENV", to_env="PATH", platform=sys.platform)
120 if sys.platform == "win32": 120 ↛ 121line 120 didn't jump to line 121 because the condition on line 120 was never true
121 expected = str(tmp / "path" / subfolder)
122 else:
123 expected = str(tmp / "path")
124 assert s.endswith(expected)
126 ## UT coverage: if the from_env is not in os.environ
127 # While hypothetical, we certainly want the target environment variable to be returned to avoid messing up the environment the PATH env var for instance.
128 s = build_new_path_env(from_env="INVALID_TEST_PATH_ENV", to_env="PATH", platform=sys.platform)
129 assert s == os.environ["PATH"]
131 # However if the target environment variable is not in os.environ, we return an empty string; nothing to mess up.
132 s = build_new_path_env(from_env="INVALID_TEST_PATH_ENV", to_env="INVALID_PATH", platform=sys.platform)
133 assert s == ""
136def test_new_path_env_warning_msg():
137 # unit test for issue #16
138 path = os.environ.get("PATH", None)
139 assert path is not None
140 new_path = build_new_path_env(from_env="UNLIKELY_TEST_PATH_ENV", to_env="PATH", platform=sys.platform)
141 assert new_path == path
144if __name__ == "__main__": 144 ↛ 145line 144 didn't jump to line 145 because the condition on line 144 was never true
145 test_build_new_path_env()