Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions bib_lookup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def _handle_config(config_arg: str) -> None:
else:
config_path = Path(config_arg)
if not config_path.is_file():
raise ValueError(f"Configuration file ``{config_arg}`` does not exist. Please check and try again.")
print(f"Error: Configuration file ``{config_arg}`` does not exist. Please check and try again.")
sys.exit(1)

if config_path.suffix == ".json":
config = json.loads(config_path.read_text())
Expand Down Expand Up @@ -305,8 +306,8 @@ def main():
)
parser.add_argument(
"--timeout",
type=int,
default=6,
type=float,
default=6.0,
help="Timeout for the lookup request. Unit is seconds. Default is 6 seconds.",
Comment on lines 308 to 311
dest="timeout",
)
Expand All @@ -329,9 +330,10 @@ def main():
)
parser.add_argument(
"--style",
type=str,
type=str.lower,
help="Style of the output, valid only when 'format' is 'text', optional.",
dest="style",
choices=sorted(BibLookup._get_supported_styles().keys()),
)
parser.add_argument(
"--verbose",
Expand Down Expand Up @@ -424,7 +426,9 @@ def main():
bl.check_bib_file(check_file)
return
else:
assert len(args["identifiers"]) > 0 or args["input_file"] is not None, "No identifiers given."
if len(args["identifiers"]) == 0 and args["input_file"] is None:
print("Error: No identifiers given.")
sys.exit(1)

extra_kw = {}
if args["format"] is not None and args["format"] not in ["bibtex", "bibentry"]:
Expand Down
40 changes: 40 additions & 0 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,46 @@ def test_cli():
exitcode, output_msg = execute_cmd(cmd)
assert not _CONFIG_FILE.exists()

# --- test --config with non-existent config file fails with exit code 1 ---
cmd = "bib-lookup --config /nonexistent/path/to/config.json"
exitcode, output_msg = execute_cmd(cmd, raise_error=False)
assert exitcode == 1

# --- test bib-lookup with no identifiers and no input file fails with exit code 1 ---
cmd = "bib-lookup"
exitcode, output_msg = execute_cmd(cmd, raise_error=False)
assert exitcode == 1
output_text = "".join(output_msg)
assert "No identifiers" in output_text or "Error" in output_text

# --- test --style with invalid value is rejected by argparse ---
cmd = "bib-lookup 10.1109/CVPR.2016.90 --format text --style totally_invalid_style"
exitcode, output_msg = execute_cmd(cmd, raise_error=False)
assert exitcode != 0 # argparse exits with code 2 for invalid choice

# --- test --style with uppercase value is accepted (type=str.lower) ---
cmd = "bib-lookup 10.1142/S1005386718000305 --format text --style APA --timeout 10 --ignore-errors"
exitcode, output_msg = execute_cmd(cmd)
assert exitcode == 0

# --- test that --timeout accepts float values ---
cmd = "bib-lookup --config 'timeout=7.5'"
exitcode, output_msg = execute_cmd(cmd)
assert exitcode == 0
assert _CONFIG_FILE.exists()
user_config = json.loads(_CONFIG_FILE.read_text())
# _parse_config_value stores as string; BibLookup coerces to float on read
assert user_config["timeout"] == "7.5", f"timeout not stored correctly: {user_config['timeout']}"
# Verify BibLookup correctly reads and coerces the float
from bib_lookup import BibLookup

bl = BibLookup()
assert bl.timeout == 7.5, f"timeout not coerced to float: {bl.timeout}"
# clean up
cmd = "bib-lookup --config reset"
exitcode, output_msg = execute_cmd(cmd)
assert not _CONFIG_FILE.exists()

# --- test that --config warns on unknown keys (instead of silent discard) ---
if _CONFIG_FILE.exists():
_CONFIG_FILE.rename(_CONFIG_FILE.with_suffix(".bak"))
Expand Down
Loading