c42a19f7d3
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
164 lines
4.6 KiB
Go
164 lines
4.6 KiB
Go
package filediff
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func put(t *testing.T, dir, name, text string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(text), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
func TestStageLocalChanges(t *testing.T) {
|
|
dir := t.TempDir()
|
|
ctx := context.Background()
|
|
put(t, dir, "dirty.txt", "already dirty\n")
|
|
put(t, dir, "delete.txt", "remove me\n")
|
|
put(t, dir, "binary", "\x00before")
|
|
put(t, dir, "mode.txt", "same\n")
|
|
before := Capture(ctx, dir)
|
|
put(t, dir, "dirty.txt", "stage one\n")
|
|
put(t, dir, "new.txt", "new content")
|
|
put(t, dir, "empty", "")
|
|
put(t, dir, "binary", "\x00after")
|
|
if err := os.Remove(filepath.Join(dir, "delete.txt")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(filepath.Join(dir, "mode.txt"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after := Capture(ctx, dir, before)
|
|
changes := Compare(before, after)
|
|
if changes.Status != "complete" || len(changes.Files) != 6 {
|
|
t.Fatal(changes)
|
|
}
|
|
for _, f := range changes.Files {
|
|
switch f.Path {
|
|
case "dirty.txt":
|
|
if !strings.Contains(f.Patch, "-already dirty") || !strings.Contains(f.Patch, "+stage one") {
|
|
t.Fatal(f)
|
|
}
|
|
case "delete.txt":
|
|
if f.Kind != "deleted" || !strings.Contains(f.Patch, "/dev/null") {
|
|
t.Fatal(f)
|
|
}
|
|
case "new.txt":
|
|
if f.Kind != "added" || !strings.Contains(f.Patch, "No newline at end of file") {
|
|
t.Fatal(f)
|
|
}
|
|
case "binary":
|
|
if !f.Binary || f.Patch != "" {
|
|
t.Fatal(f)
|
|
}
|
|
case "mode.txt":
|
|
if f.BeforeMode == f.AfterMode {
|
|
t.Fatal(f)
|
|
}
|
|
case "empty":
|
|
if f.Kind != "added" {
|
|
t.Fatal(f)
|
|
}
|
|
}
|
|
}
|
|
put(t, dir, "dirty.txt", "stage two\n")
|
|
second := Compare(after, Capture(ctx, dir, after))
|
|
if len(second.Files) != 1 || !strings.Contains(second.Files[0].Patch, "-stage one") || strings.Contains(second.Files[0].Patch, "already dirty") {
|
|
t.Fatal(second)
|
|
}
|
|
}
|
|
|
|
func TestGitInventoryDirtyUntrackedIgnoredAndSubdirectory(t *testing.T) {
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("git unavailable")
|
|
}
|
|
dir := t.TempDir()
|
|
ctx := context.Background()
|
|
git := func(args ...string) {
|
|
t.Helper()
|
|
cmd := exec.Command("git", append([]string{"-C", dir}, args...)...)
|
|
if data, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("%v: %v %s", args, err, data)
|
|
}
|
|
}
|
|
git("init", "-q")
|
|
if err := os.Mkdir(filepath.Join(dir, "sub"), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
put(t, dir, ".gitignore", "ignored\n")
|
|
put(t, dir, "sub/tracked", "index version\n")
|
|
put(t, dir, "outside", "outside\n")
|
|
git("add", ".")
|
|
put(t, dir, "sub/tracked", "dirty before\n")
|
|
put(t, dir, "sub/untracked", "before\n")
|
|
put(t, dir, "sub/ignored", "ignored before\n")
|
|
sub := filepath.Join(dir, "sub")
|
|
before := Capture(ctx, sub)
|
|
put(t, dir, "sub/tracked", "after\n")
|
|
put(t, dir, "sub/untracked", "after\n")
|
|
put(t, dir, "sub/new", "new\n")
|
|
put(t, dir, "sub/ignored", "after ignored\n")
|
|
put(t, dir, "outside", "after outside\n")
|
|
changes := Compare(before, Capture(ctx, sub, before))
|
|
if len(changes.Files) != 3 {
|
|
t.Fatal(changes)
|
|
}
|
|
for _, f := range changes.Files {
|
|
if strings.Contains(f.Path, "sub/") || strings.Contains(f.Path, "ignored") || strings.Contains(f.Path, "outside") {
|
|
t.Fatal(f)
|
|
}
|
|
if f.Path == "tracked" && !strings.Contains(f.Patch, "-dirty before") {
|
|
t.Fatal(f)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProtectedSymlinkAndLimits(t *testing.T) {
|
|
dir := t.TempDir()
|
|
ctx := context.Background()
|
|
put(t, dir, ".env", "secret")
|
|
put(t, dir, "large", strings.Repeat("x", maxFileBytes+1))
|
|
if err := os.Symlink(filepath.Join(dir, ".env"), filepath.Join(dir, "alias")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := Capture(ctx, dir)
|
|
put(t, dir, ".env", "secret changed")
|
|
put(t, dir, "large", "small now")
|
|
put(t, dir, "normal", "safe\n")
|
|
changes := Compare(before, Capture(ctx, dir, before))
|
|
if changes.Status != "partial" || len(changes.Files) != 1 || changes.Files[0].Path != "normal" {
|
|
t.Fatal(changes)
|
|
}
|
|
for _, f := range changes.Files {
|
|
if strings.Contains(f.Patch, "secret") {
|
|
t.Fatal("secret retained")
|
|
}
|
|
}
|
|
canceled, cancel := context.WithCancel(ctx)
|
|
cancel()
|
|
s := Capture(canceled, dir)
|
|
if Compare(s, before).Status != "partial" {
|
|
t.Fatal("canceled capture presented as complete")
|
|
}
|
|
if Compare(Capture(ctx, filepath.Join(dir, "missing")), before).Status != "unavailable" {
|
|
t.Fatal("missing root presented as complete")
|
|
}
|
|
}
|
|
|
|
func TestPatchLimits(t *testing.T) {
|
|
dir := t.TempDir()
|
|
ctx := context.Background()
|
|
put(t, dir, "many", "before\n")
|
|
before := Capture(ctx, dir)
|
|
put(t, dir, "many", strings.Repeat("after\n", maxLines+1))
|
|
c := Compare(before, Capture(ctx, dir, before))
|
|
if c.Status != "partial" || !c.Files[0].Truncated {
|
|
t.Fatal(c)
|
|
}
|
|
}
|