Skip to content

Commit d8b411c

Browse files
updated tests and functions according to workflow
1 parent b19f8b8 commit d8b411c

File tree

4 files changed

+95
-48
lines changed

4 files changed

+95
-48
lines changed

src/diffpy/labpdfproc/tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ def user_filesystem(tmp_path):
4848
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")
4949

5050
user_config_data = {"username": "good_username", "email": "[email protected]"}
51-
with open(conf_dir / "test.json", "w") as f:
51+
with open(conf_dir / "diffpyconfig.json", "w") as f:
5252
json.dump(user_config_data, f)
53-
with open(conf_dir / "test2.json", "w") as f:
54-
json.dump({}, f)
5553

5654
yield tmp_path

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -245,63 +245,99 @@ def test_load_user_metadata_bad(inputs, msg):
245245
actual_args = load_user_metadata(actual_args)
246246

247247

248-
# For each test case, check username and email are properly loaded and written into config file
249248
params_user_info = [
250-
# No existing config file, user enters valid username and email
251-
(["good_username", "[email protected]", "non_existing_file.json"], ["good_username", "[email protected]"]),
252-
# There exists a config file, so we read it and fetch info, no user input is fine
253-
(["", "", "test.json"], ["good_username", "[email protected]"]),
254-
# There exists a config file, user input username/email overwrites the username/email in the file
255-
(["new_username", "", "test.json"], ["new_username", "[email protected]"]),
256-
(["", "[email protected]", "test.json"], ["good_username", "[email protected]"]),
257-
(["new_username", "[email protected]", "test.json"], ["new_username", "[email protected]"]),
258-
# There exists a config file that does not contain username/email, then we write user inputs into config file
259-
(["good_username", "[email protected]", "test2.json"], ["good_username", "[email protected]"]),
249+
# No config file, check username and email are properly loaded and config file in ~ is created and written
250+
(
251+
["new_username", "[email protected]"],
252+
["input_dir", "input_dir/diffpyconfig.json", "diffpyconfig.json", "diffpyconfig.json"],
253+
["new_username", "[email protected]", "new_username", "[email protected]"],
254+
),
255+
# Config file in cwd, check username and email are properly loaded and config file is unchanged
256+
(
257+
["", ""],
258+
["conf_dir", "conf_dir/diffpyconfig.json", "diffpyconfig.json", "conf_dir/diffpyconfig.json"],
259+
["good_username", "[email protected]", "good_username", "[email protected]"],
260+
),
261+
(
262+
["new_username", ""],
263+
["conf_dir", "conf_dir/diffpyconfig.json", "diffpyconfig.json", "conf_dir/diffpyconfig.json"],
264+
["new_username", "[email protected]", "good_username", "[email protected]"],
265+
),
266+
(
267+
268+
["conf_dir", "conf_dir/diffpyconfig.json", "diffpyconfig.json", "conf_dir/diffpyconfig.json"],
269+
["good_username", "[email protected]", "good_username", "[email protected]"],
270+
),
271+
(
272+
["new_username", "[email protected]"],
273+
["conf_dir", "conf_dir/diffpyconfig.json", "diffpyconfig.json", "conf_dir/diffpyconfig.json"],
274+
["new_username", "[email protected]", "good_username", "[email protected]"],
275+
),
276+
# Config file in home dir not in cwd, check username and email are properly loaded and config file is unchanged
277+
(
278+
["", ""],
279+
["input_dir", "input_dir/diffpyconfig.json", "conf_dir/diffpyconfig.json", "conf_dir/diffpyconfig.json"],
280+
["good_username", "[email protected]", "good_username", "[email protected]"],
281+
),
282+
(
283+
["new_username", ""],
284+
["input_dir", "input_dir/diffpyconfig.json", "conf_dir/diffpyconfig.json", "conf_dir/diffpyconfig.json"],
285+
["new_username", "[email protected]", "good_username", "[email protected]"],
286+
),
287+
(
288+
289+
["input_dir", "input_dir/diffpyconfig.json", "conf_dir/diffpyconfig.json", "conf_dir/diffpyconfig.json"],
290+
["good_username", "[email protected]", "good_username", "[email protected]"],
291+
),
292+
(
293+
["new_username", "[email protected]"],
294+
["input_dir", "input_dir/diffpyconfig.json", "conf_dir/diffpyconfig.json", "conf_dir/diffpyconfig.json"],
295+
["new_username", "[email protected]", "good_username", "[email protected]"],
296+
),
260297
]
261298

