44import os
55import unittest
66
7+ from _common import CORE_MODULE_DIR , SEARCH_DIRS , YANG_DIR
78from libyang import Context , LibyangError , Module , SContainer , SLeaf , SLeafList
89from libyang .util import c2str
910
1011
11- YANG_DIR = os .path .join (os .path .dirname (__file__ ), "yang" )
12-
13-
1412# -------------------------------------------------------------------------------------
1513
1614
1715class ContextTest (unittest .TestCase ):
1816 def test_ctx_no_dir (self ):
19- with Context () as ctx :
17+ with Context (CORE_MODULE_DIR ) as ctx :
2018 self .assertIsNot (ctx , None )
2119
2220 def test_ctx_yanglib (self ):
2321 ctx = Context (
24- YANG_DIR ,
22+ ":" . join ([ YANG_DIR , CORE_MODULE_DIR ]) ,
2523 yanglib_path = YANG_DIR + "/yang-library.json" ,
2624 compile_obsolete = True ,
2725 )
@@ -31,22 +29,24 @@ def test_ctx_yanglib(self):
3129 self .assertIsInstance (j , str )
3230
3331 def test_ctx_dir (self ):
34- with Context (YANG_DIR ) as ctx :
32+ with Context (SEARCH_DIRS ) as ctx :
3533 self .assertIsNot (ctx , None )
3634
3735 def test_ctx_duplicate_searchpath (self ):
38- duplicate_search_path = ":" .join ([YANG_DIR , YANG_DIR ])
36+ duplicate_search_path = ":" .join ([YANG_DIR , YANG_DIR , CORE_MODULE_DIR ])
3937 try :
4038 Context (duplicate_search_path )
4139 except LibyangError :
4240 self .fail ("Context.__init__ should not raise LibyangError" )
4341
4442 def test_ctx_invalid_dir (self ):
45- with Context ("/does/not/exist" ) as ctx :
43+ with Context (":" . join ([ " /does/not/exist", CORE_MODULE_DIR ]) ) as ctx :
4644 self .assertIsNot (ctx , None )
4745
4846 def test_ctx_missing_dir (self ):
49- with Context (os .path .join (YANG_DIR , "yolo" )) as ctx :
47+ with Context (
48+ ":" .join ([os .path .join (YANG_DIR , "yolo" ), CORE_MODULE_DIR ])
49+ ) as ctx :
5050 self .assertIsNot (ctx , None )
5151 with self .assertRaises (LibyangError ):
5252 ctx .load_module ("yolo-system" )
@@ -56,51 +56,53 @@ def test_ctx_env_search_dir(self):
5656 os .environ ["YANGPATH" ] = ":" .join (
5757 [os .path .join (YANG_DIR , "omg" ), os .path .join (YANG_DIR , "wtf" )]
5858 )
59- with Context (os .path .join (YANG_DIR , "yolo" )) as ctx :
59+ with Context (
60+ ":" .join ([os .path .join (YANG_DIR , "yolo" ), CORE_MODULE_DIR ])
61+ ) as ctx :
6062 mod = ctx .load_module ("yolo-system" )
6163 self .assertIsInstance (mod , Module )
6264 finally :
6365 del os .environ ["YANGPATH" ]
6466
6567 def test_ctx_load_module (self ):
66- with Context (YANG_DIR ) as ctx :
68+ with Context (SEARCH_DIRS ) as ctx :
6769 mod = ctx .load_module ("yolo-system" )
6870 self .assertIsInstance (mod , Module )
6971
7072 def test_ctx_load_module_with_features (self ):
71- with Context (YANG_DIR ) as ctx :
73+ with Context (SEARCH_DIRS ) as ctx :
7274 mod = ctx .load_module ("yolo-system" , None , ["*" ])
7375 self .assertIsInstance (mod , Module )
7476 for f in list (mod .features ()):
7577 self .assertTrue (f .state ())
7678
7779 def test_ctx_get_module (self ):
78- with Context (YANG_DIR ) as ctx :
80+ with Context (SEARCH_DIRS ) as ctx :
7981 ctx .load_module ("yolo-system" )
8082 mod = ctx .get_module ("wtf-types" )
8183 self .assertIsInstance (mod , Module )
8284
8385 def test_ctx_get_invalid_module (self ):
84- with Context (YANG_DIR ) as ctx :
86+ with Context (SEARCH_DIRS ) as ctx :
8587 ctx .load_module ("wtf-types" )
8688 with self .assertRaises (LibyangError ):
8789 ctx .get_module ("yolo-system" )
8890
8991 def test_ctx_load_invalid_module (self ):
90- with Context (YANG_DIR ) as ctx :
92+ with Context (SEARCH_DIRS ) as ctx :
9193 with self .assertRaises (LibyangError ):
9294 ctx .load_module ("invalid-module" )
9395
9496 def test_ctx_find_path (self ):
95- with Context (YANG_DIR ) as ctx :
97+ with Context (SEARCH_DIRS ) as ctx :
9698 ctx .load_module ("yolo-system" )
9799 node = next (ctx .find_path ("/yolo-system:conf/offline" ))
98100 self .assertIsInstance (node , SLeaf )
99101 node2 = next (ctx .find_path ("../number" , root_node = node ))
100102 self .assertIsInstance (node2 , SLeafList )
101103
102104 def test_ctx_find_xpath_atoms (self ):
103- with Context (YANG_DIR ) as ctx :
105+ with Context (SEARCH_DIRS ) as ctx :
104106 ctx .load_module ("yolo-system" )
105107 node_iter = ctx .find_xpath_atoms ("/yolo-system:conf/offline" )
106108 node = next (node_iter )
@@ -116,7 +118,7 @@ def test_ctx_find_xpath_atoms(self):
116118 self .assertIsInstance (node , SLeafList )
117119
118120 def test_ctx_iter_modules (self ):
119- with Context (YANG_DIR ) as ctx :
121+ with Context (SEARCH_DIRS ) as ctx :
120122 ctx .load_module ("yolo-system" )
121123 modules = list (iter (ctx ))
122124 self .assertGreater (len (modules ), 0 )
@@ -126,29 +128,31 @@ def test_ctx_iter_modules(self):
126128 def test_ctx_parse_module (self ):
127129 with open (self .YOLO_MOD_PATH , encoding = "utf-8" ) as f :
128130 mod_str = f .read ()
129- with Context (YANG_DIR ) as ctx :
131+ with Context (SEARCH_DIRS ) as ctx :
130132 mod = ctx .parse_module_str (mod_str , features = ["turbo-boost" , "networking" ])
131133 self .assertIsInstance (mod , Module )
132134
133135 with open (self .YOLO_MOD_PATH , encoding = "utf-8" ) as f :
134- with Context (YANG_DIR ) as ctx :
136+ with Context (SEARCH_DIRS ) as ctx :
135137 mod = ctx .parse_module_file (f , features = ["turbo-boost" , "networking" ])
136138 self .assertIsInstance (mod , Module )
137139
138140 def test_ctx_leafref_extended (self ):
139- with Context (YANG_DIR , leafref_extended = True ) as ctx :
141+ with Context (SEARCH_DIRS , leafref_extended = True ) as ctx :
140142 mod = ctx .load_module ("yolo-leafref-extended" )
141143 self .assertIsInstance (mod , Module )
142144
143145 def test_context_dict (self ):
144- with Context (YANG_DIR ) as ctx :
146+ with Context (SEARCH_DIRS ) as ctx :
145147 orig_str = "teststring"
146148 handle = ctx .add_to_dict (orig_str )
147149 self .assertEqual (orig_str , c2str (handle ))
148150 ctx .remove_from_dict (orig_str )
149151
150152 def test_ctx_disable_searchdirs (self ):
151- with Context (YANG_DIR , disable_searchdirs = True ) as ctx :
153+ # LY_CTX_DISABLE_SEARCHDIRS no longer works at init time
154+ with Context (SEARCH_DIRS ) as ctx :
155+ ctx .set_disable_searchdirs (True )
152156 with self .assertRaises (LibyangError ):
153157 ctx .load_module ("yolo-nodetypes" )
154158
@@ -168,7 +172,8 @@ def get_module_valid_clb(mod_name, *_):
168172 def get_module_invalid_clb (mod_name , * _ ):
169173 return None
170174
171- with Context (YANG_DIR , disable_searchdirs = True ) as ctx :
175+ with Context (SEARCH_DIRS ) as ctx :
176+ ctx .set_disable_searchdirs (True )
172177 with self .assertRaises (LibyangError ):
173178 ctx .load_module ("yolo-nodetypes" )
174179
0 commit comments