-
Notifications
You must be signed in to change notification settings - Fork 11
load username and email #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33a0bd2
initial commit: added user_config and load_user_info, tests not writt…
yucongalicechen c2a838e
added tests for UC1 and UC4, rewrite function to overwrite config fil…
yucongalicechen 3967018
changed config file name to diffpyconfig
yucongalicechen b19f8b8
corrected file path for bad test cases
yucongalicechen d8b411c
updated tests and functions according to workflow
yucongalicechen bfbb4bb
reformatted tests
yucongalicechen 5519c54
reformatted tests to with/without config file
yucongalicechen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
@@ -8,6 +9,8 @@ def user_filesystem(tmp_path): | |
base_dir = Path(tmp_path) | ||
input_dir = base_dir / "input_dir" | ||
input_dir.mkdir(parents=True, exist_ok=True) | ||
conf_dir = base_dir / "conf_dir" | ||
conf_dir.mkdir(parents=True, exist_ok=True) | ||
sbillinge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
chi_data = "dataformat = twotheta\n mode = xray\n # chi_Q chi_I\n 1 2\n 3 4\n 5 6\n 7 8\n" | ||
xy_data = "1 2\n 3 4\n 5 6\n 7 8" | ||
|
@@ -44,4 +47,8 @@ def user_filesystem(tmp_path): | |
f.write("good_data.xy \n") | ||
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n") | ||
|
||
user_config_data = {"username": "good_username", "email": "[email protected]"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be |
||
with open(conf_dir / "diffpyconfig.json", "w") as f: | ||
json.dump(user_config_data, f) | ||
|
||
yield tmp_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import json | ||
import os | ||
import re | ||
from pathlib import Path | ||
|
@@ -7,6 +8,7 @@ | |
from diffpy.labpdfproc.labpdfprocapp import get_args | ||
from diffpy.labpdfproc.tools import ( | ||
known_sources, | ||
load_user_info, | ||
load_user_metadata, | ||
set_input_lists, | ||
set_output_directory, | ||
|
@@ -241,3 +243,100 @@ def test_load_user_metadata_bad(inputs, msg): | |
actual_args = get_args(cli_inputs) | ||
with pytest.raises(ValueError, match=msg[0]): | ||
actual_args = load_user_metadata(actual_args) | ||
|
||
|
||
params_user_info_without_conf_file = [ | ||
(["new_username", "[email protected]"], ["new_username", "[email protected]", "new_username", "[email protected]"]), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("inputs, expected", params_user_info_without_conf_file) | ||
def test_load_user_info_without_conf_file(monkeypatch, inputs, expected, user_filesystem): | ||
expected_args_username, expected_args_email, expected_conf_username, expected_conf_email = expected | ||
|
||
os.chdir(user_filesystem) | ||
input_user = iter(inputs) | ||
monkeypatch.setattr("builtins.input", lambda _: next(input_user)) | ||
monkeypatch.setattr("diffpy.labpdfproc.user_config.HOME_CONFIG_PATH", user_filesystem / "diffpyconfig.json") | ||
|
||
cli_inputs = ["2.5", "data.xy"] | ||
actual_args = get_args(cli_inputs) | ||
actual_args = load_user_info(actual_args) | ||
assert actual_args.username == expected_args_username | ||
assert actual_args.email == expected_args_email | ||
with open(user_filesystem / "diffpyconfig.json", "r") as f: | ||
config_data = json.load(f) | ||
assert config_data == {"username": expected_conf_username, "email": expected_conf_email} | ||
|
||
|
||
params_user_info_with_conf_file = [ | ||
(["", ""], ["good_username", "[email protected]", "good_username", "[email protected]"]), | ||
(["new_username", ""], ["new_username", "[email protected]", "good_username", "[email protected]"]), | ||
(["", "[email protected]"], ["good_username", "[email protected]", "good_username", "[email protected]"]), | ||
(["new_username", "[email protected]"], ["new_username", "[email protected]", "good_username", "[email protected]"]), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("inputs, expected", params_user_info_with_conf_file) | ||
def test_load_user_info_with_conf_file(monkeypatch, inputs, expected, user_filesystem): | ||
expected_args_username, expected_args_email, expected_conf_username, expected_conf_email = expected | ||
user_config_data = {"username": "good_username", "email": "[email protected]"} | ||
with open(user_filesystem / "diffpyconfig.json", "w") as f: | ||
json.dump(user_config_data, f) | ||
|
||
# test it works when config file is in current directory | ||
# check username and email are correctly loaded and config file is not modified | ||
os.chdir(user_filesystem / "conf_dir") | ||
input_user = iter(inputs) | ||
monkeypatch.setattr("builtins.input", lambda _: next(input_user)) | ||
monkeypatch.setattr("diffpy.labpdfproc.user_config.HOME_CONFIG_PATH", user_filesystem / "diffpyconfig.json") | ||
|
||
cli_inputs = ["2.5", "data.xy"] | ||
actual_args = get_args(cli_inputs) | ||
actual_args = load_user_info(actual_args) | ||
assert actual_args.username == expected_args_username | ||
assert actual_args.email == expected_args_email | ||
with open(Path.cwd() / "diffpyconfig.json", "r") as f: | ||
config_data = json.load(f) | ||
assert config_data == {"username": expected_conf_username, "email": expected_conf_email} | ||
|
||
# test it works when config file is in home directory and not in current directory new_dir | ||
# check username and email are correctly loaded and config file is not modified | ||
new_dir = user_filesystem / "new_dir" | ||
new_dir.mkdir(parents=True, exist_ok=True) | ||
os.chdir(new_dir) | ||
input_user = iter(inputs) | ||
monkeypatch.setattr("builtins.input", lambda _: next(input_user)) | ||
|
||
cli_inputs = ["2.5", "data.xy"] | ||
actual_args = get_args(cli_inputs) | ||
actual_args = load_user_info(actual_args) | ||
assert actual_args.username == expected_args_username | ||
assert actual_args.email == expected_args_email | ||
with open(user_filesystem / "diffpyconfig.json", "r") as f: | ||
config_data = json.load(f) | ||
assert config_data == {"username": expected_conf_username, "email": expected_conf_email} | ||
|
||
|
||
params_user_info_bad = [ | ||
# No valid username/email in config file (or no config file), | ||
# and user didn't enter username/email the first time they were asked | ||
(["", ""], "Please rerun the program and provide a username and email."), | ||
(["", "[email protected]"], "Please rerun the program and provide a username."), | ||
(["good_username", ""], "Please rerun the program and provide an email."), | ||
# User entered an invalid email | ||
(["good_username", "bad_email"], "Please rerun the program and provide a valid email."), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("inputs, msg", params_user_info_bad) | ||
def test_load_user_info_bad(monkeypatch, inputs, msg, user_filesystem): | ||
os.chdir(user_filesystem) | ||
input_user = iter(inputs) | ||
monkeypatch.setattr("builtins.input", lambda _: next(input_user)) | ||
monkeypatch.setattr("diffpy.labpdfproc.user_config.HOME_CONFIG_PATH", user_filesystem / "diffpyconfig.json") | ||
|
||
cli_inputs = ["2.5", "data.xy"] | ||
actual_args = get_args(cli_inputs) | ||
with pytest.raises(ValueError, match=msg[0]): | ||
actual_args = load_user_info(actual_args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
CONFIG_FILE = "diffpyconfig.json" | ||
CWD_CONFIG_PATH = Path.cwd() / CONFIG_FILE | ||
HOME_CONFIG_PATH = Path.home() / CONFIG_FILE | ||
|
||
|
||
def prompt_user_info(): | ||
username = input("Please enter your username (or press Enter to skip if you already entered before): ") | ||
email = input("Please enter your email (or press Enter to skip if you already entered before): ") | ||
return username, email | ||
|
||
|
||
def find_conf_file(): | ||
if CWD_CONFIG_PATH.exists() and CWD_CONFIG_PATH.is_file(): | ||
return CWD_CONFIG_PATH | ||
elif HOME_CONFIG_PATH.exists() and HOME_CONFIG_PATH.is_file(): | ||
return HOME_CONFIG_PATH | ||
return None | ||
|
||
|
||
def read_conf_file(): | ||
conf_file = find_conf_file() | ||
if conf_file: | ||
with open(conf_file, "r") as f: | ||
config = json.load(f) | ||
return config.get("username"), config.get("email") | ||
return None, None | ||
|
||
|
||
def write_conf_file(username, email): | ||
with open(HOME_CONFIG_PATH, "w") as f: | ||
json.dump({"username": username, "email": email}, f) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's call it
home_dir
instead ofconf_dir
to make the intent clearer.