|
1 |
| -import json |
2 | 1 | import os
|
3 | 2 | import re
|
4 | 3 | from pathlib import Path
|
@@ -245,117 +244,24 @@ def test_load_user_metadata_bad(inputs, msg):
|
245 | 244 | actual_args = load_user_metadata(actual_args)
|
246 | 245 |
|
247 | 246 |
|
248 |
| -def _setup_dirs(monkeypatch, user_filesystem): |
| 247 | +params_user_info = [ |
| 248 | + ([ None, None], [ "home_username", "[email protected]"]), |
| 249 | + ([ "cli_username", None], [ "cli_username", "[email protected]"]), |
| 250 | + ([ None, "[email protected]"], [ "home_username", "[email protected]"]), |
| 251 | + ([ "cli_username", "[email protected]"], [ "cli_username", "[email protected]"]), |
| 252 | +] |
| 253 | + |
| 254 | + |
| 255 | +@pytest.mark.parametrize("inputs, expected", params_user_info) |
| 256 | +def test_load_user_info(monkeypatch, inputs, expected, user_filesystem): |
249 | 257 | cwd = Path(user_filesystem)
|
250 | 258 | home_dir = cwd / "home_dir"
|
251 | 259 | monkeypatch.setattr("pathlib.Path.home", lambda _: home_dir)
|
252 | 260 | os.chdir(cwd)
|
253 |
| - return home_dir |
254 | 261 |
|
255 |
| - |
256 |
| -def _run_tests(inputs, expected): |
257 | 262 | expected_username, expected_email = expected
|
258 | 263 | cli_inputs = ["2.5", "data.xy", "--username", inputs[0], "--email", inputs[1]]
|
259 | 264 | actual_args = get_args(cli_inputs)
|
260 | 265 | actual_args = load_user_info(actual_args)
|
261 | 266 | assert actual_args.username == expected_username
|
262 | 267 | assert actual_args.email == expected_email
|
263 |
| - |
264 |
| - |
265 |
| -params_user_info_with_home_conf_file = [ |
266 |
| - ([ "", ""], [ "home_username", "[email protected]"]), |
267 |
| - ([ "cli_username", ""], [ "cli_username", "[email protected]"]), |
268 |
| - ([ "", "[email protected]"], [ "home_username", "[email protected]"]), |
269 |
| - ([ None, None], [ "home_username", "[email protected]"]), |
270 |
| - ([ "cli_username", None], [ "cli_username", "[email protected]"]), |
271 |
| - ([ None, "[email protected]"], [ "home_username", "[email protected]"]), |
272 |
| - ([ "cli_username", "[email protected]"], [ "cli_username", "[email protected]"]), |
273 |
| -] |
274 |
| -params_user_info_with_local_conf_file = [ |
275 |
| - ([ "", ""], [ "cwd_username", "[email protected]"]), |
276 |
| - ([ "cli_username", ""], [ "cli_username", "[email protected]"]), |
277 |
| - |
278 |
| - ([ None, None], [ "cwd_username", "[email protected]"]), |
279 |
| - ([ "cli_username", None], [ "cli_username", "[email protected]"]), |
280 |
| - ([ None, "[email protected]"], [ "cwd_username", "[email protected]"]), |
281 |
| - ([ "cli_username", "[email protected]"], [ "cli_username", "[email protected]"]), |
282 |
| -] |
283 |
| -params_user_info_with_no_home_conf_file = [ |
284 |
| - ( |
285 |
| - [None, None], |
286 |
| - [ "input_username", "[email protected]"], |
287 |
| - [ "input_username", "[email protected]"], |
288 |
| - ), |
289 |
| - ( |
290 |
| - ["cli_username", None], |
291 |
| - |
292 |
| - [ "cli_username", "[email protected]"], |
293 |
| - ), |
294 |
| - ( |
295 |
| - |
296 |
| - ["input_username", ""], |
297 |
| - [ "input_username", "[email protected]"], |
298 |
| - ), |
299 |
| - ( |
300 |
| - ["", ""], |
301 |
| - [ "input_username", "[email protected]"], |
302 |
| - [ "input_username", "[email protected]"], |
303 |
| - ), |
304 |
| - ( |
305 |
| - ["cli_username", ""], |
306 |
| - |
307 |
| - [ "cli_username", "[email protected]"], |
308 |
| - ), |
309 |
| - ( |
310 |
| - |
311 |
| - ["input_username", ""], |
312 |
| - [ "input_username", "[email protected]"], |
313 |
| - ), |
314 |
| - ( |
315 |
| - [ "cli_username", "[email protected]"], |
316 |
| - [ "input_username", "[email protected]"], |
317 |
| - [ "cli_username", "[email protected]"], |
318 |
| - ), |
319 |
| -] |
320 |
| -params_user_info_no_conf_file_no_inputs = [ |
321 |
| - ([None, None], ["", ""], ["", ""]), |
322 |
| -] |
323 |
| - |
324 |
| - |
325 |
| -@pytest.mark.parametrize("inputs, expected", params_user_info_with_home_conf_file) |
326 |
| -def test_load_user_info_with_home_conf_file(monkeypatch, inputs, expected, user_filesystem): |
327 |
| - _setup_dirs(monkeypatch, user_filesystem) |
328 |
| - _run_tests(inputs, expected) |
329 |
| - |
330 |
| - |
331 |
| -@pytest.mark.parametrize("inputs, expected", params_user_info_with_local_conf_file) |
332 |
| -def test_load_user_info_with_local_conf_file(monkeypatch, inputs, expected, user_filesystem): |
333 |
| - _setup_dirs(monkeypatch, user_filesystem) |
334 |
| - local_config_data = { "username": "cwd_username", "email": "[email protected]"} |
335 |
| - with open(Path(user_filesystem) / "diffpyconfig.json", "w") as f: |
336 |
| - json.dump(local_config_data, f) |
337 |
| - _run_tests(inputs, expected) |
338 |
| - os.remove(Path().home() / "diffpyconfig.json") |
339 |
| - _run_tests(inputs, expected) |
340 |
| - |
341 |
| - |
342 |
| -@pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_with_no_home_conf_file) |
343 |
| -def test_load_user_info_with_no_home_conf_file(monkeypatch, inputsa, inputsb, expected, user_filesystem): |
344 |
| - _setup_dirs(monkeypatch, user_filesystem) |
345 |
| - os.remove(Path().home() / "diffpyconfig.json") |
346 |
| - inp_iter = iter(inputsb) |
347 |
| - monkeypatch.setattr("builtins.input", lambda _: next(inp_iter)) |
348 |
| - _run_tests(inputsa, expected) |
349 |
| - confile = Path().home() / "diffpyconfig.json" |
350 |
| - assert confile.is_file() |
351 |
| - |
352 |
| - |
353 |
| -@pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_no_conf_file_no_inputs) |
354 |
| -def test_load_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, expected, user_filesystem): |
355 |
| - _setup_dirs(monkeypatch, user_filesystem) |
356 |
| - os.remove(Path().home() / "diffpyconfig.json") |
357 |
| - inp_iter = iter(inputsb) |
358 |
| - monkeypatch.setattr("builtins.input", lambda _: next(inp_iter)) |
359 |
| - _run_tests(inputsa, expected) |
360 |
| - confile = Path().home() / "diffpyconfig.json" |
361 |
| - assert confile.exists() is False |
0 commit comments