From b8112a84eb8cdd77bfca698d108e067dd7a80f51 Mon Sep 17 00:00:00 2001 From: Don Naro Date: Fri, 11 Oct 2024 16:22:46 +0100 Subject: [PATCH 1/2] add tests requirements --- requirements/tests.in | 1 + requirements/tests.txt | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 requirements/tests.in create mode 100644 requirements/tests.txt diff --git a/requirements/tests.in b/requirements/tests.in new file mode 100644 index 0000000..e079f8a --- /dev/null +++ b/requirements/tests.in @@ -0,0 +1 @@ +pytest diff --git a/requirements/tests.txt b/requirements/tests.txt new file mode 100644 index 0000000..096139d --- /dev/null +++ b/requirements/tests.txt @@ -0,0 +1,14 @@ +# +# This file is autogenerated by pip-compile with Python 3.12 +# by the following command: +# +# pip-compile --allow-unsafe --output-file=requirements/tests.txt --strip-extras requirements/tests.in +# +iniconfig==2.0.0 + # via pytest +packaging==24.1 + # via pytest +pluggy==1.5.0 + # via pytest +pytest==8.3.3 + # via -r requirements/tests.in From be8c210f92d59c027c554d43add916508674280b Mon Sep 17 00:00:00 2001 From: Don Naro Date: Fri, 11 Oct 2024 16:23:13 +0100 Subject: [PATCH 2/2] initial test files --- tests/conftest.py | 51 ++++++++++++++++++++++++++++++++++ tests/json/github-payload.json | 12 ++++++++ tests/json/gitlab-payload.json | 12 ++++++++ tests/test_webhook_parser.py | 40 ++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/json/github-payload.json create mode 100644 tests/json/gitlab-payload.json create mode 100644 tests/test_webhook_parser.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..6d06878 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,51 @@ +"""Pytest conftest.py""" + +import json +import os +import tempfile +from pathlib import Path + +import pytest + +TESTS_DIR = Path(__file__).parent +JSON_DIR = TESTS_DIR / "json" + + +def load_json_payload(filename): + with open(JSON_DIR / filename, encoding="utf-8") as file: + return json.load(file) + + +TEST_PAYLOADS = { + file.stem.upper().replace("-", "_"): load_json_payload(file.name) + for file in JSON_DIR.glob("*.json") +} + + +@pytest.fixture +def temp_github_payload(): + """Create a temporary JSON file with GitHub payload.""" + test_data = TEST_PAYLOADS["GITHUB_PAYLOAD"] + with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: + json.dump(test_data, f) + yield f.name + os.unlink(f.name) + + +@pytest.fixture +def temp_gitlab_payload(): + """Create a temporary JSON file with GitLab payload.""" + test_data = TEST_PAYLOADS["GITLAB_PAYLOAD"] + with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: + json.dump(test_data, f) + yield f.name + os.unlink(f.name) + + +@pytest.fixture +def empty_json_file(): + """Create an empty JSON file.""" + with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: + f.write("{}") + yield f.name + os.unlink(f.name) diff --git a/tests/json/github-payload.json b/tests/json/github-payload.json new file mode 100644 index 0000000..da1ff84 --- /dev/null +++ b/tests/json/github-payload.json @@ -0,0 +1,12 @@ +{ + "repository": { + "clone_url": "https://github.com/owner/repository.git" + }, + "commits": [ + { + "added": ["ADDED.md"], + "removed": ["REMOVED.md"], + "modified": ["MODIFIED.md"] + } + ] +} diff --git a/tests/json/gitlab-payload.json b/tests/json/gitlab-payload.json new file mode 100644 index 0000000..cef97b5 --- /dev/null +++ b/tests/json/gitlab-payload.json @@ -0,0 +1,12 @@ +{ + "project": { + "git_http_url": "https://gitlab.example.com/owner/repository.git" + }, + "commits": [ + { + "added": ["ADDED_FILE.md"], + "modified": ["REMOVED_FILE.md"], + "removed": ["MODIFIED_FILE.md"] + } + ] +} diff --git a/tests/test_webhook_parser.py b/tests/test_webhook_parser.py new file mode 100644 index 0000000..31e6faf --- /dev/null +++ b/tests/test_webhook_parser.py @@ -0,0 +1,40 @@ +import subprocess +import sys +from pathlib import Path + +SCRIPT_PATH = Path(__file__).parent.parent / "forge-webhook-parser.py" + + +def run_script(args): + """Helper function to run the script as a subprocess""" + result = subprocess.run( + [sys.executable, str(SCRIPT_PATH)] + args, capture_output=True, text=True + ) + return result + + +def test_read_github_payload_file(temp_github_payload): + """Test reading GitHub payload from JSON file.""" + result = run_script(["-f", temp_github_payload]) + + assert result.returncode == 0 + assert "Added: ADDED.md" in result.stdout + assert "Removed: REMOVED.md" in result.stdout + assert "Modified: MODIFIED.md" in result.stdout + + +def test_read_gitlab_payload_file(temp_gitlab_payload): + """Test reading GitLab payload from JSON file.""" + result = run_script(["-f", temp_gitlab_payload]) + + assert result.returncode == 0 + assert "Added: ADDED_FILE.md" in result.stdout + assert "Removed: REMOVED_FILE.md" in result.stdout + assert "Modified: MODIFIED_FILE.md" in result.stdout + + +def test_empty_json(empty_json_file): + """Test handling of empty JSON.""" + result = run_script(["-f", empty_json_file]) + assert result.returncode == 1 + assert "An error occurred" in result.stderr