| 1 |
import unittest |
| 2 |
import unittest.mock |
| 3 |
import sys |
| 4 |
import os |
| 5 |
|
| 6 |
|
| 7 |
def reset_import_and_get_fresh_config(): |
| 8 |
# Reloads the clang.cindex module to reset any class-level state in Config. |
| 9 |
sys.modules.pop("clang.cindex", None) |
| 10 |
sys.modules.pop("clang", None) |
| 11 |
from clang.cindex import Config |
| 12 |
|
| 13 |
return Config() |
| 14 |
|
| 15 |
|
| 16 |
class TestEnvironementVariable(unittest.TestCase): |
| 17 |
def test_working_libclang_library_file(self): |
| 18 |
ref_libclang_library_file = reset_import_and_get_fresh_config().get_filename() |
| 19 |
with unittest.mock.patch.dict( |
| 20 |
os.environ, {"LIBCLANG_LIBRARY_FILE": ref_libclang_library_file} |
| 21 |
): |
| 22 |
reset_import_and_get_fresh_config().lib |
| 23 |
|
| 24 |
@unittest.mock.patch.dict("os.environ", {"LIBCLANG_LIBRARY_FILE": "/dev/null"}) |
| 25 |
def test_non_working_libclang_library_file(self): |
| 26 |
config = reset_import_and_get_fresh_config() |
| 27 |
import clang.cindex |
| 28 |
|
| 29 |
with self.assertRaises(clang.cindex.LibclangError): |
| 30 |
config.lib |
| 31 |
|
| 32 |
def test_working_libclang_library_path(self): |
| 33 |
# Get adequate libclang path |
| 34 |
ref_libclang_library_file = reset_import_and_get_fresh_config().get_filename() |
| 35 |
ref_libclang_library_path, filename = os.path.split(ref_libclang_library_file) |
| 36 |
filename_root, filename_ext = os.path.splitext(filename) |
| 37 |
|
| 38 |
# Config only recognizes the default libclang filename. |
| 39 |
# If LIBCLANG_LIBRARY_FILE points to a non-standard name, skip this test. |
| 40 |
|
| 41 |
if not ( |
| 42 |
filename_root == "libclang" and filename_ext in (".so", ".dll", ".dylib") |
| 43 |
): |
| 44 |
self.skipTest(f"Skipping because {filename} is not a default libclang name") |
| 45 |
|
| 46 |
with unittest.mock.patch.dict( |
| 47 |
os.environ, {"LIBCLANG_LIBRARY_PATH": ref_libclang_library_path} |
| 48 |
): |
| 49 |
# Remove LIBCLANG_LIBRARY_FILE to avoid it taking precedence if set by the user |
| 50 |
# Need to be in the mocked environement |
| 51 |
os.environ.pop("LIBCLANG_LIBRARY_FILE", None) |
| 52 |
reset_import_and_get_fresh_config().lib |
| 53 |
|
| 54 |
@unittest.mock.patch.dict("os.environ", {"LIBCLANG_LIBRARY_PATH": "not_a_real_dir"}) |
| 55 |
def test_non_working_libclang_library_path(self): |
| 56 |
# Remove LIBCLANG_LIBRARY_FILE to avoid it taking precedence if set by the user |
| 57 |
os.environ.pop("LIBCLANG_LIBRARY_FILE", None) |
| 58 |
|
| 59 |
config = reset_import_and_get_fresh_config() |
| 60 |
import clang.cindex |
| 61 |
|
| 62 |
with self.assertRaises(clang.cindex.LibclangError): |
| 63 |
config.lib |