@@ -245,63 +245,99 @@ def test_load_user_metadata_bad(inputs, msg):
245
245
actual_args = load_user_metadata (actual_args )
246
246
247
247
248
- # For each test case, check username and email are properly loaded and written into config file
249
248
params_user_info = [
250
- # No existing config file, user enters valid username and email
251
- ([
"good_username" ,
"[email protected] " ,
"non_existing_file.json" ], [
"good_username" ,
"[email protected] " ]),
252
- # There exists a config file, so we read it and fetch info, no user input is fine
253
- ([
"" ,
"" ,
"test.json" ], [
"good_username" ,
"[email protected] " ]),
254
- # There exists a config file, user input username/email overwrites the username/email in the file
255
- ([
"new_username" ,
"" ,
"test.json" ], [
"new_username" ,
"[email protected] " ]),
256
- ([
"" ,
"[email protected] " ,
"test.json" ], [
"good_username" ,
"[email protected] " ]),
257
- ([
"new_username" ,
"[email protected] " ,
"test.json" ], [
"new_username" ,
"[email protected] " ]),
258
- # There exists a config file that does not contain username/email, then we write user inputs into config file
259
- ([
"good_username" ,
"[email protected] " ,
"test2.json" ], [
"good_username" ,
"[email protected] " ]),
249
+ # No config file, check username and email are properly loaded and config file in ~ is created and written
250
+ (
251
+ [
"new_username" ,
"[email protected] " ],
252
+ ["input_dir" , "input_dir/diffpyconfig.json" , "diffpyconfig.json" , "diffpyconfig.json" ],
253
+ [
"new_username" ,
"[email protected] " ,
"new_username" ,
"[email protected] " ],
254
+ ),
255
+ # Config file in cwd, check username and email are properly loaded and config file is unchanged
256
+ (
257
+ ["" , "" ],
258
+ ["conf_dir" , "conf_dir/diffpyconfig.json" , "diffpyconfig.json" , "conf_dir/diffpyconfig.json" ],
259
+ [
"good_username" ,
"[email protected] " ,
"good_username" ,
"[email protected] " ],
260
+ ),
261
+ (
262
+ ["new_username" , "" ],
263
+ ["conf_dir" , "conf_dir/diffpyconfig.json" , "diffpyconfig.json" , "conf_dir/diffpyconfig.json" ],
264
+ [
"new_username" ,
"[email protected] " ,
"good_username" ,
"[email protected] " ],
265
+ ),
266
+ (
267
+
268
+ ["conf_dir" , "conf_dir/diffpyconfig.json" , "diffpyconfig.json" , "conf_dir/diffpyconfig.json" ],
269
+ [
"good_username" ,
"[email protected] " ,
"good_username" ,
"[email protected] " ],
270
+ ),
271
+ (
272
+ [
"new_username" ,
"[email protected] " ],
273
+ ["conf_dir" , "conf_dir/diffpyconfig.json" , "diffpyconfig.json" , "conf_dir/diffpyconfig.json" ],
274
+ [
"new_username" ,
"[email protected] " ,
"good_username" ,
"[email protected] " ],
275
+ ),
276
+ # Config file in home dir not in cwd, check username and email are properly loaded and config file is unchanged
277
+ (
278
+ ["" , "" ],
279
+ ["input_dir" , "input_dir/diffpyconfig.json" , "conf_dir/diffpyconfig.json" , "conf_dir/diffpyconfig.json" ],
280
+ [
"good_username" ,
"[email protected] " ,
"good_username" ,
"[email protected] " ],
281
+ ),
282
+ (
283
+ ["new_username" , "" ],
284
+ ["input_dir" , "input_dir/diffpyconfig.json" , "conf_dir/diffpyconfig.json" , "conf_dir/diffpyconfig.json" ],
285
+ [
"new_username" ,
"[email protected] " ,
"good_username" ,
"[email protected] " ],
286
+ ),
287
+ (
288
+
289
+ ["input_dir" , "input_dir/diffpyconfig.json" , "conf_dir/diffpyconfig.json" , "conf_dir/diffpyconfig.json" ],
290
+ [
"good_username" ,
"[email protected] " ,
"good_username" ,
"[email protected] " ],
291
+ ),
292
+ (
293
+ [
"new_username" ,
"[email protected] " ],
294
+ ["input_dir" , "input_dir/diffpyconfig.json" , "conf_dir/diffpyconfig.json" , "conf_dir/diffpyconfig.json" ],
295
+ [
"new_username" ,
"[email protected] " ,
"good_username" ,
"[email protected] " ],
296
+ ),
260
297
]
261
298
262
299
263
- @pytest .mark .parametrize ("inputs, expected" , params_user_info )
264
- def test_load_user_info (monkeypatch , inputs , expected , user_filesystem ):
265
- os .chdir (user_filesystem / "conf_dir" )
266
- expected_username , expected_email = expected
267
- input_username , input_email , input_config_file = inputs
268
- mock_prompt_user_info = iter ([input_username , input_email ])
300
+ @pytest .mark .parametrize ("inputs, paths, expected" , params_user_info )
301
+ def test_load_user_info (monkeypatch , inputs , paths , expected , user_filesystem ):
302
+ os .chdir (user_filesystem / paths [0 ])
303
+ expected_args_username , expected_args_email , expected_conf_username , expected_conf_email = expected
304
+ mock_prompt_user_info = iter (inputs )
269
305
monkeypatch .setattr ("builtins.input" , lambda _ : next (mock_prompt_user_info ))
306
+ monkeypatch .setattr ("diffpy.labpdfproc.user_config.CWD_CONFIG_PATH" , user_filesystem / paths [1 ])
307
+ monkeypatch .setattr ("diffpy.labpdfproc.user_config.HOME_CONFIG_PATH" , user_filesystem / paths [2 ])
270
308
271
309
cli_inputs = ["2.5" , "data.xy" ]
272
310
actual_args = get_args (cli_inputs )
273
- actual_args = load_user_info (actual_args , input_config_file )
311
+ actual_args = load_user_info (actual_args )
274
312
275
- assert actual_args .username == expected_username
276
- assert actual_args .email == expected_email
277
- with open (input_config_file , "r" ) as f :
313
+ assert actual_args .username == expected_args_username
314
+ assert actual_args .email == expected_args_email
315
+ with open (user_filesystem / paths [ 3 ] , "r" ) as f :
278
316
config_data = json .load (f )
279
- assert config_data == {"username" : expected_username , "email" : expected_email }
317
+ assert config_data == {"username" : expected_conf_username , "email" : expected_conf_email }
280
318
281
319
282
320
params_user_info_bad = [
283
321
# No valid username/email in config file (or no config file),
284
322
# and user didn't enter username/email the first time they were asked
285
- (["" , "" , "non_existing_file.json" ], "Please rerun the program and provide a username and email." ),
286
- ([
"" ,
"[email protected] " , "non_existing_file.json" ],
"Please rerun the program and provide a username." ),
287
- (["good_username" , "" , "non_existing_file.json" ], "Please rerun the program and provide an email." ),
323
+ (["" , "" ], "Please rerun the program and provide a username and email." ),
324
+ ([
"" ,
"[email protected] " ],
"Please rerun the program and provide a username." ),
325
+ (["good_username" , "" ], "Please rerun the program and provide an email." ),
288
326
# User entered an invalid email
289
- (
290
- ["good_username" , "bad_email" , "non_existing_file.json" ],
291
- "Please rerun the program and provide a valid email." ,
292
- ),
293
- (["good_username" , "bad_email" , "test.json" ], "Please rerun the program and provide a valid email." ),
327
+ (["good_username" , "bad_email" ], "Please rerun the program and provide a valid email." ),
294
328
]
295
329
296
330
297
331
@pytest .mark .parametrize ("inputs, msg" , params_user_info_bad )
298
332
def test_load_user_info_bad (monkeypatch , inputs , msg , user_filesystem ):
299
- os .chdir (user_filesystem / "conf_dir" )
300
- input_username , input_email , input_config_file = inputs
333
+ os .chdir (user_filesystem )
334
+ input_username , input_email = inputs
301
335
mock_prompt_user_info = iter ([input_username , input_email ])
302
336
monkeypatch .setattr ("builtins.input" , lambda _ : next (mock_prompt_user_info ))
337
+ monkeypatch .setattr ("diffpy.labpdfproc.user_config.CWD_CONFIG_PATH" , Path .cwd () / "diffpyconfig.json" )
338
+ monkeypatch .setattr ("diffpy.labpdfproc.user_config.HOME_CONFIG_PATH" , user_filesystem / "diffpyconfig.json" )
303
339
304
340
cli_inputs = ["2.5" , "data.xy" ]
305
341
actual_args = get_args (cli_inputs )
306
342
with pytest .raises (ValueError , match = msg [0 ]):
307
- actual_args = load_user_info (actual_args , config_file = input_config_file )
343
+ actual_args = load_user_info (actual_args )
0 commit comments