Skip to main content

Add a basic set of tests

ID
c3b6b37
date
2024-05-16 20:27:14+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
815196c
message
Add a basic set of tests
changed files
5 files, 62 additions, 3 deletions

Changed files

.gitattributes (47) → .gitattributes (0)

diff --git a/.gitattributes b/.gitattributes
deleted file mode 100644
index c226feb..0000000
--- a/.gitattributes
+++ /dev/null
@@ -1 +0,0 @@
-save_safari_webarchive linguist-language=Swift

.gitignore (6) → .gitignore (19)

diff --git a/.gitignore b/.gitignore
index 0d20b64..d080851 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 *.pyc
+*.webarchive

save_safari_webarchive (3400) → save_safari_webarchive.swift (3400)

diff --git a/save_safari_webarchive b/save_safari_webarchive.swift
similarity index 100%
rename from save_safari_webarchive
rename to save_safari_webarchive.swift

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

diff --git a/tests/test_save_safari_webarchive.py b/tests/test_save_safari_webarchive.py
index b0ec0b3..7d29f6e 100755
--- a/tests/test_save_safari_webarchive.py
+++ b/tests/test_save_safari_webarchive.py
@@ -1,5 +1,35 @@
 #!/usr/bin/env python3
 
+import pathlib
 
-def test_truth():
-    assert True
+from utils import save_safari_webarchive
+
+
+def test_creates_a_single_archive(tmp_path: pathlib.Path) -> None:
+    out_path = tmp_path / "example.webarchive"
+    assert not out_path.exists()
+
+    result = save_safari_webarchive(["https://example.com", str(out_path)])
+
+    assert result["returncode"] == 0
+    assert result["stdout"] is not None
+    assert result["stderr"] is None
+    assert out_path.exists()
+
+
+def test_does_not_overwrite_existing_archive(tmp_path: pathlib.Path) -> None:
+    out_path = tmp_path / "example.webarchive"
+    out_path.write_text("This should still be here later")
+
+    result = save_safari_webarchive(["https://example.com", str(out_path)])
+
+    assert result["returncode"] == 1
+    assert result["stdout"] is None
+    assert result["stderr"] == (
+        "Unable to save webarchive file: "
+        "The file “example.webarchive” couldn’t be saved in the folder "
+        "“test_does_not_overwrite_existi0” because a file with "
+        "the same name already exists.\n"
+    )
+
+    assert out_path.read_text() == "This should still be here later"

tests/utils.py (0) → tests/utils.py (665)

diff --git a/tests/utils.py b/tests/utils.py
new file mode 100644
index 0000000..f74ec36
--- /dev/null
+++ b/tests/utils.py
@@ -0,0 +1,29 @@
+import subprocess
+import typing
+
+
+class CommandOutput(typing.TypedDict):
+    returncode: int
+    stdout: str | None
+    stderr: str | None
+
+
+def save_safari_webarchive(args: list[str]) -> CommandOutput:
+    proc = subprocess.Popen(
+        ["swift", "save_safari_webarchive.swift"] + args,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+    )
+    stdout, stderr = proc.communicate()
+
+    if stdout is not None:
+        stdout = stdout.decode("utf8")
+
+    if stderr is not None:
+        stderr = stderr.decode("utf8")
+
+    return CommandOutput(
+        returncode=proc.returncode,
+        stdout=stdout or None,
+        stderr=stderr or None,
+    )