Monorepo for Tangled
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <matias@insaurral.de>

authored by

Matías Insaurralde and committed by tangled.org 54065496 eb118dda

+81
+81
patchutil/patchutil_test.go
··· 326 326 } 327 327 } 328 328 329 + func TestIsFormatPatch(t *testing.T) { 330 + tests := []struct { 331 + name string 332 + patch string 333 + want bool 334 + }{ 335 + // fast path: sentinel timestamp 336 + { 337 + name: "sentinel timestamp", 338 + patch: "From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001\nFrom: Author <a@example.com>\n", 339 + want: true, 340 + }, 341 + // header-count path: various two-header combinations 342 + { 343 + name: "From and Date headers", 344 + patch: "From: Author <a@example.com>\nDate: Mon, 1 Jan 2024 00:00:00 +0000\n", 345 + want: true, 346 + }, 347 + { 348 + name: "From and Subject headers", 349 + patch: "From: Author <a@example.com>\nSubject: [PATCH] fix thing\n", 350 + want: true, 351 + }, 352 + { 353 + name: "Subject and Date headers", 354 + patch: "Subject: [PATCH] fix thing\nDate: Mon, 1 Jan 2024 00:00:00 +0000\n", 355 + want: true, 356 + }, 357 + { 358 + name: "commit and From headers", 359 + patch: "commit abc123\nFrom: Author <a@example.com>\n", 360 + want: true, 361 + }, 362 + // boundary: headers at lines 9 and 10 (0-indexed 8 and 9, last scanned) 363 + { 364 + name: "headers at lines 9 and 10", 365 + patch: "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nFrom: Author <a@example.com>\nSubject: [PATCH] fix\n", 366 + want: true, 367 + }, 368 + // false cases 369 + { 370 + name: "empty string", 371 + patch: "", 372 + want: false, 373 + }, 374 + { 375 + name: "single line", 376 + patch: "From: Author <a@example.com>", 377 + want: false, 378 + }, 379 + { 380 + name: "plain diff", 381 + patch: "diff --git a/f.txt b/f.txt\n--- a/f.txt\n+++ b/f.txt\n", 382 + want: false, 383 + }, 384 + { 385 + name: "From prefix but wrong timestamp falls through to header count of 1", 386 + patch: "From 3c5035488318164b81f60fe3adcd6c9199d76331 Tue Oct 10 12:00:00 2023\nFrom: Author <a@example.com>\n", 387 + want: false, 388 + }, 389 + { 390 + name: "only one recognized header", 391 + patch: "Subject: [PATCH] fix thing\nsome other line\n", 392 + want: false, 393 + }, 394 + { 395 + name: "headers pushed past line 10 are not counted", 396 + patch: "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10\nFrom: Author <a@example.com>\nSubject: [PATCH] fix\n", 397 + want: false, 398 + }, 399 + } 400 + 401 + for _, tt := range tests { 402 + t.Run(tt.name, func(t *testing.T) { 403 + if got := IsFormatPatch(tt.patch); got != tt.want { 404 + t.Errorf("IsFormatPatch() = %v, want %v", got, tt.want) 405 + } 406 + }) 407 + } 408 + } 409 + 329 410 func TestImplsInterfaces(t *testing.T) { 330 411 id := &InterdiffResult{} 331 412 _ = isDiffsRenderer(id)