@@ -75,15 +75,29 @@ def test_auth_has_file_settings(self, patched_init, read_settings_from_file):
7575
7676
7777class TestCliFileUsing (fake_filesystem_unittest .TestCase ):
78- HOME_FILE_CONTENT = ("host =https://jenkins.host.com\n "
78+ HOME_FILE_CONTENT = ("[DEFAULT]\n "
79+ "host =https://jenkins.host.com\n "
7980 "username= username\n "
8081 "some weird settings = value = value" )
8182
82- LOCAL_FILE_CONTENT = ("host=http://jenkins.localhosthost.ua\n "
83+ LOCAL_FILE_CONTENT = ("[DEFAULT]\n "
84+ "host=http://jenkins.localhosthost.ua\n "
8385 "username=Denys\n "
8486 "password=myPassword\n "
8587 "other_setting=some_value" )
8688
89+ MULTIENV_FILE_CONTENT = ("[DEFAULT]\n "
90+ "host =https://jenkins.host.com\n "
91+ "username= username\n "
92+ "some default settings = value = value\n "
93+ "\n "
94+ "[alternative]\n "
95+ "host=http://jenkins.localhosthost.ua\n "
96+ "username=Denys\n "
97+ "password=myPassword\n "
98+ "other_setting=some_value"
99+ )
100+
87101 def setUp (self ):
88102 self .setUpPyfakefs ()
89103
@@ -97,7 +111,7 @@ def test_read_settings_from_file(self):
97111 self .fs .CreateFile (home_folder_filename ,
98112 contents = self .HOME_FILE_CONTENT )
99113 self .assertTrue (os .path .exists (home_folder_filename ))
100- settings_dict = JenkinsCli .read_settings_from_file ()
114+ settings_dict = JenkinsCli .read_settings_from_file (environment = None )
101115 self .assertEqual (settings_dict ,
102116 {"host" : 'https://jenkins.host.com' ,
103117 "username" : "username" ,
@@ -107,19 +121,43 @@ def test_read_settings_from_file(self):
107121 self .fs .CreateFile (local_folder_filename ,
108122 contents = self .LOCAL_FILE_CONTENT )
109123 self .assertTrue (os .path .exists (local_folder_filename ))
110- settings_dict = JenkinsCli .read_settings_from_file ()
124+ settings_dict = JenkinsCli .read_settings_from_file (environment = None )
111125 self .assertEqual (settings_dict ,
112126 {"host" : 'http://jenkins.localhosthost.ua' ,
113127 "username" : "Denys" ,
114128 "password" : "myPassword" ,
115129 "other_setting" : "some_value"
116130 })
117131
132+ def test_read_settings_from_file_alt_environment (self ):
133+ # make sure we are in the fake fs
134+ current_folder = os .getcwd ()
135+ local_folder_filename = os .path .join (current_folder , JenkinsCli .SETTINGS_FILE_NAME )
136+ self .assertFalse (os .path .exists (local_folder_filename ))
137+
138+ # create the fake config file
139+ self .fs .CreateFile (local_folder_filename ,
140+ contents = self .MULTIENV_FILE_CONTENT )
141+ self .assertTrue (os .path .exists (local_folder_filename ))
142+
143+ # read the config from the file
144+ settings_dict = JenkinsCli .read_settings_from_file (environment = 'alternative' )
145+
146+ # test and that the alternative environment is used, with the missing
147+ # values being provided from the DEFAULT environmtne
148+ self .assertEqual (settings_dict ,
149+ {"host" : 'http://jenkins.localhosthost.ua' ,
150+ "username" : "Denys" ,
151+ "password" : "myPassword" ,
152+ "other_setting" : "some_value" ,
153+ 'some default settings' : 'value = value'
154+ })
155+
118156
119157class TestCliCommands (unittest .TestCase ):
120158
121159 def setUp (self ):
122- self .args = Namespace (host = 'http://jenkins.host.com' , username = None , password = None )
160+ self .args = Namespace (host = 'http://jenkins.host.com' , username = None , password = None , environment = None )
123161 self .print_patcher = mock .patch ('jenkins_cli.cli.print' )
124162 self .patched_print = self .print_patcher .start ()
125163
0 commit comments