diff --git a/src/norwegianblue/__init__.py b/src/norwegianblue/__init__.py index 060fed4..e9a312a 100644 --- a/src/norwegianblue/__init__.py +++ b/src/norwegianblue/__init__.py @@ -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( @@ -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 @@ -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]: diff --git a/src/norwegianblue/cli.py b/src/norwegianblue/cli.py index b1eef1c..975724d 100644 --- a/src/norwegianblue/cli.py +++ b/src/norwegianblue/cli.py @@ -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, diff --git a/tests/test_norwegianblue.py b/tests/test_norwegianblue.py index 37dbeec..d319e20 100644 --- a/tests/test_norwegianblue.py +++ b/tests/test_norwegianblue.py @@ -391,9 +391,20 @@ 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" @@ -401,12 +412,8 @@ def test_404(self) -> None: 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