From 540654960227b2edb7020107ba07dbf9cfdb9494 Mon Sep 17 00:00:00 2001 From: Matías Insaurralde Date: Sat, 04 Apr 2026 02:50:22 +0000 Subject: [PATCH] patchutil: add tests for IsFormatPatch Cover the sentinel-timestamp fast path, all two-header combinations for the heuristic path, the line-10 boundary, and false cases (empty, single line, plain diff, wrong timestamp, one header, headers beyond line 10). Signed-off-by: Matías Insaurralde --- patchutil/patchutil_test.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file(s) changed, 81 insertion(s)(+), 0 deletion(s)(-) diff --git a/patchutil/patchutil_test.go b/patchutil/patchutil_test.go --- a/patchutil/patchutil_test.go +++ b/patchutil/patchutil_test.go @@ -326,6 +326,87 @@ }) } } +func TestIsFormatPatch(t *testing.T) { + tests := []struct { + name string + patch string + want bool + }{ + // fast path: sentinel timestamp + { + name: "sentinel timestamp", + patch: "From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001\nFrom: Author \n", + want: true, + }, + // header-count path: various two-header combinations + { + name: "From and Date headers", + patch: "From: Author \nDate: Mon, 1 Jan 2024 00:00:00 +0000\n", + want: true, + }, + { + name: "From and Subject headers", + patch: "From: Author \nSubject: [PATCH] fix thing\n", + want: true, + }, + { + name: "Subject and Date headers", + patch: "Subject: [PATCH] fix thing\nDate: Mon, 1 Jan 2024 00:00:00 +0000\n", + want: true, + }, + { + name: "commit and From headers", + patch: "commit abc123\nFrom: Author \n", + want: true, + }, + // boundary: headers at lines 9 and 10 (0-indexed 8 and 9, last scanned) + { + name: "headers at lines 9 and 10", + patch: "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nFrom: Author \nSubject: [PATCH] fix\n", + want: true, + }, + // false cases + { + name: "empty string", + patch: "", + want: false, + }, + { + name: "single line", + patch: "From: Author ", + want: false, + }, + { + name: "plain diff", + patch: "diff --git a/f.txt b/f.txt\n--- a/f.txt\n+++ b/f.txt\n", + want: false, + }, + { + name: "From prefix but wrong timestamp falls through to header count of 1", + patch: "From 3c5035488318164b81f60fe3adcd6c9199d76331 Tue Oct 10 12:00:00 2023\nFrom: Author \n", + want: false, + }, + { + name: "only one recognized header", + patch: "Subject: [PATCH] fix thing\nsome other line\n", + want: false, + }, + { + name: "headers pushed past line 10 are not counted", + patch: "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nFrom: Author \nSubject: [PATCH] fix\n", + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsFormatPatch(tt.patch); got != tt.want { + t.Errorf("IsFormatPatch() = %v, want %v", got, tt.want) + } + }) + } +} + func TestImplsInterfaces(t *testing.T) { id := &InterdiffResult{} _ = isDiffsRenderer(id) -- tangled.sh