@@ -264,6 +264,37 @@ def test_attribute(self) -> None:
264264 assert 'foo' == ns ['b' ] == ns .b
265265 assert 3.14 == ns ['c' ]['x' ] == ns .c .x
266266
267+ def test_attribute_missing (self ) -> None :
268+ """Test raises AttributeError if item is missing."""
269+ ns = Namespace ({'a' : 1 , 'b' : 'foo' , 'c' : {'x' : 3.14 }})
270+ with pytest .raises (AttributeError ) as exc_info :
271+ assert ns .d == 42
272+ exc_info .match ('\' d\' not found' )
273+
274+ def test_attribute_expand_env (self ) -> None :
275+ """Test transparent environment variable expansion."""
276+ os .environ ['CMDKIT_TEST_A' ] = 'foo-bar'
277+ ns = Namespace ({'test_env' : 'CMDKIT_TEST_A' })
278+ assert ns .get ('test' ) is None
279+ assert ns .get ('test_env' ) == 'CMDKIT_TEST_A'
280+ assert ns .test_env == 'CMDKIT_TEST_A'
281+ assert ns .test == 'foo-bar'
282+
283+ def test_attribute_expand_eval (self ) -> None :
284+ """Test transparent shell expression expansion."""
285+ ns = Namespace ({'test_eval' : 'echo foo-bar' })
286+ assert ns .get ('test' ) is None
287+ assert ns .get ('test_eval' ) == 'echo foo-bar'
288+ assert ns .test_eval == 'echo foo-bar'
289+ assert ns .test == 'foo-bar'
290+
291+ def test_attribute_expand_multiple_variants (self ) -> None :
292+ """Test failure to expand because item has multiple variants."""
293+ ns = Namespace ({'a' : 1 , 'test' : 'foo' , 'test_eval' : 'echo bar' })
294+ with pytest .raises (AttributeError ) as exc_info :
295+ assert ns .test == 'foo'
296+ assert exc_info .match ('\' test\' has more than one variant' )
297+
267298 def test_duplicates (self ) -> None :
268299 """Namespace can find duplicate leaves in the tree."""
269300 ns = Namespace ({'a' : {'x' : 1 , 'y' : 2 }, 'b' : {'x' : 3 , 'z' : 4 }})
@@ -463,20 +494,41 @@ def test_blending(self) -> None:
463494 assert cfg ['a' ]['z' ] == 4
464495 assert cfg ['b' ]['z' ] == 3
465496
497+ def test_attribute_missing (self ) -> None :
498+ """Test raises AttributeError if item is missing"""
499+ cfg = Configuration (a = Namespace ({'a' : 1 , 'b' : 'foo' , 'c' : {'x' : 3.14 }}))
500+ with pytest .raises (AttributeError ) as exc_info :
501+ assert cfg .d == 42
502+ exc_info .match ('\' d\' not found' )
503+
466504 def test_attribute_expand_env (self ) -> None :
467505 """Test transparent environment variable expansion."""
468506 os .environ ['CMDKIT_TEST_A' ] = 'foo-bar'
469- ns = Configuration (a = Namespace ({'test_env' : 'CMDKIT_TEST_A' }))
470- assert ns .get ('test' ) is None
471- assert ns .get ('test_env' ) == 'CMDKIT_TEST_A'
472- assert ns .test == 'foo-bar'
507+ cfg = Configuration (a = Namespace ({'nested' : {'test_env' : 'CMDKIT_TEST_A' }}))
508+ assert cfg .nested .get ('test' ) is None
509+ assert cfg .nested .get ('test_env' ) == 'CMDKIT_TEST_A'
510+ assert cfg .nested .test_env == 'CMDKIT_TEST_A'
511+ assert cfg .nested .test == 'foo-bar'
473512
474513 def test_attribute_expand_eval (self ) -> None :
475514 """Test transparent shell expression expansion."""
476- ns = Configuration (a = Namespace ({'test_eval' : 'echo foo-bar' }))
477- assert ns .get ('test' ) is None
478- assert ns .get ('test_eval' ) == 'echo foo-bar'
479- assert ns .test == 'foo-bar'
515+ cfg = Configuration (a = Namespace ({'nested' : {'test_eval' : 'echo foo-bar' }}))
516+ assert cfg .nested .get ('test' ) is None
517+ assert cfg .nested .get ('test_eval' ) == 'echo foo-bar'
518+ assert cfg .nested .test_eval == 'echo foo-bar'
519+ assert cfg .nested .test == 'foo-bar'
520+
521+ def test_attribute_expand_multiple_variants (self ) -> None :
522+ """Test failure to expand because multiple variants found."""
523+ cfg = Configuration (a = Namespace ({'nested' : {'test' : 'foo' , 'a' : 1 }}),
524+ b = Namespace ({'nested' : {'test_eval' : 'echo bar' , 'b' : 2 }}),
525+ c = Namespace ({'other' : {'secret_eval' : 'echo baz' }}))
526+ with pytest .raises (AttributeError ) as exc_info :
527+ assert cfg .nested .a == 1
528+ assert cfg .nested .b == 2
529+ assert cfg .other .secret == 'baz'
530+ assert cfg .nested .test == 'foo'
531+ assert exc_info .match ('\' test\' has more than one variant' )
480532
481533 def test_from_local (self ) -> None :
482534 """Test Configuration.from_local factory method."""
0 commit comments