|
| 1 | +package guildpath |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestEnsureGuildDir_CreatesWith0700(t *testing.T) { |
| 10 | + home := t.TempDir() |
| 11 | + t.Setenv("HOME", home) |
| 12 | + dir, err := EnsureGuildDir() |
| 13 | + if err != nil { |
| 14 | + t.Fatalf("EnsureGuildDir: %v", err) |
| 15 | + } |
| 16 | + if dir != filepath.Join(home, ".guild") { |
| 17 | + t.Errorf("dir = %q; want %q", dir, filepath.Join(home, ".guild")) |
| 18 | + } |
| 19 | + info, err := os.Stat(dir) |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("stat %s: %v", dir, err) |
| 22 | + } |
| 23 | + if got := info.Mode().Perm(); got != DirPerm { |
| 24 | + t.Errorf("perm = %o; want %o", got, DirPerm) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestEnsureGuildDir_Idempotent(t *testing.T) { |
| 29 | + home := t.TempDir() |
| 30 | + t.Setenv("HOME", home) |
| 31 | + d1, err := EnsureGuildDir() |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("first EnsureGuildDir: %v", err) |
| 34 | + } |
| 35 | + d2, err := EnsureGuildDir() |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("second EnsureGuildDir: %v", err) |
| 38 | + } |
| 39 | + if d1 != d2 { |
| 40 | + t.Errorf("paths differ: %q vs %q", d1, d2) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Regression: when a stale 0o755 dir pre-exists (e.g. created by a |
| 45 | +// prior CLI verb before this fix), EnsureGuildDir must not chmod it |
| 46 | +// back to 0o700. We document and rely on the umask-preserving |
| 47 | +// idempotence; sites that need a guaranteed-tight dir should call |
| 48 | +// os.Chmod after EnsureGuildDir, but we do not foreclose other |
| 49 | +// callers' deliberate widening. |
| 50 | +func TestEnsureGuildDir_PreservesExistingMode(t *testing.T) { |
| 51 | + home := t.TempDir() |
| 52 | + t.Setenv("HOME", home) |
| 53 | + dir := filepath.Join(home, ".guild") |
| 54 | + if err := os.MkdirAll(dir, 0o750); err != nil { |
| 55 | + t.Fatalf("pre-seed dir: %v", err) |
| 56 | + } |
| 57 | + if _, err := EnsureGuildDir(); err != nil { |
| 58 | + t.Fatalf("EnsureGuildDir: %v", err) |
| 59 | + } |
| 60 | + info, err := os.Stat(dir) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("stat: %v", err) |
| 63 | + } |
| 64 | + if got := info.Mode().Perm(); got != 0o750 { |
| 65 | + t.Errorf("perm = %o; EnsureGuildDir must not retroactively chmod (#79 comment)", got) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestTightenDBPerms_AllSiblings(t *testing.T) { |
| 70 | + dir := t.TempDir() |
| 71 | + path := filepath.Join(dir, "lore.db") |
| 72 | + // Seed all four siblings with the default 0o644 sqlite ships them at. |
| 73 | + for _, suffix := range []string{"", "-wal", "-shm", "-journal"} { |
| 74 | + p := path + suffix |
| 75 | + if err := os.WriteFile(p, []byte("x"), 0o644); err != nil { //nolint:gosec // test fixture mirrors sqlite's default |
| 76 | + t.Fatalf("seed %s: %v", p, err) |
| 77 | + } |
| 78 | + } |
| 79 | + if err := TightenDBPerms(path); err != nil { |
| 80 | + t.Fatalf("TightenDBPerms: %v", err) |
| 81 | + } |
| 82 | + for _, suffix := range []string{"", "-wal", "-shm", "-journal"} { |
| 83 | + p := path + suffix |
| 84 | + info, err := os.Stat(p) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("stat %s: %v", p, err) |
| 87 | + } |
| 88 | + if got := info.Mode().Perm(); got != DBPerm { |
| 89 | + t.Errorf("%s perm = %o; want %o", p, got, DBPerm) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestTightenDBPerms_MissingSiblings(t *testing.T) { |
| 95 | + dir := t.TempDir() |
| 96 | + path := filepath.Join(dir, "quest.db") |
| 97 | + // Only the main file exists; sidecars haven't been materialised yet. |
| 98 | + if err := os.WriteFile(path, []byte("x"), 0o644); err != nil { //nolint:gosec // test fixture |
| 99 | + t.Fatalf("seed: %v", err) |
| 100 | + } |
| 101 | + if err := TightenDBPerms(path); err != nil { |
| 102 | + t.Fatalf("TightenDBPerms: %v", err) |
| 103 | + } |
| 104 | + info, err := os.Stat(path) |
| 105 | + if err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + if got := info.Mode().Perm(); got != DBPerm { |
| 109 | + t.Errorf("main file perm = %o; want %o", got, DBPerm) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestTightenDBPerms_MemoryPath(t *testing.T) { |
| 114 | + if err := TightenDBPerms(":memory:"); err != nil { |
| 115 | + t.Errorf("TightenDBPerms(:memory:) should be a no-op, got %v", err) |
| 116 | + } |
| 117 | + if err := TightenDBPerms(""); err != nil { |
| 118 | + t.Errorf("TightenDBPerms(\"\") should be a no-op, got %v", err) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Regression for the install-path race: a CLI verb that ran before |
| 123 | +// `guild init` could create ~/.guild at 0o755 and the install would |
| 124 | +// then no-op. Route the install path through EnsureGuildDir so the |
| 125 | +// first-creator semantics are consistent. |
| 126 | +func TestEnsureGuildDir_FirstCreatorWins(t *testing.T) { |
| 127 | + home := t.TempDir() |
| 128 | + t.Setenv("HOME", home) |
| 129 | + dir, err := EnsureGuildDir() |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("EnsureGuildDir: %v", err) |
| 132 | + } |
| 133 | + info, err := os.Stat(dir) |
| 134 | + if err != nil { |
| 135 | + t.Fatal(err) |
| 136 | + } |
| 137 | + if got := info.Mode().Perm(); got != DirPerm { |
| 138 | + t.Errorf("first-creator perm = %o; want %o", got, DirPerm) |
| 139 | + } |
| 140 | +} |
0 commit comments