Skip to content
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

Fix IndexError when there are no suggestions #189

Merged
merged 4 commits into from
Jan 24, 2024
Merged
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
11 changes: 8 additions & 3 deletions src/norwegianblue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@

BASE_URL = "https://endoflife.date/api/"
USER_AGENT = f"norwegianblue/{__version__}"
ERROR_404_TEXT = "Product '{}' not found, run 'eol all' for list. Did you mean: '{}'?"


def error_404_text(product: str, suggestion: str) -> str:
return f"Product '{product}' not found, run 'eol all' for list." + (
f" Did you mean: '{suggestion}'?" if suggestion else ""
)


def norwegianblue(
Expand Down Expand Up @@ -62,7 +67,7 @@ def norwegianblue(
logging.info("HTTP status code: %d", r.status_code)
if r.status_code == 404:
suggestion = suggest_product(product)
msg = ERROR_404_TEXT.format(product, suggestion)
msg = error_404_text(product, suggestion)
raise ValueError(msg)

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


def _ltsify(data: list[dict]) -> list[dict]:
Expand Down
12 changes: 9 additions & 3 deletions src/norwegianblue/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,19 @@ def main() -> None:
show_title=multiple_products,
)
except ValueError as e:
prompt = f"{e} [Y/n] "
suggestion = norwegianblue.suggest_product(product)

prompt = f"{e}{' [Y/n] ' if suggestion else ''}"
if args.color != "no":
prompt = colored(prompt, "yellow")
if not suggestion:
print(prompt)
print()
continue
answer = input(prompt)
if answer not in ("", "y", "Y"):
sys.exit()
suggestion = norwegianblue.suggest_product(product)
print()
continue
output = norwegianblue.norwegianblue(
product=suggestion,
format=args.formatter,
Expand Down
23 changes: 15 additions & 8 deletions tests/test_norwegianblue.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,22 +391,29 @@ def test_all_products(self) -> None:
assert output == expected

@respx.mock
def test_404(self) -> None:
@pytest.mark.parametrize(
"product, expected",
[
(
"androd",
r"Product 'androd' not found, run 'eol all' for list\. "
r"Did you mean: 'android'?",
),
("julia", r"Product 'julia' not found, run 'eol all' for list\."),
],
)
def test_404(self, product, expected) -> None:
# Arrange
mocked_url = "https://endoflife.date/api/androd.json"
mocked_url = f"https://endoflife.date/api/{product}.json"
respx.get(mocked_url).respond(status_code=404)

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

# Act / Assert
with pytest.raises(
ValueError,
match=r"Product 'androd' not found, run 'eol all' for list\. "
r"Did you mean: 'android'?",
):
norwegianblue.norwegianblue(product="androd")
with pytest.raises(ValueError, match=expected):
norwegianblue.norwegianblue(product=product)

def test_norwegianblue_norwegianblue(self) -> None:
# Act
Expand Down
Loading