Skip to content

Commit cab4e39

Browse files
authored
Fix IndexError when there are no suggestions (#189)
2 parents 27da2ae + d8be1c0 commit cab4e39

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/norwegianblue/__init__.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222

2323
BASE_URL = "https://endoflife.date/api/"
2424
USER_AGENT = f"norwegianblue/{__version__}"
25-
ERROR_404_TEXT = "Product '{}' not found, run 'eol all' for list. Did you mean: '{}'?"
25+
26+
27+
def error_404_text(product: str, suggestion: str) -> str:
28+
return f"Product '{product}' not found, run 'eol all' for list." + (
29+
f" Did you mean: '{suggestion}'?" if suggestion else ""
30+
)
2631

2732

2833
def norwegianblue(
@@ -62,7 +67,7 @@ def norwegianblue(
6267
logging.info("HTTP status code: %d", r.status_code)
6368
if r.status_code == 404:
6469
suggestion = suggest_product(product)
65-
msg = ERROR_404_TEXT.format(product, suggestion)
70+
msg = error_404_text(product, suggestion)
6671
raise ValueError(msg)
6772

6873
# Raise if we made a bad request
@@ -104,7 +109,7 @@ def suggest_product(product: str) -> str:
104109
# Find the closest match
105110
result = difflib.get_close_matches(product, all_products, n=1)
106111
logging.info("Suggestion:\t%s (score: %d)", *result)
107-
return result[0]
112+
return result[0] if result else ""
108113

109114

110115
def _ltsify(data: list[dict]) -> list[dict]:

src/norwegianblue/cli.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,19 @@ def main() -> None:
136136
show_title=multiple_products,
137137
)
138138
except ValueError as e:
139-
prompt = f"{e} [Y/n] "
139+
suggestion = norwegianblue.suggest_product(product)
140+
141+
prompt = f"{e}{' [Y/n] ' if suggestion else ''}"
140142
if args.color != "no":
141143
prompt = colored(prompt, "yellow")
144+
if not suggestion:
145+
print(prompt)
146+
print()
147+
continue
142148
answer = input(prompt)
143149
if answer not in ("", "y", "Y"):
144-
sys.exit()
145-
suggestion = norwegianblue.suggest_product(product)
150+
print()
151+
continue
146152
output = norwegianblue.norwegianblue(
147153
product=suggestion,
148154
format=args.formatter,

tests/test_norwegianblue.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -391,22 +391,29 @@ def test_all_products(self) -> None:
391391
assert output == expected
392392

393393
@respx.mock
394-
def test_404(self) -> None:
394+
@pytest.mark.parametrize(
395+
"product, expected",
396+
[
397+
(
398+
"androd",
399+
r"Product 'androd' not found, run 'eol all' for list\. "
400+
r"Did you mean: 'android'?",
401+
),
402+
("julia", r"Product 'julia' not found, run 'eol all' for list\."),
403+
],
404+
)
405+
def test_404(self, product, expected) -> None:
395406
# Arrange
396-
mocked_url = "https://endoflife.date/api/androd.json"
407+
mocked_url = f"https://endoflife.date/api/{product}.json"
397408
respx.get(mocked_url).respond(status_code=404)
398409

399410
mocked_url = "https://endoflife.date/api/all.json"
400411
mocked_response = SAMPLE_RESPONSE_ALL_JSON
401412
respx.get(mocked_url).respond(content=mocked_response)
402413

403414
# Act / Assert
404-
with pytest.raises(
405-
ValueError,
406-
match=r"Product 'androd' not found, run 'eol all' for list\. "
407-
r"Did you mean: 'android'?",
408-
):
409-
norwegianblue.norwegianblue(product="androd")
415+
with pytest.raises(ValueError, match=expected):
416+
norwegianblue.norwegianblue(product=product)
410417

411418
def test_norwegianblue_norwegianblue(self) -> None:
412419
# Act

0 commit comments

Comments
 (0)