summaryrefslogtreecommitdiff
path: root/internal/processor/processor_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-27 08:27:28 +0300
committerPaul Buetow <paul@buetow.org>2026-04-27 08:27:28 +0300
commit8b37e6e04035f9f8a3d01701dae121cdc67cbf43 (patch)
treecc9b7ddbadbb401eb957c38cac96b5819e503dda /internal/processor/processor_test.go
parentf8f17653e66bd2cd90c6a2cf7970afee54c643dc (diff)
processor: fix uniqueID to return error instead of infinite loop on Stat errors
Previously, uniqueID(postsDir, t) returned only a string and looped forever when os.Stat returned an error other than IsNotExist (e.g. permission denied). Change the signature to (string, error), propagate the error, and update the caller in processFile to handle it. Add positive and negative tests covering new ID generation, suffix collision, and permission-based stat failure.
Diffstat (limited to 'internal/processor/processor_test.go')
-rw-r--r--internal/processor/processor_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/internal/processor/processor_test.go b/internal/processor/processor_test.go
index d04a0d5..3b34675 100644
--- a/internal/processor/processor_test.go
+++ b/internal/processor/processor_test.go
@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"testing"
+ "time"
"codeberg.org/snonux/snonux/internal/config"
"codeberg.org/snonux/snonux/internal/post"
@@ -141,6 +142,57 @@ func TestRun_markdown(t *testing.T) {
}
}
+func TestUniqueID_new(t *testing.T) {
+ t.Parallel()
+
+ postsDir := t.TempDir()
+ id, err := uniqueID(postsDir, time.Now().UTC())
+ if err != nil {
+ t.Fatalf("uniqueID: %v", err)
+ }
+ if id == "" {
+ t.Fatal("expected non-empty id")
+ }
+}
+
+func TestUniqueID_collision(t *testing.T) {
+ t.Parallel()
+
+ postsDir := t.TempDir()
+ now := time.Now().UTC()
+
+ // Pre-create the first expected directory so uniqueID must pick the next suffix.
+ firstID := post.NewID(now, 0)
+ if err := os.MkdirAll(filepath.Join(postsDir, firstID), 0o755); err != nil {
+ t.Fatal(err)
+ }
+
+ id, err := uniqueID(postsDir, now)
+ if err != nil {
+ t.Fatalf("uniqueID: %v", err)
+ }
+ if id == firstID {
+ t.Fatalf("expected different id, got %q", id)
+ }
+}
+
+func TestUniqueID_statError(t *testing.T) {
+ t.Parallel()
+
+ // Create a postsDir and remove read permission so Stat fails with
+ // a permission error rather than IsNotExist.
+ postsDir := t.TempDir()
+ if err := os.Chmod(postsDir, 0o000); err != nil {
+ t.Fatal(err)
+ }
+ defer os.Chmod(postsDir, 0o755) // restore for cleanup
+
+ _, err := uniqueID(postsDir, time.Now().UTC())
+ if err == nil {
+ t.Fatal("expected error when stat fails")
+ }
+}
+
func TestRun_markdownWithLocalImage(t *testing.T) {
t.Parallel()