262299

263-
@pytest.mark.parametrize("inputs, expected", params_user_info)
264-
def test_load_user_info(monkeypatch, inputs, expected, user_filesystem):
265-
os.chdir(user_filesystem / "conf_dir")
266-
expected_username, expected_email = expected
267-
input_username, input_email, input_config_file = inputs
268-
mock_prompt_user_info = iter([input_username, input_email])
300+
@pytest.mark.parametrize("inputs, paths, expected", params_user_info)
301+
def test_load_user_info(monkeypatch, inputs, paths, expected, user_filesystem):
302+
os.chdir(user_filesystem / paths[0])
303+
expected_args_username, expected_args_email, expected_conf_username, expected_conf_email = expected
304+
mock_prompt_user_info = iter(inputs)
269305
monkeypatch.setattr("builtins.input", lambda _: next(mock_prompt_user_info))
306+
monkeypatch.setattr("diffpy.labpdfproc.user_config.CWD_CONFIG_PATH", user_filesystem / paths[1])
307+
monkeypatch.setattr("diffpy.labpdfproc.user_config.HOME_CONFIG_PATH", user_filesystem / paths[2])
270308

271309
cli_inputs = ["2.5", "data.xy"]
272310
actual_args = get_args(cli_inputs)
273-
actual_args = load_user_info(actual_args, input_config_file)
311+
actual_args = load_user_info(actual_args)
274312

275-
assert actual_args.username == expected_username
276-
assert actual_args.email == expected_email
277-
with open(input_config_file, "r") as f:
313+
assert actual_args.username == expected_args_username
314+
assert actual_args.email == expected_args_email
315+
with open(user_filesystem / paths[3], "r") as f:
278316
config_data = json.load(f)
279-
assert config_data == {"username": expected_username, "email": expected_email}
317+
assert config_data == {"username": expected_conf_username, "email": expected_conf_email}
280318

281319

282320
params_user_info_bad = [
283321
# No valid username/email in config file (or no config file),
284322
# and user didn't enter username/email the first time they were asked
285-
(["", "", "non_existing_file.json"], "Please rerun the program and provide a username and email."),
286-
(["", "[email protected]", "non_existing_file.json"], "Please rerun the program and provide a username."),
287-
(["good_username", "", "non_existing_file.json"], "Please rerun the program and provide an email."),
323+
(["", ""], "Please rerun the program and provide a username and email."),
324+
(["", "[email protected]"], "Please rerun the program and provide a username."),
325+
(["good_username", ""], "Please rerun the program and provide an email."),
288326
# User entered an invalid email
289-
(
290-
["good_username", "bad_email", "non_existing_file.json"],
291-
"Please rerun the program and provide a valid email.",
292-
),
293-
(["good_username", "bad_email", "test.json"], "Please rerun the program and provide a valid email."),
327+
(["good_username", "bad_email"], "Please rerun the program and provide a valid email."),
294328
]
295329

296330

297331
@pytest.mark.parametrize("inputs, msg", params_user_info_bad)
298332
def test_load_user_info_bad(monkeypatch, inputs, msg, user_filesystem):
299-
os.chdir(user_filesystem / "conf_dir")
300-
input_username, input_email, input_config_file = inputs
333+
os.chdir(user_filesystem)
334+
input_username, input_email = inputs
301335
mock_prompt_user_info = iter([input_username, input_email])
302336
monkeypatch.setattr("builtins.input", lambda _: next(mock_prompt_user_info))
337+
monkeypatch.setattr("diffpy.labpdfproc.user_config.CWD_CONFIG_PATH", Path.cwd() / "diffpyconfig.json")
338+
monkeypatch.setattr("diffpy.labpdfproc.user_config.HOME_CONFIG_PATH", user_filesystem / "diffpyconfig.json")
303339

