|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
8 | | -from diffpy.utils.tools import get_package_info, get_user_info |
9 | | - |
10 | | -# def _setup_dirs(monkeypatch, user_filesystem): |
11 | | -# home_dir, cwd_dir = user_filesystem.home_dir, user_filesystem.cwd_dir |
12 | | -# os.chdir(cwd_dir) |
13 | | -# return home_dir |
14 | | -# |
15 | | - |
16 | | - |
17 | | -def _run_tests(inputs, expected): |
18 | | - args = {"username": inputs[0], "email": inputs[1]} |
19 | | - expected_username, expected_email = expected |
20 | | - config = get_user_info(args) |
21 | | - assert config.get("username") == expected_username |
22 | | - assert config.get("email") == expected_email |
23 | | - |
24 | | - |
25 | | -params_user_info_with_local_conf_file = [ |
26 | | - ([ "", ""], [ "cwd_username", "[email protected]"]), |
27 | | - ([ "cli_username", ""], [ "cli_username", "[email protected]"]), |
28 | | - |
29 | | - ([ None, None], [ "cwd_username", "[email protected]"]), |
30 | | - ([ "cli_username", None], [ "cli_username", "[email protected]"]), |
31 | | - ([ None, "[email protected]"], [ "cwd_username", "[email protected]"]), |
32 | | - ([ "cli_username", "[email protected]"], [ "cli_username", "[email protected]"]), |
33 | | -] |
34 | | -params_user_info_with_no_home_conf_file = [ |
35 | | - ( |
36 | | - [None, None], |
37 | | - [ "input_username", "[email protected]"], |
38 | | - [ "input_username", "[email protected]"], |
39 | | - ), |
40 | | - ( |
41 | | - ["cli_username", None], |
42 | | - |
43 | | - [ "cli_username", "[email protected]"], |
44 | | - ), |
45 | | - ( |
46 | | - |
47 | | - ["input_username", ""], |
48 | | - [ "input_username", "[email protected]"], |
49 | | - ), |
50 | | - ( |
51 | | - ["", ""], |
52 | | - [ "input_username", "[email protected]"], |
53 | | - [ "input_username", "[email protected]"], |
54 | | - ), |
55 | | - ( |
56 | | - ["cli_username", ""], |
57 | | - |
58 | | - [ "cli_username", "[email protected]"], |
59 | | - ), |
60 | | - ( |
61 | | - |
62 | | - ["input_username", ""], |
63 | | - [ "input_username", "[email protected]"], |
64 | | - ), |
65 | | - ( |
66 | | - [ "cli_username", "[email protected]"], |
67 | | - [ "input_username", "[email protected]"], |
68 | | - [ "cli_username", "[email protected]"], |
69 | | - ), |
70 | | -] |
71 | | -params_user_info_no_conf_file_no_inputs = [ |
72 | | - ([None, None], ["", ""], ["", ""]), |
73 | | -] |
| 8 | +from diffpy.utils.tools import check_and_build_global_config, get_package_info, get_user_info |
74 | 9 |
|
75 | 10 |
|
76 | 11 | @pytest.mark.parametrize( |
@@ -149,27 +84,63 @@ def test_get_user_info_with_local_conf_file(runtime_inputs, expected, user_files |
149 | 84 | assert actual == expected |
150 | 85 |
|
151 | 86 |
|
152 | | -# @pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_with_no_home_conf_file) |
153 | | -# def test_get_user_info_with_no_home_conf_file(monkeypatch, inputsa, inputsb, expected, user_filesystem): |
154 | | -# _setup_dirs(monkeypatch, user_filesystem) |
155 | | -# os.remove(Path().home() / "diffpyconfig.json") |
156 | | -# inp_iter = iter(inputsb) |
157 | | -# monkeypatch.setattr("builtins.input", lambda _: next(inp_iter)) |
158 | | -# _run_tests(inputsa, expected) |
159 | | -# confile = Path().home() / "diffpyconfig.json" |
160 | | -# assert confile.is_file() |
161 | | -# |
162 | | -# |
163 | | -# @pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_no_conf_file_no_inputs) |
164 | | -# def test_get_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, expected, user_filesystem): |
165 | | -# _setup_dirs(monkeypatch, user_filesystem) |
166 | | -# os.remove(Path().home() / "diffpyconfig.json") |
167 | | -# inp_iter = iter(inputsb) |
168 | | -# monkeypatch.setattr("builtins.input", lambda _: next(inp_iter)) |
169 | | -# _run_tests(inputsa, expected) |
170 | | -# confile = Path().home() / "diffpyconfig.json" |
171 | | -# assert confile.exists() is False |
172 | | -# |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "test_inputs,expected", |
| 89 | + [ # Check check_and_build_global_config() builds correct config when config is found missing |
| 90 | + ( # C1: user inputs valid name, email and orcid |
| 91 | + {"user_inputs": ["input_name", "[email protected]", "input_orcid"]}, |
| 92 | + {"owner_email": "[email protected]", "owner_orcid": "input_orcid", "owner_name": "input_name"}, |
| 93 | + ), |
| 94 | + ({"user_inputs": ["", "", ""]}, None), # C2: empty strings passed in, expect no config file created |
| 95 | + ( # C3: just username input, expect config file but with some empty values |
| 96 | + {"user_inputs": ["input_name", "", ""]}, |
| 97 | + {"owner_email": "", "owner_orcid": "", "owner_name": "input_name"}, |
| 98 | + ), |
| 99 | + ], |
| 100 | +) |
| 101 | +def test_check_and_build_global_config(test_inputs, expected, user_filesystem, mocker): |
| 102 | + # user_filesystem[0] is tmp_dir/home_dir with the global config file in it, user_filesystem[1] |
| 103 | + # is tmp_dir/cwd_dir |
| 104 | + mocker.patch.object(Path, "home", return_value=user_filesystem[0]) |
| 105 | + os.chdir(user_filesystem[1]) |
| 106 | + confile = user_filesystem[0] / "diffpyconfig.json" |
| 107 | + # remove the config file from home that came with user_filesystem |
| 108 | + os.remove(confile) |
| 109 | + mocker.patch("builtins.input", side_effect=test_inputs["user_inputs"]) |
| 110 | + actual_bool = check_and_build_global_config() |
| 111 | + try: |
| 112 | + with open(confile, "r") as f: |
| 113 | + actual = json.load(f) |
| 114 | + expected_bool = True |
| 115 | + except FileNotFoundError: |
| 116 | + expected_bool = False |
| 117 | + actual = None |
| 118 | + assert actual == expected |
| 119 | + assert actual_bool == expected_bool |
| 120 | + |
| 121 | + |
| 122 | +def test_check_and_build_global_config_file_exists(user_filesystem, mocker): |
| 123 | + mocker.patch.object(Path, "home", return_value=user_filesystem[0]) |
| 124 | + os.chdir(user_filesystem[1]) |
| 125 | + confile = user_filesystem[0] / "diffpyconfig.json" |
| 126 | + expected = { "owner_name": "home_ownername", "owner_email": "[email protected]", "owner_orcid": "home_orcid"} |
| 127 | + actual_bool = check_and_build_global_config() |
| 128 | + assert actual_bool is True |
| 129 | + with open(confile, "r") as f: |
| 130 | + actual = json.load(f) |
| 131 | + assert actual == expected |
| 132 | + |
| 133 | + |
| 134 | +def test_check_and_build_global_config_skipped(user_filesystem, mocker): |
| 135 | + mocker.patch.object(Path, "home", return_value=user_filesystem[0]) |
| 136 | + os.chdir(user_filesystem[1]) |
| 137 | + confile = user_filesystem[0] / "diffpyconfig.json" |
| 138 | + # remove the config file from home that came with user_filesystem |
| 139 | + os.remove(confile) |
| 140 | + actual_bool = check_and_build_global_config(skip_config_creation=True) |
| 141 | + assert actual_bool is False |
| 142 | + assert not confile.exists() |
| 143 | + |
173 | 144 |
|
174 | 145 | params_package_info = [ |
175 | 146 | (["diffpy.utils", None], {"package_info": {"diffpy.utils": "3.3.0"}}), |
|
0 commit comments