| 1 | //go:build windows |
| 2 | |
| 3 | package worktree |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestCreateChecksOutLongRepositoryPathOnWindows(t *testing.T) { |
| 15 | requireGit(t) |
| 16 | repo := initRepo(t) |
| 17 | relativeDir := "" |
| 18 | for len(filepath.Join(repo, relativeDir, "deep-file.txt")) <= 320 { |
| 19 | relativeDir = filepath.Join(relativeDir, "deep-repository-segment") |
| 20 | } |
| 21 | relativePath := filepath.Join(relativeDir, "deep-file.txt") |
| 22 | if err := os.MkdirAll(filepath.Dir(filepath.Join(repo, relativePath)), 0o755); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | if err := os.WriteFile(filepath.Join(repo, relativePath), []byte("long path\n"), 0o644); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | git := func(args ...string) { |
| 29 | t.Helper() |
| 30 | cmd := exec.Command("git", append([]string{"-c", "core.longpaths=true", "-C", repo}, args...)...) |
| 31 | cmd.Env = append(os.Environ(), |
| 32 | "GIT_AUTHOR_NAME=Reasonix Test", "GIT_AUTHOR_EMAIL=reasonix@example.invalid", |
| 33 | "GIT_COMMITTER_NAME=Reasonix Test", "GIT_COMMITTER_EMAIL=reasonix@example.invalid") |
| 34 | if out, err := cmd.CombinedOutput(); err != nil { |
| 35 | t.Fatalf("git %v: %v\n%s", args, err, out) |
| 36 | } |
| 37 | } |
| 38 | git("add", "--", relativePath) |
| 39 | git("commit", "-m", "add long path") |
| 40 | |
| 41 | result, err := Create(context.Background(), repo, t.TempDir()) |
| 42 | if err != nil { |
| 43 | t.Fatalf("Create() with a long repository path: %v", err) |
| 44 | } |
| 45 | checkedOutPath := filepath.Join(result.WorktreeRoot, relativePath) |
| 46 | if len(checkedOutPath) <= 260 { |
| 47 | t.Fatalf("checked-out path length = %d, want a path beyond MAX_PATH: %q", len(checkedOutPath), checkedOutPath) |
| 48 | } |
| 49 | got, err := os.ReadFile(checkedOutPath) |
| 50 | if err != nil { |
| 51 | t.Fatalf("long-path worktree missing committed file: %v", err) |
| 52 | } |
| 53 | if strings.TrimSpace(string(got)) != "long path" { |
| 54 | t.Fatalf("long-path worktree file = %q", got) |
| 55 | } |
| 56 | } |
| 57 |