Skip to content

Commit f064217

Browse files
committed
initial commit of tests for the config updater.
I need to get the updated user_filesystem fixture so merging main next
1 parent c67fec3 commit f064217

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

news/configupdate.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* no news added: covered in the news from the get_user_info work
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/tools.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,13 @@ def get_user_info(args=None):
130130
if config_bool is False:
131131
os.remove(Path().home() / "diffpyconfig.json")
132132
config = {"username": "", "email": ""}
133-
134133
return config
135134

136135

136+
def check_and_build_global_config():
137+
return
138+
139+
137140
def get_package_info(package_names, metadata=None):
138141
"""
139142
Fetches package version and updates it into (given) metadata.

tests/test_tools.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

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
99

1010

1111
def _setup_dirs(monkeypatch, user_filesystem):
@@ -123,6 +123,36 @@ def test_get_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, exp
123123
assert confile.exists() is False
124124

125125

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+
126156
params_package_info = [
127157
(["diffpy.utils", None], {"package_info": {"diffpy.utils": "3.3.0"}}),
128158
(["package1", None], {"package_info": {"package1": "1.2.3", "diffpy.utils": "3.3.0"}}),

0 commit comments

Comments
 (0)