1
1
#!/usr/bin/env python
2
2
3
- import os
4
3
import shutil
5
4
import tempfile
6
5
import unittest
6
+ import unittest .mock
7
7
from tempfile import NamedTemporaryFile
8
8
9
9
import can
10
10
11
11
12
12
class LoadConfigTest (unittest .TestCase ):
13
- configuration = {
13
+ configuration_in = {
14
14
"default" : {"interface" : "serial" , "channel" : "0" },
15
15
"one" : {"interface" : "kvaser" , "channel" : "1" , "bitrate" : 100000 },
16
16
"two" : {"channel" : "2" },
17
17
}
18
+ configuration_out = {
19
+ "default" : {"interface" : "serial" , "channel" : 0 },
20
+ "one" : {"interface" : "kvaser" , "channel" : 1 , "bitrate" : 100000 },
21
+ "two" : {"channel" : 2 },
22
+ }
18
23
19
24
def setUp (self ):
20
25
# Create a temporary directory
@@ -31,7 +36,7 @@ def _gen_configration_file(self, sections):
31
36
content = []
32
37
for section in sections :
33
38
content .append (f"[{ section } ]" )
34
- for k , v in self .configuration [section ].items ():
39
+ for k , v in self .configuration_in [section ].items ():
35
40
content .append (f"{ k } = { v } " )
36
41
tmp_config_file .write ("\n " .join (content ))
37
42
return tmp_config_file .name
@@ -42,43 +47,43 @@ def _dict_to_env(self, d):
42
47
def test_config_default (self ):
43
48
tmp_config = self ._gen_configration_file (["default" ])
44
49
config = can .util .load_config (path = tmp_config )
45
- self .assertEqual (config , self .configuration ["default" ])
50
+ self .assertEqual (config , self .configuration_out ["default" ])
46
51
47
52
def test_config_whole_default (self ):
48
- tmp_config = self ._gen_configration_file (self .configuration )
53
+ tmp_config = self ._gen_configration_file (self .configuration_in )
49
54
config = can .util .load_config (path = tmp_config )
50
- self .assertEqual (config , self .configuration ["default" ])
55
+ self .assertEqual (config , self .configuration_out ["default" ])
51
56
52
57
def test_config_whole_context (self ):
53
- tmp_config = self ._gen_configration_file (self .configuration )
58
+ tmp_config = self ._gen_configration_file (self .configuration_in )
54
59
config = can .util .load_config (path = tmp_config , context = "one" )
55
- self .assertEqual (config , self .configuration ["one" ])
60
+ self .assertEqual (config , self .configuration_out ["one" ])
56
61
57
62
def test_config_merge_context (self ):
58
- tmp_config = self ._gen_configration_file (self .configuration )
63
+ tmp_config = self ._gen_configration_file (self .configuration_in )
59
64
config = can .util .load_config (path = tmp_config , context = "two" )
60
- expected = self .configuration ["default" ]
61
- expected .update (self .configuration ["two" ])
65
+ expected = self .configuration_out ["default" ]. copy ()
66
+ expected .update (self .configuration_out ["two" ])
62
67
self .assertEqual (config , expected )
63
68
64
69
def test_config_merge_environment_to_context (self ):
65
- tmp_config = self ._gen_configration_file (self .configuration )
70
+ tmp_config = self ._gen_configration_file (self .configuration_in )
66
71
env_data = {"interface" : "serial" , "bitrate" : 125000 }
67
72
env_dict = self ._dict_to_env (env_data )
68
73
with unittest .mock .patch .dict ("os.environ" , env_dict ):
69
74
config = can .util .load_config (path = tmp_config , context = "one" )
70
- expected = self .configuration ["one" ]
75
+ expected = self .configuration_out ["one" ]. copy ()
71
76
expected .update (env_data )
72
77
self .assertEqual (config , expected )
73
78
74
79
def test_config_whole_environment (self ):
75
- tmp_config = self ._gen_configration_file (self .configuration )
80
+ tmp_config = self ._gen_configration_file (self .configuration_in )
76
81
env_data = {"interface" : "socketcan" , "channel" : "3" , "bitrate" : 250000 }
77
82
env_dict = self ._dict_to_env (env_data )
78
83
with unittest .mock .patch .dict ("os.environ" , env_dict ):
79
84
config = can .util .load_config (path = tmp_config , context = "one" )
80
- expected = self .configuration ["one" ]
81
- expected .update (env_data )
85
+ expected = self .configuration_out ["one" ]. copy ()
86
+ expected .update ({ "interface" : "socketcan" , "channel" : 3 , "bitrate" : 250000 } )
82
87
self .assertEqual (config , expected )
83
88
84
89
0 commit comments