|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import re
|
3 | 4 | from pathlib import Path
|
|
7 | 8 | from diffpy.labpdfproc.labpdfprocapp import get_args
|
8 | 9 | from diffpy.labpdfproc.tools import (
|
9 | 10 | known_sources,
|
| 11 | + load_user_info, |
10 | 12 | load_user_metadata,
|
11 | 13 | set_input_lists,
|
12 | 14 | set_output_directory,
|
@@ -241,3 +243,119 @@ def test_load_user_metadata_bad(inputs, msg):
|
241 | 243 | actual_args = get_args(cli_inputs)
|
242 | 244 | with pytest.raises(ValueError, match=msg[0]):
|
243 | 245 | actual_args = load_user_metadata(actual_args)
|
| 246 | + |
| 247 | + |
| 248 | +def _setup_dirs(monkeypatch, user_filesystem): |
| 249 | + cwd = Path(user_filesystem) |
| 250 | + home_dir = cwd / "home_dir" |
| 251 | + monkeypatch.setattr("pathlib.Path.home", lambda _: home_dir) |
| 252 | + os.chdir(cwd) |
| 253 | + return home_dir |
| 254 | + |
| 255 | + |
| 256 | +def _run_tests(inputs, expected): |
| 257 | + expected_username, expected_email = expected |
| 258 | + cli_inputs = ["2.5", "data.xy", "--username", inputs[0], "--email", inputs[1]] |
| 259 | + actual_args = get_args(cli_inputs) |
| 260 | + actual_args = load_user_info(actual_args) |
| 261 | + assert actual_args.username == expected_username |
| 262 | + 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