@@ -2029,6 +2029,49 @@ def test_config_dict_with_int_keys(self):
20292029 config = fdroidserver .common .read_config ()
20302030 self .assertEqual ('/usr/lib/jvm/java-8-openjdk' , config ['java_paths' ]['8' ])
20312031
2032+ @mock .patch .dict (os .environ , {'PATH' : os .getenv ('PATH' )}, clear = True )
2033+ def test_config_lazy_load_env_vars (self ):
2034+ """Test the environment variables in config.yml is lazy loaded.
2035+
2036+ It shouldn't throw errors when read the config if the environment variables are
2037+ not set. It should throw errors when the variables are get from the config.
2038+ """
2039+ os .chdir (self .testdir )
2040+ fdroidserver .common .write_config_file (
2041+ textwrap .dedent (
2042+ """
2043+ serverwebroot: {env: serverwebroot}
2044+ servergitmirrors:
2045+ - url: {env: mirror1}
2046+ - url: {env: mirror2}
2047+ keypass: {env: keypass}
2048+ keystorepass: {env: keystorepass}
2049+ """
2050+ )
2051+ )
2052+ with self .assertNoLogs (level = logging .ERROR ):
2053+ config = fdroidserver .common .read_config ()
2054+
2055+ # KeyError should be raised if a key is not in the config.yml
2056+ with self .assertRaises (KeyError ):
2057+ config ['gpghome' ]
2058+
2059+ self .assertEqual (config .get ('gpghome' , 'gpg' ), 'gpg' )
2060+ os .environ .update ({key : f"{ key } supersecret" for key in ["serverwebroot" , "mirror1" , "mirror2" , "keystorepass" ]})
2061+ self .assertEqual (config ['keystorepass' ], 'keystorepasssupersecret' )
2062+ self .assertEqual (config ['serverwebroot' ], [{'url' : 'serverwebrootsupersecret/' }])
2063+ self .assertEqual (config ['servergitmirrors' ], [{'url' : 'mirror1supersecret' }, {'url' : 'mirror2supersecret' }])
2064+
2065+ @mock .patch .dict (os .environ , {'PATH' : os .getenv ('PATH' )}, clear = True )
2066+ def test_config_lazy_load_env_vars_not_set (self ):
2067+ os .chdir (self .testdir )
2068+ fdroidserver .common .write_config_file ('keypass: {env: keypass}' )
2069+ fdroidserver .common .read_config ()
2070+ with self .assertLogs (level = logging .ERROR ) as lw :
2071+ fdroidserver .common .config ['keypass' ]
2072+ self .assertTrue ('is not set' in lw .output [0 ])
2073+ self .assertEqual (1 , len (lw .output ))
2074+
20322075 @mock .patch .dict (os .environ , {'PATH' : os .getenv ('PATH' )}, clear = True )
20332076 def test_test_sdk_exists_fails_on_bad_sdk_path (self ):
20342077 config = {'sdk_path' : 'nothinghere' }
@@ -3465,7 +3508,7 @@ def test_get_config(self):
34653508 self .assertIsNone (fdroidserver .common .config )
34663509 config = fdroidserver .common .read_config ()
34673510 self .assertIsNotNone (fdroidserver .common .config )
3468- self .assertEqual ( dict , type (config ))
3511+ self .assertTrue ( isinstance (config , dict ))
34693512 self .assertEqual (config , fdroidserver .common .config )
34703513
34713514 def test_get_config_global (self ):
@@ -3475,7 +3518,7 @@ def test_get_config_global(self):
34753518 self .assertIsNone (fdroidserver .common .config )
34763519 c = fdroidserver .common .read_config ()
34773520 self .assertIsNotNone (fdroidserver .common .config )
3478- self .assertEqual ( dict , type ( c ))
3521+ self .assertTrue ( isinstance ( c , dict ))
34793522 self .assertEqual (c , fdroidserver .common .config )
34803523 self .assertTrue (
34813524 'config' not in vars () and 'config' not in globals (),
@@ -3515,14 +3558,6 @@ def test_config_perm_env_warning(self):
35153558 self .assertTrue ('unsafe' in lw .output [0 ])
35163559 self .assertEqual (1 , len (lw .output ))
35173560
3518- @mock .patch .dict (os .environ , {'PATH' : os .getenv ('PATH' )}, clear = True )
3519- def test_config_perm_unset_env_no_warning (self ):
3520- fdroidserver .common .write_config_file ('keypass: {env: keypass}' )
3521- with self .assertLogs (level = logging .WARNING ) as lw :
3522- fdroidserver .common .read_config ()
3523- self .assertTrue ('unsafe' not in lw .output [0 ])
3524- self .assertEqual (1 , len (lw .output ))
3525-
35263561
35273562class GetHeadCommitIdTest (unittest .TestCase ):
35283563 """Test and compare two methods of getting the commit ID."""
0 commit comments