-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
81 lines (72 loc) · 2.64 KB
/
Copy pathintegration_test.go
File metadata and controls
81 lines (72 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/m7medVision/sx/internal/config"
"github.com/m7medVision/sx/internal/git"
)
// TestWorktreeAndFiles exercises the real git-worktree + file-copy pipeline
// (everything the TUI does on ctrl-w) against a throwaway repo.
func TestWorktreeAndFiles(t *testing.T) {
repo := t.TempDir()
run := func(args ...string) {
t.Helper()
cmd := exec.Command("git", append([]string{"-C", repo}, args...)...)
cmd.Env = append(os.Environ(),
"GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t",
"GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
run("init", "-b", "main")
os.WriteFile(filepath.Join(repo, "README"), []byte("hi"), 0o644)
run("add", "-A")
run("commit", "-m", "init")
// Gitignored files that must be copied into the clean worktree.
os.WriteFile(filepath.Join(repo, ".env"), []byte("SECRET=1"), 0o644)
os.WriteFile(filepath.Join(repo, ".sx.yaml"),
[]byte("files:\n copy:\n - .env\n"), 0o644)
// 1. .gitignore gets the worktree dir, once.
if err := git.EnsureWorktreesIgnored(repo, ".worktrees"); err != nil {
t.Fatal(err)
}
_ = git.EnsureWorktreesIgnored(repo, ".worktrees") // idempotent
gi, _ := os.ReadFile(filepath.Join(repo, ".gitignore"))
if strings.Count(string(gi), ".worktrees/") != 1 {
t.Fatalf(".gitignore not written once: %q", gi)
}
// 2. An existing source branch gets its own worktree.
run("branch", "feature/x")
wtPath := filepath.Join(repo, ".worktrees", "feature-x")
resolved, err := git.EnsureWorktree(repo, "feature/x", wtPath)
if err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(resolved, "README")); err != nil {
t.Fatalf("worktree checkout missing: %v", err)
}
// 3. Config-driven copy brings .env into the clean worktree.
cfg := config.Load(repo)
if err := cfg.ApplyFiles(repo, resolved); err != nil {
t.Fatal(err)
}
if b, err := os.ReadFile(filepath.Join(resolved, ".env")); err != nil || string(b) != "SECRET=1" {
t.Fatalf(".env not copied: %v %q", err, b)
}
// 4. The branch reported by the worktree matches.
out, _ := exec.Command("git", "-C", resolved, "branch", "--show-current").Output()
if strings.TrimSpace(string(out)) != "feature/x" {
t.Fatalf("unexpected branch: %q", out)
}
// 5. Linked-worktree detection: the new worktree is linked, repo root is not.
if _, linked := git.IsLinkedWorktree(resolved); !linked {
t.Fatal("expected resolved worktree to be detected as linked")
}
if _, linked := git.IsLinkedWorktree(repo); linked {
t.Fatal("main worktree should not be detected as linked")
}
}