- Introduce a data model for structured representation of Git patches - Implement a parser to extract key fields from patch text - Add unit tests to ensure robustness and validate edge cases
79 lines
3.3 KiB
Python
79 lines
3.3 KiB
Python
import unittest
|
|
from patchman.utils.git_patch_parser.parser import parse_git_patch
|
|
from patchman.utils.git_patch_parser.models import GitPatch
|
|
|
|
class TestGitPatchParser(unittest.TestCase):
|
|
def test_parse_valid_patch(self):
|
|
patch_text = """From 0123456789abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001
|
|
From: Max Mustermann <max@example.com>
|
|
Date: Wed, 23 Jul 2025 12:34:56 +0200
|
|
Subject: [PATCH] Dein Patch-Titel
|
|
|
|
Dies ist eine Beschreibung des Patches.
|
|
|
|
diff --git a/file.txt b/file.txt
|
|
index 83db48f..f735c3b 100644
|
|
--- a/file.txt
|
|
+++ b/file.txt
|
|
@@ -1 +1 @@
|
|
-Hello World
|
|
+Hello Patchman
|
|
"""
|
|
expected_patch = GitPatch(
|
|
commit_hash="0123456789abcdef1234567890abcdef12345678",
|
|
author="Max Mustermann <max@example.com>",
|
|
date="Wed, 23 Jul 2025 12:34:56 +0200",
|
|
subject="[PATCH] Dein Patch-Titel",
|
|
description="Dies ist eine Beschreibung des Patches.",
|
|
diff="diff --git a/file.txt b/file.txt\nindex 83db48f..f735c3b 100644\n--- a/file.txt\n+++ b/file.txt\n@@ -1 +1 @@\n-Hello World\n+Hello Patchman\n"
|
|
)
|
|
parsed_patch = parse_git_patch(patch_text)
|
|
self.assertEqual(parsed_patch, expected_patch)
|
|
|
|
def test_parse_invalid_patch(self):
|
|
patch_text = "Invalid patch content"
|
|
with self.assertRaises(ValueError) as context:
|
|
parse_git_patch(patch_text)
|
|
self.assertIn("Commit hash not found", str(context.exception))
|
|
|
|
def test_parse_patch_missing_author(self):
|
|
patch_text = """From 0123456789abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001
|
|
Date: Wed, 23 Jul 2025 12:34:56 +0200
|
|
Subject: [PATCH] Test"""
|
|
with self.assertRaises(ValueError) as context:
|
|
parse_git_patch(patch_text)
|
|
self.assertIn("Author not found", str(context.exception))
|
|
|
|
def test_parse_patch_missing_date(self):
|
|
patch_text = """From 0123456789abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001
|
|
From: Test Author <test@example.com>
|
|
Subject: [PATCH] Test"""
|
|
with self.assertRaises(ValueError) as context:
|
|
parse_git_patch(patch_text)
|
|
self.assertIn("Date not found", str(context.exception))
|
|
|
|
def test_parse_patch_missing_subject(self):
|
|
patch_text = """From 0123456789abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001
|
|
From: Test Author <test@example.com>
|
|
Date: Wed, 23 Jul 2025 12:34:56 +0200"""
|
|
with self.assertRaises(ValueError) as context:
|
|
parse_git_patch(patch_text)
|
|
self.assertIn("Subject not found", str(context.exception))
|
|
|
|
def test_parse_patch_without_description_and_diff(self):
|
|
patch_text = """From 0123456789abcdef1234567890abcdef12345678 Mon Sep 17 00:00:00 2001
|
|
From: Test Author <test@example.com>
|
|
Date: Wed, 23 Jul 2025 12:34:56 +0200
|
|
Subject: [PATCH] Minimal patch"""
|
|
|
|
parsed_patch = parse_git_patch(patch_text)
|
|
self.assertEqual(parsed_patch.commit_hash, "0123456789abcdef1234567890abcdef12345678")
|
|
self.assertEqual(parsed_patch.author, "Test Author <test@example.com>")
|
|
self.assertEqual(parsed_patch.date, "Wed, 23 Jul 2025 12:34:56 +0200")
|
|
self.assertEqual(parsed_patch.subject, "[PATCH] Minimal patch")
|
|
self.assertEqual(parsed_patch.description, "")
|
|
self.assertEqual(parsed_patch.diff, "")
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|