@@ -97,58 +97,73 @@ def test_from_configuration_files_get_typed_value(tmp_path_factory: pytest.TempP
9797
9898
9999@pytest .mark .parametrize (
100- "config_location, config_content , expected_result" ,
100+ "config_setup , expected_result" ,
101101 [
102+ # Validate lookup works with: config > home > cwd
102103 (
103- "config" ,
104- {"catalog" : {"default" : {"uri" : "https://service.io/api" }}},
104+ {"config_location" : "config" , "config_content" : {"catalog" : {"default" : {"uri" : "https://service.io/api" }}}},
105105 {"catalog" : {"default" : {"uri" : "https://service.io/api" }}},
106106 ),
107107 (
108- "home" ,
109- {"catalog" : {"default" : {"uri" : "https://service.io/api" }}},
108+ {"config_location" : "home" , "config_content" : {"catalog" : {"default" : {"uri" : "https://service.io/api" }}}},
110109 {"catalog" : {"default" : {"uri" : "https://service.io/api" }}},
111110 ),
112111 (
113- "current" ,
114- {"catalog" : {"default" : {"uri" : "https://service.io/api" }}},
112+ {"config_location" : "current" , "config_content" : {"catalog" : {"default" : {"uri" : "https://service.io/api" }}}},
115113 {"catalog" : {"default" : {"uri" : "https://service.io/api" }}},
116114 ),
117- ("none" , None , None ),
115+ (
116+ {"config_location" : "none" , "config_content" : None },
117+ None ,
118+ ),
119+ # Validate lookup order: home > cwd if present in both
120+ (
121+ {
122+ "config_location" : "both" ,
123+ "home_content" : {"catalog" : {"default" : {"uri" : "https://service.io/home" }}},
124+ "current_content" : {"catalog" : {"default" : {"uri" : "https://service.io/current" }}},
125+ },
126+ {"catalog" : {"default" : {"uri" : "https://service.io/home" }}},
127+ ),
118128 ],
119129)
120130def test_from_multiple_configuration_files (
121131 monkeypatch : pytest .MonkeyPatch ,
122132 tmp_path_factory : pytest .TempPathFactory ,
123- config_location : str ,
124- config_content : Optional [Dict [str , Any ]],
133+ config_setup : Dict [str , Any ],
125134 expected_result : Optional [Dict [str , Any ]],
126135) -> None :
127- def create_config_file (directory : str , content : Optional [Dict [str , Any ]]) -> None :
128- config_file_path = os .path .join (directory , ".pyiceberg.yaml" )
129- with open (config_file_path , "w" , encoding = "utf-8" ) as file :
130- yaml_str = as_document (content ).as_yaml () if content else ""
131- file .write (yaml_str )
132-
133- config_path = str (tmp_path_factory .mktemp ("config" ))
134- home_path = str (tmp_path_factory .mktemp ("home" ))
135- current_path = str (tmp_path_factory .mktemp ("current" ))
136-
137- location_to_path = {
138- "config" : config_path ,
139- "home" : home_path ,
140- "current" : current_path ,
136+ def create_config_files (
137+ paths : Dict [str , str ],
138+ contents : Dict [str , Optional [Dict [str , Any ]]],
139+ ) -> None :
140+ """Helper to create configuration files in specified paths."""
141+ for location , content in contents .items ():
142+ if content :
143+ config_file_path = os .path .join (paths [location ], ".pyiceberg.yaml" )
144+ with open (config_file_path , "w" , encoding = "UTF8" ) as file :
145+ yaml_str = as_document (content ).as_yaml () if content else ""
146+ file .write (yaml_str )
147+
148+ paths = {
149+ "config" : str (tmp_path_factory .mktemp ("config" )),
150+ "home" : str (tmp_path_factory .mktemp ("home" )),
151+ "current" : str (tmp_path_factory .mktemp ("current" )),
141152 }
142153
143- if config_location in location_to_path and config_content :
144- create_config_file (location_to_path [config_location ], config_content )
154+ contents = {
155+ "config" : config_setup .get ("config_content" ) if config_setup .get ("config_location" ) == "config" else None ,
156+ "home" : config_setup .get ("home_content" ) if config_setup .get ("config_location" ) in ["home" , "both" ] else None ,
157+ "current" : config_setup .get ("current_content" ) if config_setup .get ("config_location" ) in ["current" , "both" ] else None ,
158+ }
145159
146- monkeypatch .setenv ("PYICEBERG_HOME" , config_path )
147- monkeypatch .setattr (os .path , "expanduser" , lambda _ : home_path )
160+ create_config_files (paths , contents )
148161
149- if config_location == "current" :
150- monkeypatch .chdir (current_path )
162+ monkeypatch .setenv ("PYICEBERG_HOME" , paths ["config" ])
163+ monkeypatch .setattr (os .path , "expanduser" , lambda _ : paths ["home" ])
164+ if config_setup .get ("config_location" ) in ["current" , "both" ]:
165+ monkeypatch .chdir (paths ["current" ])
151166
152167 assert Config ()._from_configuration_files () == expected_result , (
153- f"Unexpected configuration result for content: { config_content } "
168+ f"Unexpected configuration result for content: { expected_result } "
154169 )
0 commit comments