304340
cli_inputs = ["2.5", "data.xy"]
305341
actual_args = get_args(cli_inputs)
306342
with pytest.raises(ValueError, match=msg[0]):
307-
actual_args = load_user_info(actual_args, config_file=input_config_file)
343+
actual_args = load_user_info(actual_args)

src/diffpy/labpdfproc/tools.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pathlib import Path
22

3-
from diffpy.labpdfproc.user_config import CONFIG_FILE, prompt_user_info, read_conf_file, write_conf_file
3+
from diffpy.labpdfproc.user_config import prompt_user_info, read_conf_file, write_conf_file
44

55
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
66
known_sources = [key for key in WAVELENGTHS.keys()]
@@ -175,13 +175,14 @@ def load_user_metadata(args):
175175
return args
176176

177177

178-
def load_user_info(args, config_file=CONFIG_FILE):
178+
def load_user_info(args):
179179
"""
180180
Load username and email into args.
181181
182-
Prompt the user to enter username and email. If not provided, read from the config file.
182+
Prompt the user to enter username and email.
183+
If not provided, read from the config file (first from cwd, and then from home directory).
183184
If neither are available, raise a ValueError.
184-
Save provided values to the config file (overwriting existing values if needed).
185+
Save provided values to the config file if a config file doesn't exist.
185186
186187
Parameters
187188
----------
@@ -194,7 +195,7 @@ def load_user_info(args, config_file=CONFIG_FILE):
194195
195196
"""
196197
input_username, input_email = prompt_user_info()
197-
conf_username, conf_email = read_conf_file(config_file)
198+
conf_username, conf_email = read_conf_file()
198199

199200
no_username = not input_username and not conf_username
200201
no_email = not input_email and not conf_email
@@ -212,5 +213,7 @@ def load_user_info(args, config_file=CONFIG_FILE):
212213

213214
setattr(args, "username", username)
214215
setattr(args, "email", email)
215-
write_conf_file(username, email, config_file)
216+
217+
if not conf_username and not conf_email:
218+
write_conf_file(username, email)
216219
return args

src/diffpy/labpdfproc/user_config.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from pathlib import Path
33

44
CONFIG_FILE = "diffpyconfig.json"
5+
CWD_CONFIG_PATH = Path.cwd() / CONFIG_FILE
6+
HOME_CONFIG_PATH = Path.home() / CONFIG_FILE
57

68

79
def prompt_user_info():
@@ -10,15 +12,23 @@ def prompt_user_info():
1012
return username, email
1113

1214

13-
def read_conf_file(config_file=CONFIG_FILE):
14-
config_path = Path(config_file).resolve()
15-
if config_path.exists() and config_path.is_file():
16-
with open(config_file, "r") as f:
15+
def find_conf_file():
16+
if CWD_CONFIG_PATH.exists() and CWD_CONFIG_PATH.is_file():
17+
return CWD_CONFIG_PATH
18+
elif HOME_CONFIG_PATH.exists() and HOME_CONFIG_PATH.is_file():
19+
return HOME_CONFIG_PATH
20+
return None
21+
22+
23+
def read_conf_file():
24+
conf_file = find_conf_file()
25+
if conf_file:
26+
with open(conf_file, "r") as f:
1727
config = json.load(f)
1828
return config.get("username"), config.get("email")
1929
return None, None
2030

2131

22-
def write_conf_file(username, email, config_file=CONFIG_FILE):
23-
with open(config_file, "w") as f:
32+
def write_conf_file(username, email):
33+
with open(HOME_CONFIG_PATH, "w") as f:
2434
json.dump({"username": username, "email": email}, f)

0 commit comments

Comments
 (0)