MCPcopy Create free account
hub / github.com/alibaba/open-code-review / TestRangeDiffDetectsRename

Function TestRangeDiffDetectsRename

internal/diff/git_test.go:710–762  ·  view source on GitHub ↗

TestRangeDiffDetectsRename guards against issue #99: when a file is renamed on the target branch, `ocr review --from master --to BRANCH` must recognize the rename and read content at the NEW path. Before the fix the rename could surface as delete(old)+add(new) (e.g. diff.renames=false) and the parse

(t *testing.T)

Source from the content-addressed store, hash-verified

708// broken /dev/null detection sent the deleted half into `git show ref:oldpath`
709// -> "WARNING: cannot read file ... exit status 128".
710func TestRangeDiffDetectsRename(t *testing.T) {
711 repo := initRepoWithChange(t)
712
713 // Reset the working-tree modification left by the helper.
714 runGitTest(t, repo, "checkout", "--", "sample.txt")
715 // Simulate a user config where git does NOT detect renames on its own;
716 // the provider must force --find-renames.
717 runGitTest(t, repo, "config", "diff.renames", "false")
718
719 // Commit a file large enough for git's similarity detection to work
720 // (tiny files fall below the rename threshold even for 1-line edits).
721 var content strings.Builder
722 for i := 1; i <= 50; i++ {
723 fmt.Fprintf(&content, "line%d\n", i)
724 }
725 orig := filepath.Join(repo, "orig.txt")
726 if err := os.WriteFile(orig, []byte(content.String()), 0o644); err != nil {
727 t.Fatalf("write orig.txt: %v", err)
728 }
729 runGitTest(t, repo, "add", "orig.txt")
730 runGitTest(t, repo, "commit", "-q", "-m", "add orig.txt")
731
732 // Rename on a feature branch, with a small edit (like the issue repro).
733 runGitTest(t, repo, "checkout", "-q", "-b", "feature")
734 runGitTest(t, repo, "mv", "orig.txt", "renamed.txt")
735 edited := strings.Replace(content.String(), "line25\n", "line25-edited\n", 1)
736 if err := os.WriteFile(filepath.Join(repo, "renamed.txt"), []byte(edited), 0o644); err != nil {
737 t.Fatalf("edit renamed.txt: %v", err)
738 }
739 runGitTest(t, repo, "add", "-A")
740 runGitTest(t, repo, "commit", "-q", "-m", "rename orig.txt")
741
742 runner := gitcmd.New(0)
743 provider := NewProvider(repo, "HEAD~1", "feature", runner)
744
745 diffs, err := provider.GetDiff(context.Background())
746 if err != nil {
747 t.Fatalf("GetDiff (range, rename) returned error: %v", err)
748 }
749 if len(diffs) != 1 {
750 t.Fatalf("expected exactly 1 diff for a rename, got %d: %+v", len(diffs), diffs)
751 }
752 d := diffs[0]
753 if !d.IsRenamed {
754 t.Errorf("IsRenamed = false, want true")
755 }
756 if d.OldPath != "orig.txt" || d.NewPath != "renamed.txt" {
757 t.Errorf("OldPath/NewPath = %q/%q, want orig.txt/renamed.txt", d.OldPath, d.NewPath)
758 }
759 if d.NewFileContent == "" {
760 t.Errorf("NewFileContent is empty: content at new path was not read at ref")
761 }
762}
763
764// TestRangeDiffSurvivesExternalDiffTool covers the ModeRange call site
765// (git diff <base> <to>), which likewise must pass --no-ext-diff so that a

Callers

nothing calls this directly

Calls 6

GetDiffMethod · 0.95
NewFunction · 0.92
initRepoWithChangeFunction · 0.85
runGitTestFunction · 0.85
NewProviderFunction · 0.70
StringMethod · 0.45

Tested by

no test coverage detected