Merge pull request #1 from alexwlchan/run-tests
- ID
be6fa4a- date
2024-05-16 20:28:16+00:00- author
Alex Chan <alex@alexwlchan.net>- parents
edc8030,c3b6b37- message
Merge pull request #1 from alexwlchan/run-tests Start adding some test config- changed files
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
.github/dependabot.yml (0) → .github/dependabot.yml (196)
diff --git a/.github/dependabot.yml b/.github/dependabot.yml
new file mode 100644
index 0000000..4d20f4b
--- /dev/null
+++ b/.github/dependabot.yml
@@ -0,0 +1,10 @@
+version: 2
+updates:
+ - package-ecosystem: "bundler"
+ directory: "/"
+ schedule:
+ interval: "daily"
+ - package-ecosystem: "pip"
+ directory: "/"
+ schedule:
+ interval: "daily"
.github/workflows/test.yml (0) → .github/workflows/test.yml (496)
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
new file mode 100644
index 0000000..d798fd7
--- /dev/null
+++ b/.github/workflows/test.yml
@@ -0,0 +1,30 @@
+name: Test
+
+on:
+ push:
+ branches:
+ - main
+
+ pull_request:
+ branches:
+ - main
+
+jobs:
+ test:
+ runs-on: macos-latest
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Set up Python
+ uses: actions/setup-python@v5
+ with:
+ python-version: "3.12"
+ cache: 'pip'
+ cache-dependency-path: 'dev_requirements.txt'
+
+ - name: Install dependencies
+ run: pip install -r dev_requirements.txt
+
+ - name: Run tests
+ run: python3 -m pytest tests
.gitignore (0) → .gitignore (19)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d080851
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.pyc
+*.webarchive
dev_requirements.in (0) → dev_requirements.in (7)
diff --git a/dev_requirements.in b/dev_requirements.in
new file mode 100644
index 0000000..e079f8a
--- /dev/null
+++ b/dev_requirements.in
@@ -0,0 +1 @@
+pytest
dev_requirements.txt (0) → dev_requirements.txt (277)
diff --git a/dev_requirements.txt b/dev_requirements.txt
new file mode 100644
index 0000000..13d0142
--- /dev/null
+++ b/dev_requirements.txt
@@ -0,0 +1,14 @@
+#
+# This file is autogenerated by pip-compile with Python 3.11
+# by the following command:
+#
+# pip-compile dev_requirements.in
+#
+iniconfig==2.0.0
+ # via pytest
+packaging==24.0
+ # via pytest
+pluggy==1.5.0
+ # via pytest
+pytest==8.2.0
+ # via -r dev_requirements.in
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 (0) → tests/test_save_safari_webarchive.py (1129)
diff --git a/tests/test_save_safari_webarchive.py b/tests/test_save_safari_webarchive.py
new file mode 100755
index 0000000..7d29f6e
--- /dev/null
+++ b/tests/test_save_safari_webarchive.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import pathlib
+
+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,
+ )