Prefer friendly paths for get-local-path

This commit is contained in:
Jan Bader
2026-07-19 22:20:59 +02:00
parent fc499af504
commit 4b96db068a
2 changed files with 86 additions and 1 deletions
+49
View File
@@ -2,6 +2,7 @@ package main
import (
"context"
"os"
"path/filepath"
"testing"
@@ -119,3 +120,51 @@ func TestBuildMountTreeAddsByIDDirectory(t *testing.T) {
}
}
}
func TestFriendlyLocalPathPrefersNonByIDPath(t *testing.T) {
mountPath := t.TempDir()
friendlyDir := filepath.Join(mountPath, "Bills", "2024", "07")
byIDDir := filepath.Join(mountPath, "by-id", "it")
if err := os.MkdirAll(friendlyDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(byIDDir, 0755); err != nil {
t.Fatal(err)
}
friendlyPath := filepath.Join(friendlyDir, "Invoice.pdf")
byIDPath := filepath.Join(byIDDir, "item-1.pdf")
if err := os.WriteFile(friendlyPath, []byte("pdf"), 0644); err != nil {
t.Fatal(err)
}
if err := os.Link(friendlyPath, byIDPath); err != nil {
t.Fatal(err)
}
got, err := friendlyLocalPath(mountPath, byIDPath)
if err != nil {
t.Fatal(err)
}
if got != friendlyPath {
t.Fatalf("expected friendly path %q, got %q", friendlyPath, got)
}
}
func TestFriendlyLocalPathReturnsEmptyWhenNoFriendlyPathExists(t *testing.T) {
mountPath := t.TempDir()
byIDDir := filepath.Join(mountPath, "by-id", "it")
if err := os.MkdirAll(byIDDir, 0755); err != nil {
t.Fatal(err)
}
byIDPath := filepath.Join(byIDDir, "item-1.pdf")
if err := os.WriteFile(byIDPath, []byte("pdf"), 0644); err != nil {
t.Fatal(err)
}
got, err := friendlyLocalPath(mountPath, byIDPath)
if err != nil {
t.Fatal(err)
}
if got != "" {
t.Fatalf("expected no friendly path, got %q", got)
}
}