返回 DeepSeek-Reasonix
creation_race_test.go
根目录 / internal / pathidentity / creation_race_test.go
1 package pathidentity
2
3 import (
4 "errors"
5 "os"
6 "path/filepath"
7 "testing"
8 )
9
10 func TestResolveAllowsCreationBetweenFilesystemObservations(t *testing.T) {
11 for _, tail := range []string{"state.lock", filepath.Join("new", "state.lock")} {
12 t.Run(tail, func(t *testing.T) {
13 root := t.TempDir()
14 path := filepath.Join(root, tail)
15 created := false
16 got, err := resolveThroughExistingAncestorWith(path, func(current string) (string, error) {
17 resolved, resolveErr := filepath.EvalSymlinks(current)
18 if !created {
19 created = true
20 if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
21 t.Fatal(err)
22 }
23 if err := os.WriteFile(path, nil, 0o600); err != nil {
24 t.Fatal(err)
25 }
26 }
27 return resolved, resolveErr
28 })
29 if err != nil {
30 t.Fatalf("concurrent creation rejected: %v", err)
31 }
32 want, err := filepath.EvalSymlinks(path)
33 if err != nil || !created || got != want {
34 t.Fatalf("resolved = %q, want %q; created=%v, err=%v", got, want, created, err)
35 }
36 })
37 }
38 }
39
40 func TestResolveRejectsDanglingAncestorAndLeaf(t *testing.T) {
41 root := t.TempDir()
42 link := filepath.Join(root, "dangling")
43 if err := os.Symlink(filepath.Join(root, "absent"), link); err != nil {
44 t.Skipf("symlink unavailable: %v", err)
45 }
46 for _, path := range []string{link, filepath.Join(link, "state.lock")} {
47 _, err := Resolve(path, Options{FollowLeaf: true})
48 var identityErr *Error
49 if !errors.As(err, &identityErr) || identityErr.Kind != ErrorUnavailable {
50 t.Fatalf("Resolve(%q) = %v, want unavailable", path, err)
51 }
52 }
53 }
54
54 lines GO