Skip to main content

Add a test for incorrect arguments

ID
0da3ebc
date
2024-05-16 20:31:44+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
be6fa4a
message
Add a test for incorrect arguments
changed files
2 files, 24 additions, 1 deletion

Changed files

save_safari_webarchive.swift (3400) → save_safari_webarchive.swift (3410)

diff --git a/save_safari_webarchive.swift b/save_safari_webarchive.swift
index 4e60955..0bbf451 100755
--- a/save_safari_webarchive.swift
+++ b/save_safari_webarchive.swift
@@ -111,7 +111,7 @@ extension WKWebView {
 }
 
 guard CommandLine.arguments.count == 3 else {
-    print("Usage: \(CommandLine.arguments[0]) <URL> <OUTPUT_PATH>")
+    fputs("Usage: \(CommandLine.arguments[0]) <URL> <OUTPUT_PATH>\n", stderr)
     exit(1)
 }
 

tests/test_save_safari_webarchive.py (1129) → tests/test_save_safari_webarchive.py (1759)

diff --git a/tests/test_save_safari_webarchive.py b/tests/test_save_safari_webarchive.py
index 7d29f6e..173dba8 100755
--- a/tests/test_save_safari_webarchive.py
+++ b/tests/test_save_safari_webarchive.py
@@ -2,6 +2,8 @@
 
 import pathlib
 
+import pytest
+
 from utils import save_safari_webarchive
 
 
@@ -33,3 +35,24 @@ def test_does_not_overwrite_existing_archive(tmp_path: pathlib.Path) -> None:
     )
 
     assert out_path.read_text() == "This should still be here later"
+
+
+@pytest.mark.parametrize(
+    "argv",
+    [
+        pytest.param([], id="no_arguments"),
+        pytest.param(["https://example.com"], id="not_enough_arguments"),
+        pytest.param(
+            ["https://example.com", "example.webarchive", "--debug"],
+            id="too_many_arguments",
+        ),
+    ],
+)
+def test_it_fails_if_you_supply_the_wrong_arguments(argv: list[str]) -> None:
+    result = save_safari_webarchive(argv)
+
+    assert result["returncode"] == 1
+    assert result["stdout"] is None
+    assert (
+        result["stderr"] == "Usage: save_safari_webarchive.swift <URL> <OUTPUT_PATH>\n"
+    )