| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "testing" |
| 10 | |
| 11 | "reasonix/internal/control" |
| 12 | "reasonix/internal/event" |
| 13 | "reasonix/internal/gitcmd" |
| 14 | ) |
| 15 | |
| 16 | // The changes panel reads a tab through the identity its session resolved when |
| 17 | // it opened, even after the workspace — a subdirectory of that repository — |
| 18 | // gains its own .git whose local config names a filter. |
| 19 | func TestWorkspaceChangesReadTheSessionRepository(t *testing.T) { |
| 20 | if runtime.GOOS == "windows" { |
| 21 | t.Skip("payload script is POSIX shell") |
| 22 | } |
| 23 | mono := t.TempDir() |
| 24 | pkg := filepath.Join(mono, "pkg") |
| 25 | git := func(dir string, args ...string) { |
| 26 | t.Helper() |
| 27 | cmd := exec.Command("git", append([]string{"-C", dir}, args...)...) |
| 28 | cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=t", "GIT_AUTHOR_EMAIL=t@t", "GIT_COMMITTER_NAME=t", "GIT_COMMITTER_EMAIL=t@t") |
| 29 | if out, err := cmd.CombinedOutput(); err != nil { |
| 30 | t.Fatalf("git %v: %v: %s", args, err, out) |
| 31 | } |
| 32 | } |
| 33 | if err := os.MkdirAll(pkg, 0o755); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if err := os.WriteFile(filepath.Join(pkg, "f.txt"), []byte("a\n"), 0o644); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | git(mono, "init", "-q") |
| 40 | git(mono, "add", ".") |
| 41 | git(mono, "commit", "-qm", "base") |
| 42 | repo, err := gitcmd.Open(context.Background(), pkg) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | ctrl := control.New(control.Options{Sink: event.Discard, WorkspaceRoot: pkg, WorkspaceRepo: repo}) |
| 47 | a := &App{tabs: map[string]*WorkspaceTab{"a": {WorkspaceRoot: pkg, Ctrl: ctrl}}} |
| 48 | |
| 49 | marker := filepath.Join(t.TempDir(), "executed") |
| 50 | payload := filepath.Join(t.TempDir(), "payload.sh") |
| 51 | if err := os.WriteFile(payload, []byte("#!/bin/sh\necho ran >> '"+marker+"'\ncat\n"), 0o755); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | git(pkg, "init", "-q") |
| 55 | git(pkg, "config", "filter.pwn.clean", payload) |
| 56 | if err := os.WriteFile(filepath.Join(pkg, ".gitattributes"), []byte("f.txt filter=pwn\n"), 0o644); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if err := os.WriteFile(filepath.Join(pkg, "f.txt"), []byte("b\n"), 0o644); err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | |
| 63 | view := a.WorkspaceChanges("a") |
| 64 | var modified bool |
| 65 | for _, f := range view.Files { |
| 66 | modified = modified || (f.Path == "f.txt" && f.GitStatus == "M") |
| 67 | } |
| 68 | if !view.GitAvailable || !modified || view.Removed != 1 { |
| 69 | t.Fatalf("changes = %+v, want f.txt modified against the enclosing repository", view) |
| 70 | } |
| 71 | if _, err := a.WorkspaceChangeDetail("a", "f.txt"); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if _, err := os.Stat(marker); err == nil { |
| 75 | t.Fatal("the changes panel ran the nested repository's filter") |
| 76 | } |
| 77 | } |
| 78 |