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

Function TestWorkspaceDiffUsesLiveTree

internal/diff/workspace_test.go:18–85  ·  view source on GitHub ↗
(test *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestWorkspaceDiffUsesLiveTree(test *testing.T) {
19 for _, format := range []string{"sha1", "sha256"} {
20 for _, unborn := range []bool{false, true} {
21 name := format + "/committed"
22 if unborn {
23 name = format + "/unborn"
24 }
25 test.Run(name, func(test *testing.T) {
26 repo := test.TempDir()
27 runGitTest(test, repo, "init", "-q", "--object-format="+format)
28 runGitTest(test, repo, "config", "user.name", "Test")
29 runGitTest(test, repo, "config", "user.email", "test@example.com")
30 runGitTest(test, repo, "config", "commit.gpgsign", "false")
31 if !unborn {
32 writeCommit(test, repo, "version.go", "package example\nconst Value = 1\n", "base")
33 }
34 path := filepath.Join(repo, "version.go")
35 write := func(content string) {
36 test.Helper()
37 if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
38 test.Fatal(err)
39 }
40 }
41 write("package example\nconst Value = 2\n")
42 runGitTest(test, repo, "add", "version.go")
43 write("package example\nconst Value = 3\n")
44 indexBefore, err := os.ReadFile(filepath.Join(repo, ".git", "index"))
45 if err != nil {
46 test.Fatal(err)
47 }
48 provider := NewWorkspaceProvider(repo, gitcmd.New(2))
49 diffs, err := provider.GetDiff(context.Background())
50 if err != nil || len(diffs) != 1 {
51 test.Fatalf("workspace diff: %v %v", diffs, err)
52 }
53 indexAfterDiff, err := os.ReadFile(filepath.Join(repo, ".git", "index"))
54 if err != nil || !bytes.Equal(indexBefore, indexAfterDiff) {
55 test.Fatalf("first diff modified the index: %v\nbefore: %x\nafter: %x", err, indexBefore, indexAfterDiff)
56 }
57 if !strings.Contains(diffs[0].Diff, "+const Value = 3") || strings.Contains(diffs[0].Diff, "Value = 2") || !strings.Contains(diffs[0].NewFileContent, "Value = 3") {
58 test.Fatalf("diff or new content used the index: %+v", diffs[0])
59 }
60 resolution := provider.ResolveInput(context.Background())
61 if resolution.BaseIsEmpty != unborn || resolution.ResolvedHead != "" || resolution.ExactRange != "" {
62 test.Fatalf("incorrect workspace identity: %+v", resolution)
63 }
64 if !unborn && resolution.ResolvedBase != gitOut(test, repo, "rev-parse", "HEAD") {
65 test.Fatalf("base not pinned: %+v", resolution)
66 }
67 if unborn {
68 if err := os.Remove(path); err != nil {
69 test.Fatal(err)
70 }
71 } else {
72 write("package example\nconst Value = 1\n")
73 }
74 diffs, err = provider.GetDiff(context.Background())
75 if err != nil || len(diffs) != 0 {

Callers

nothing calls this directly

Calls 8

NewFunction · 0.92
runGitTestFunction · 0.85
writeCommitFunction · 0.85
NewWorkspaceProviderFunction · 0.85
gitOutFunction · 0.85
GetDiffMethod · 0.80
ResolveInputMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected