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

Function TestRangeDiffDetectsRename

internal/diff/git_test.go:718–770  ·  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

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