11#!/usr/bin/env python
22
3- import os
43import shutil
54import tempfile
65import unittest
6+ import unittest .mock
77from tempfile import NamedTemporaryFile
88
99import can
1010
1111
1212class LoadConfigTest (unittest .TestCase ):
13- configuration = {
13+ configuration_in = {
1414 "default" : {"interface" : "serial" , "channel" : "0" },
1515 "one" : {"interface" : "kvaser" , "channel" : "1" , "bitrate" : 100000 },
1616 "two" : {"channel" : "2" },
1717 }
18+ configuration_out = {
19+ "default" : {"interface" : "serial" , "channel" : 0 },
20+ "one" : {"interface" : "kvaser" , "channel" : 1 , "bitrate" : 100000 },
21+ "two" : {"channel" : 2 },
22+ }
1823
1924 def setUp (self ):
2025 # Create a temporary directory
@@ -31,7 +36,7 @@ def _gen_configration_file(self, sections):
3136 content = []
3237 for section in sections :
3338 content .append (f"[{ section } ]" )
34- for k , v in self .configuration [section ].items ():
39+ for k , v in self .configuration_in [section ].items ():
3540 content .append (f"{ k } = { v } " )
3641 tmp_config_file .write ("\n " .join (content ))
3742 return tmp_config_file .name
@@ -42,43 +47,43 @@ def _dict_to_env(self, d):
4247 def test_config_default (self ):
4348 tmp_config = self ._gen_configration_file (["default" ])
4449 config = can .util .load_config (path = tmp_config )
45- self .assertEqual (config , self .configuration ["default" ])
50+ self .assertEqual (config , self .configuration_out ["default" ])
4651
4752 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 )
4954 config = can .util .load_config (path = tmp_config )
50- self .assertEqual (config , self .configuration ["default" ])
55+ self .assertEqual (config , self .configuration_out ["default" ])
5156
5257 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 )
5459 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" ])
5661
5762 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 )
5964 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" ])
6267 self .assertEqual (config , expected )
6368
6469 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 )
6671 env_data = {"interface" : "serial" , "bitrate" : 125000 }
6772 env_dict = self ._dict_to_env (env_data )
6873 with unittest .mock .patch .dict ("os.environ" , env_dict ):
6974 config = can .util .load_config (path = tmp_config , context = "one" )
70- expected = self .configuration ["one" ]
75+ expected = self .configuration_out ["one" ]. copy ()
7176 expected .update (env_data )
7277 self .assertEqual (config , expected )
7378
7479 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 )
7681 env_data = {"interface" : "socketcan" , "channel" : "3" , "bitrate" : 250000 }
7782 env_dict = self ._dict_to_env (env_data )
7883 with unittest .mock .patch .dict ("os.environ" , env_dict ):
7984 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 } )
8287 self .assertEqual (config , expected )
8388
8489
0 commit comments