|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | | -from diffpy.utils.tools import get_package_info, get_user_info |
| 8 | +from diffpy.utils.tools import check_and_build_global_config, get_package_info, get_user_info |
9 | 9 |
|
10 | 10 |
|
11 | 11 | def _setup_dirs(monkeypatch, user_filesystem): |
@@ -123,6 +123,36 @@ def test_get_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, exp |
123 | 123 | assert confile.exists() is False |
124 | 124 |
|
125 | 125 |
|
| 126 | +@pytest.mark.parametrize( |
| 127 | + "test_inputs,expected", |
| 128 | + [ # Check check_and_build_global_config() builds correct config when config is found missing |
| 129 | + ( # C1: user inputs valid name, email and orcid |
| 130 | + {"user_inputs": ["input_name", "input@email.com", "input_orcid"]}, |
| 131 | + {"owner_email": "input@email.com", "owner_orcid": "input_orcid", "owner_name": "input_name"}, |
| 132 | + ), |
| 133 | + # ( # C2: empty strings passed in, expect uname, email, orcid from home_config |
| 134 | + # {"owner_name": "", "owner_email": "", "owner_orcid": ""}, |
| 135 | + # {"owner_name": "home_ownername", "owner_email": "home@email.com", "owner_orcid": "home_orcid"}, |
| 136 | + # ), |
| 137 | + ], |
| 138 | +) |
| 139 | +def test_check_and_build_global_config(test_inputs, expected, user_filesystem, mocker): |
| 140 | + # user_filesystem[0] is tmp_dir/home_dir with the global config file in it, user_filesystem[1] |
| 141 | + # is tmp_dir/cwd_dir |
| 142 | + mocker.patch.object(Path, "home", return_value=user_filesystem[0]) |
| 143 | + os.chdir(user_filesystem[1]) |
| 144 | + # remove the config file from home that came with user_filesystem |
| 145 | + old_confile = user_filesystem[0] / "diffpyconfig.json" |
| 146 | + os.remove(old_confile) |
| 147 | + check_and_build_global_config() |
| 148 | + inp_iter = iter(test_inputs["user_inputs"]) |
| 149 | + mocker.patch("builtins.input", lambda _: next(inp_iter)) |
| 150 | + with open(old_confile, "r") as f: |
| 151 | + actual = json.load(f) |
| 152 | + print(actual) |
| 153 | + assert actual == expected |
| 154 | + |
| 155 | + |
126 | 156 | params_package_info = [ |
127 | 157 | (["diffpy.utils", None], {"package_info": {"diffpy.utils": "3.3.0"}}), |
128 | 158 | (["package1", None], {"package_info": {"package1": "1.2.3", "diffpy.utils": "3.3.0"}}), |
|
0 commit comments