1+ import os
12import pytest
23
4+ from pathlib import Path , PurePath
5+
36from manage .config import (
7+ _EnvironWithFallback ,
48 _expand_vars ,
59 config_append ,
610 config_bool ,
711 config_dict_merge ,
812 config_split ,
913 config_split_append ,
14+ resolve_config ,
1015)
1116
1217
@@ -88,16 +93,26 @@ def test_expand_vars(value, variables, expected):
8893 assert _expand_vars (value , variables ) == expected
8994
9095
91- def test_expand_vars_removes_missing_variable_and_leading_separator ():
92- actual = _expand_vars ("%MISSING%\\ Python" , {})
96+ def test_expand_vars_fails_on_missing_variable ():
97+ with pytest .raises (LookupError ):
98+ _expand_vars ("%MISSING%\\ Python" , {})
9399
94- assert actual == "Python"
95100
101+ def test_expand_vars_fails_on_empty_variable ():
102+ with pytest .raises (LookupError ):
103+ _expand_vars ("%EMPTY%\\ Python" , {"EMPTY" : "" })
96104
97- def test_expand_vars_removes_empty_variable_and_leading_separator ():
98- actual = _expand_vars ("%EMPTY%\\ Python" , {"EMPTY" : "" })
99105
100- assert actual == "Python"
106+ def test_environ_with_fallback ():
107+ env = _EnvironWithFallback ({"AllUsersProfile" : "test" , "APPDATA" : "" })
108+ # Should be the value we provided, despite case
109+ assert env .get ("allusersprofile" ) == "test"
110+ # Should be empty, since it was provided as empty
111+ assert env .get ("AppData" , "default" ) == ""
112+ # Should be set, even though it wasn't provided
113+ assert env .get ("WinDir" , "default" ) not in (None , "" , "default" )
114+ # Should be empty, since it's neither set nor has a fallback
115+ assert env .get ("PYTHON_MANAGER_DEFAULT" , "default" ) == "default"
101116
102117
103118@pytest .mark .parametrize (
@@ -200,4 +215,40 @@ def test_config_split_append_does_not_mutate_existing_list():
200215
201216 assert actual == ["first" , ["second" , "third" ]]
202217 assert existing == ["first" ]
203- assert actual is not existing
218+ assert actual is not existing
219+
220+
221+ @pytest .mark .parametrize (
222+ ("cfg" , "schema" , "expect" , "env" ),
223+ [
224+ ({}, {"a" : (int , None ), "b" : (int , None )}, {}, None ),
225+ ({"a" : "1" }, {"a" : (int , None )}, {"a" : 1 }, None ),
226+ ({"a" : 1 }, {"a" : (str , None )}, {"a" : "1" }, None ),
227+ ({"a" : "abc" }, {"a" : (str , None , "path" )}, {"a" : PurePath ("X:/test/abc" )}, None ),
228+ ({"a" : "%X%" }, {"a" : (str , None , "path" )}, {"a" : PurePath ("X:/test/%X%" )}, {"X" : "abc" }),
229+ ({"a" : "%X%" }, {"a" : (str , None , "path" , "env" )}, {"a" : PurePath ("X:/env/abc" )}, {"X" : "X:/env/abc" }),
230+ ({"a" : "%X%" }, {"a" : (str , None , "path" )}, {"a" : PurePath ("X:/test/%X%" )}, None ),
231+ ],
232+ ids = [
233+ "empty" ,
234+ "to_int" ,
235+ "to_str" ,
236+ "to_path" ,
237+ "to_path_without_env" ,
238+ "to_path_with_env" ,
239+ "to_path_not_in_env" ,
240+ ],
241+ )
242+ def test_config_resolve (cfg , schema , expect , env , monkeypatch ):
243+ resolve_config (cfg , "<test.cfg>" , PurePath ("X:/test/" ), schema = schema , environ = env )
244+ assert cfg == expect
245+
246+
247+ def test_config_resolve_env_fallback (assert_log ):
248+ cfg = {"a" : "%AppData%/Python" }
249+ schema = {"a" : (str , None , "path" , "env" )}
250+ expect = {"a" : Path (os .environ ["AppData" ]) / "Python" }
251+ env = _EnvironWithFallback ({k : v for k , v in os .environ .items () if k .lower () != "appdata" })
252+ resolve_config (cfg , "<test.cfg>" , Path (), schema = schema , environ = env )
253+ assert cfg == expect
254+ assert_log (("Environment variable.*not found" , ["AppData" ]))
0 commit comments