3
3
"""
4
4
5
5
from traitlets import Dict
6
+ from traitlets .config import Config
6
7
import os
7
8
import sys
8
9
9
10
from tljh import configurer
10
11
11
12
12
- class MockConfigurer :
13
- """
14
- Mock a Traitlets Config class object.
15
-
16
- Equivalent to the `c` in `c.JupyterHub.some_property` method of setting
17
- traitlet properties. If an accessed attribute doesn't exist, a new instance
18
- of EmtpyObject is returned. This lets us set arbitrary attributes two
19
- levels deep.
20
-
21
- >>> c = MockConfigurer()
22
- >>> c.FirstLevel.second_level = 'hi'
23
- >>> c.FirstLevel.second_level == 'hi'
24
- True
25
- >>> hasattr(c.FirstLevel, 'does_not_exist')
26
- False
27
-
28
- The actual Config class implementation can be found at
29
- https://github.com/ipython/traitlets/blob/34f596dd03b98434900a7d31c912fc168342bb80/traitlets/config/loader.py#L220
30
- """
31
-
32
- class _EmptyObject :
33
- """
34
- Empty class for putting attributes in.
35
- """
36
- pass
37
-
38
- def __getattr__ (self , k ):
39
- if k not in self .__dict__ :
40
- self .__dict__ [k ] = MockConfigurer ._EmptyObject ()
41
- return self .__dict__ [k ]
42
-
43
- def __getitem__ (self , key ):
44
- """
45
- To mimic the traitlets Config class instance we often access as "c", we
46
- need to provide a subscript functionality that can be used as
47
- c["Something"]. To do this, we provide a __getitem__ function.
48
- """
49
- return self .__getattr__ (key )
50
-
51
- def test_mock_configurer ():
52
- """
53
- Test the MockConfigurer's mocking ability
54
- """
55
- m = MockConfigurer ()
56
- m .SomethingSomething = 'hi'
57
- m .FirstLevel .second_level = 'boo'
58
-
59
- assert m .SomethingSomething == 'hi'
60
- assert m .FirstLevel .second_level == 'boo'
61
- assert m ["FirstLevel" ].second_level == 'boo'
62
13
63
- assert not hasattr (m .FirstLevel , 'non_existent' )
64
14
65
15
66
16
def apply_mock_config (overrides ):
@@ -69,7 +19,7 @@ def apply_mock_config(overrides):
69
19
70
20
overrides should be a dict that matches what you parse from a config.yaml
71
21
"""
72
- c = MockConfigurer ()
22
+ c = Config ()
73
23
configurer .apply_config (overrides , c )
74
24
return c
75
25
@@ -112,7 +62,7 @@ def test_app_default():
112
62
"""
113
63
c = apply_mock_config ({})
114
64
# default_url is not set, so JupyterHub will pick default.
115
- assert not hasattr ( c .Spawner , 'default_url' )
65
+ assert 'default_url' not in c .Spawner
116
66
117
67
118
68
def test_app_jupyterlab ():
0 commit comments