22import unittest .mock
33import sys
44import os
5- from clang .cindex import Config
5+
6+
7+ def reset_import_and_get_frech_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 ()
614
715
816class TestEnvironementVariable (unittest .TestCase ):
917 def test_working_libclang_library_file (self ):
10- ref_libclang_library_file = Config ().get_filename ()
18+ ref_libclang_library_file = reset_import_and_get_frech_config ().get_filename ()
1119 with unittest .mock .patch .dict (
1220 os .environ , {"LIBCLANG_LIBRARY_FILE" : ref_libclang_library_file }
1321 ):
14- Config ().lib
22+ reset_import_and_get_frech_config ().lib
1523
1624 @unittest .mock .patch .dict ("os.environ" , {"LIBCLANG_LIBRARY_FILE" : "/dev/null" })
17- def _test_non_working_libclang_library_file (self ):
25+ def test_non_working_libclang_library_file (self ):
26+ config = reset_import_and_get_frech_config ()
27+ import clang .cindex
28+
1829 with self .assertRaises (clang .cindex .LibclangError ):
19- Config () .lib
30+ config .lib
2031
2132 def test_working_libclang_library_path (self ):
22- ref_libclang_library_file = Config ().get_filename ()
33+ # Get adequate libclang path
34+ ref_libclang_library_file = reset_import_and_get_frech_config ().get_filename ()
2335 ref_libclang_library_path , filename = os .path .split (ref_libclang_library_file )
2436 filename_root , filename_ext = os .path .splitext (filename )
2537
@@ -34,9 +46,12 @@ def test_working_libclang_library_path(self):
3446 with unittest .mock .patch .dict (
3547 os .environ , {"LIBCLANG_LIBRARY_PATH" : ref_libclang_library_path }
3648 ):
37- Config ().lib
49+ reset_import_and_get_frech_config ().lib
3850
3951 @unittest .mock .patch .dict ("os.environ" , {"LIBCLANG_LIBRARY_PATH" : "not_a_real_dir" })
4052 def _test_non_working_libclang_library_path (self ):
53+ config = reset_import_and_get_frech_config ()
54+ import clang .cindex
55+
4156 with self .assertRaises (clang .cindex .LibclangError ):
42- Config () .lib
57+ config .lib
0 commit comments