| 1 | //go:build windows |
| 2 | |
| 3 | package workspacelease |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "reasonix/internal/filelock" |
| 15 | ) |
| 16 | |
| 17 | func TestJunctionWorkspaceRetainsOldAliasLock(t *testing.T) { |
| 18 | root := t.TempDir() |
| 19 | target, alias := filepath.Join(root, "target"), filepath.Join(root, "alias") |
| 20 | if err := os.Mkdir(target, 0o700); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if out, err := exec.Command("cmd", "/c", "mklink", "/J", alias, target).CombinedOutput(); err != nil { |
| 24 | t.Fatalf("create junction: %v: %s", err, out) |
| 25 | } |
| 26 | lockDir := filepath.Join(root, "locks") |
| 27 | owner, err := New(alias, lockDir, nil) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | // Use the historical filesystem observation, including 8.3 expansion of |
| 32 | // the runner's temporary directory. The raw input is not the old lock key. |
| 33 | oldRoot, err := filepath.EvalSymlinks(alias) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | oldPath := workspaceLockPath(lockDir, compatibilityIdentityPath(nearestGitWorktreeRoot(oldRoot))) |
| 38 | if owner.lockPath != oldPath { |
| 39 | t.Fatalf("legacy lock changed: %q != %q", owner.lockPath, oldPath) |
| 40 | } |
| 41 | for _, group := range []bool{false, true} { |
| 42 | releaseOld, err := filelock.TryAcquire(oldPath) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 47 | var release func() |
| 48 | if group { |
| 49 | release, err = HoldWriteRoots(ctx, lockDir, alias) |
| 50 | } else { |
| 51 | release, err = owner.HoldWrite(ctx) |
| 52 | } |
| 53 | cancel() |
| 54 | releaseOld() |
| 55 | if release != nil { |
| 56 | release() |
| 57 | } |
| 58 | if !errors.Is(err, context.DeadlineExceeded) { |
| 59 | t.Fatalf("group=%v ignored old alias writer: %v", group, err) |
| 60 | } |
| 61 | } |
| 62 | release, err := owner.HoldWrite(context.Background()) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | defer release() |
| 67 | legacy, err := filelock.TryAcquire(oldPath) |
| 68 | if legacy != nil { |
| 69 | legacy() |
| 70 | } |
| 71 | if !errors.Is(err, filelock.ErrHeld) { |
| 72 | t.Fatalf("old writer ignored current holder: %v", err) |
| 73 | } |
| 74 | } |
| 75 |