1b698a1241
Every run in this repository reported an incomplete file-change capture because the tracked .env.example matched the secret-file rules and was skipped. Committed templates (.example, .sample, .template, .dist) hold placeholders, so they are captured like any file; real secret files and protected directories stay excluded.
303 lines
9.9 KiB
Go
303 lines
9.9 KiB
Go
package filediff
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"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))
|
|
// The oversized file is compared by hash and listed without a patch; the
|
|
// protected file and the symlink stay excluded.
|
|
if changes.Status != "partial" || len(changes.Files) != 2 || changes.Files[0].Path != "large" || changes.Files[1].Path != "normal" {
|
|
t.Fatal(changes)
|
|
}
|
|
if large := changes.Files[0]; !large.NoPatch || !large.Truncated || large.Patch != "" || large.Kind != "modified" {
|
|
t.Fatalf("oversized file not listed without patch: %+v", large)
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestBinaryFilesDoNotStarveSourceDiffs(t *testing.T) {
|
|
dir := t.TempDir()
|
|
ctx := context.Background()
|
|
image := "\x00" + strings.Repeat("x", maxFileBytes-1)
|
|
for i := 0; i <= maxSnapshotBytes/maxFileBytes; i++ {
|
|
put(t, dir, fmt.Sprintf("image-%03d.png", i), image)
|
|
}
|
|
put(t, dir, "source.go", "before\n")
|
|
before := Capture(ctx, dir)
|
|
put(t, dir, "source.go", "after\n")
|
|
put(t, dir, "image-000.png", "\x00changed")
|
|
changes := Compare(before, Capture(ctx, dir, before))
|
|
if changes.Status != "complete" || len(changes.Files) != 2 {
|
|
t.Fatalf("binary inventory hid source changes: %+v", changes)
|
|
}
|
|
for _, change := range changes.Files {
|
|
if change.Path == "source.go" && !strings.Contains(change.Patch, "+after") {
|
|
t.Fatal("source patch missing", change)
|
|
}
|
|
}
|
|
for name, f := range before.files {
|
|
if f.binary && len(f.data) != 0 {
|
|
t.Fatalf("binary bytes unnecessarily retained for %s", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func gitRepo(t *testing.T) (string, func(...string)) {
|
|
t.Helper()
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("git unavailable")
|
|
}
|
|
dir := t.TempDir()
|
|
run := func(args ...string) {
|
|
t.Helper()
|
|
cmd := exec.Command("git", append([]string{"-C", dir, "-c", "user.email=t@example.invalid", "-c", "user.name=t", "-c", "commit.gpgsign=false"}, args...)...)
|
|
if data, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("%v: %v %s", args, err, data)
|
|
}
|
|
}
|
|
run("init", "-q")
|
|
return dir, run
|
|
}
|
|
|
|
// TestOverBudgetChangeIsListedWithoutPatch is the run-236 regression outside
|
|
// Git: a workspace whose text exceeds the snapshot budget must still list the
|
|
// one changed file (sorted last, after the budget is exhausted) instead of
|
|
// returning files: [] with every late path skipped.
|
|
func TestOverBudgetChangeIsListedWithoutPatch(t *testing.T) {
|
|
restore := SetSnapshotBudgetForTesting(64)
|
|
defer restore()
|
|
dir := t.TempDir()
|
|
if err := os.MkdirAll(filepath.Join(dir, "web/src"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 20; i++ {
|
|
put(t, dir, fmt.Sprintf("a-%02d.txt", i), strings.Repeat("filler\n", 4))
|
|
}
|
|
put(t, dir, "web/src/Inbox.test.tsx", "it('works')\n")
|
|
ctx := context.Background()
|
|
before := Capture(ctx, dir)
|
|
put(t, dir, "web/src/Inbox.test.tsx", "it('works')\nit('more')\n")
|
|
changes := Compare(before, Capture(ctx, dir, before))
|
|
if len(changes.Files) != 1 || changes.Files[0].Path != "web/src/Inbox.test.tsx" {
|
|
t.Fatalf("over-budget change not listed: %+v", changes)
|
|
}
|
|
if f := changes.Files[0]; f.Kind != "modified" || !f.NoPatch || f.Patch != "" || f.NoPatchReason != reasonBudget {
|
|
t.Fatalf("over-budget change should be listed without patch: %+v", f)
|
|
}
|
|
if changes.Status != "partial" || changes.Skipped != 0 {
|
|
t.Fatalf("status/skipped = %q/%d, want partial/0", changes.Status, changes.Skipped)
|
|
}
|
|
// Unchanged over-budget files are compared by hash, so a visit that edits
|
|
// nothing is a complete, empty capture rather than a partial one.
|
|
again := Capture(ctx, dir)
|
|
if empty := Compare(again, Capture(ctx, dir, again)); empty.Status != "complete" || len(empty.Files) != 0 {
|
|
t.Fatalf("no-op over-budget visit = %+v, want complete and empty", empty)
|
|
}
|
|
}
|
|
|
|
// TestGitStatusListsUncomparableChanges proves a file the content snapshot
|
|
// skips (here: above the hash bound) is still listed when its Git status
|
|
// changes during the visit, while a protected path never is.
|
|
func TestGitStatusListsUncomparableChanges(t *testing.T) {
|
|
previous := maxHashBytes
|
|
maxHashBytes = 16
|
|
defer func() { maxHashBytes = previous }()
|
|
dir, git := gitRepo(t)
|
|
put(t, dir, "big.bin", strings.Repeat("x", 64))
|
|
put(t, dir, ".env", "A=1\n")
|
|
put(t, dir, "small.txt", "one\n")
|
|
git("add", "big.bin", "small.txt")
|
|
git("commit", "-qm", "init")
|
|
ctx := context.Background()
|
|
before := Capture(ctx, dir)
|
|
put(t, dir, "big.bin", strings.Repeat("y", 64))
|
|
put(t, dir, "fresh.bin", strings.Repeat("z", 64))
|
|
put(t, dir, ".env", "A=2\n")
|
|
changes := Compare(before, Capture(ctx, dir, before))
|
|
got := map[string]string{}
|
|
for _, f := range changes.Files {
|
|
if !f.NoPatch {
|
|
t.Fatalf("git-listed change carries a patch: %+v", f)
|
|
}
|
|
got[f.Path] = f.Kind
|
|
}
|
|
if len(got) != 2 || got["big.bin"] != "modified" || got["fresh.bin"] != "added" {
|
|
t.Fatalf("git status listing = %v (%+v)", got, changes)
|
|
}
|
|
if changes.Status != "partial" || !strings.Contains(changes.Diagnostic, "without patches") {
|
|
t.Fatalf("status/diagnostic = %q/%q", changes.Status, changes.Diagnostic)
|
|
}
|
|
}
|
|
|
|
func TestProtectedKeepsSecretsButNotCommittedTemplates(t *testing.T) {
|
|
for name, want := range map[string]bool{
|
|
".env": true,
|
|
".env.local": true,
|
|
"app/prod.env": true,
|
|
"certs/server.key": true,
|
|
"secrets/token.example": true, // a protected directory stays protected
|
|
".env.example": false,
|
|
"app/.env.sample": false,
|
|
"config/server.key.dist": false,
|
|
"deploy/.env.template": false,
|
|
"docs/example.md": false,
|
|
} {
|
|
if got := protected(name); got != want {
|
|
t.Errorf("protected(%q) = %v, want %v", name, got, want)
|
|
}
|
|
}
|
|
}
|