GitRoot
Craft your forge, Build your project, Grow your community freely
1package main
2
3import (
4 "bytes"
5 "embed"
6 "io/fs"
7 "strings"
8 "testing"
9 "time"
10
11 "gitroot.dev/libs/golang/plugin/model"
12 "gitroot.dev/libs/golang/plugin/test"
13
14 "github.com/sergi/go-diff/diffmatchpatch"
15)
16
17//go:embed test_ressources/*
18var fakeFs embed.FS
19
20type command struct {
21 kind string
22
23 //kind == commit/endCommit
24 branch string
25 commit string
26 hash string
27
28 //kind == addFile && replace && replaceAll
29 filepath string
30
31 //kind == modFile
32 newFilepath string
33
34 //kind == check
35 aCheck string
36 bCheck string
37
38 //kind == replace
39 oldContent string
40 newContent string // kind == replaceAll
41
42 //kind == persoCheck
43 call func()
44}
45
46func (c command) Exec(p *Plugin, t *testing.T) {
47 switch c.kind {
48 case "init":
49 p.Init("fake", false, `{"defaultTargetBranch":"dev"}`)
50 case "commit":
51 p.StartCommit(model.Commit{
52 Branch: c.branch,
53 Hash: c.hash,
54 Message: c.commit,
55 Date: time.Now(),
56 CommitterName: "me",
57 })
58 case "endCommit":
59 p.EndCommit(model.Commit{
60 Branch: c.branch,
61 Hash: c.hash,
62 Message: c.commit,
63 Date: time.Now(),
64 CommitterName: "me",
65 })
66 case "addFile":
67 p.AddFile(model.File{Path: c.filepath})
68 case "modFile":
69 p.ModFile(model.File{OldPath: c.filepath, Path: c.newFilepath})
70 case "finish":
71 p.Finish()
72 case "check":
73 file, err := fs.ReadFile(p.server.Worktree(), c.aCheck)
74 if err != nil {
75 t.Fatal(err)
76 }
77 goodfile, _ := fs.ReadFile(fakeFs, c.bCheck)
78 if !bytes.Equal(file, goodfile) {
79 //t.Fatalf("bad %s > %s", c.bCheck, file)
80 dmp := diffmatchpatch.New()
81
82 diffs := dmp.DiffMain(string(file), string(goodfile), false)
83 t.Fatal(dmp.DiffPrettyText(diffs))
84 }
85 case "replace":
86 file, err := fs.ReadFile(p.server.Worktree(), c.filepath)
87 if err != nil {
88 t.Fatal(err)
89 }
90 p.server.ModifyContent(c.filepath, strings.Replace(string(file), c.oldContent, c.newContent, 1))
91 case "replaceAll":
92 p.server.ModifyContent(c.filepath, c.newContent)
93 case "persoCheck":
94 c.call()
95 }
96}
97
98type testData struct {
99 name string
100 commands []command
101}
102
103func TestStatus(t *testing.T) {
104 fakeServer := test.NewFakeServer(t)
105 p := &Plugin{
106 server: fakeServer,
107 }
108
109 datas := []testData{{
110 name: "simple",
111 commands: []command{
112 {kind: "init"},
113 {kind: "commit", branch: "test", commit: "hello", hash: "aaaa"},
114 {kind: "addFile", filepath: "new_file.sh"},
115 {kind: "endCommit", branch: "test", commit: "hello", hash: "aaaa"},
116 {kind: "finish"},
117 {kind: "check", aCheck: "grafts/test.md", bCheck: "test_ressources/graft_1.md"},
118 {kind: "init"},
119 {kind: "commit", branch: "test", commit: "hello2", hash: "bbbb"},
120 {kind: "modFile", filepath: "new_file.sh", newFilepath: "new_file2.sh"},
121 {kind: "endCommit", branch: "test", commit: "hello2", hash: "bbbb"},
122 {kind: "commit", branch: "test", commit: "hello3", hash: "cccc"},
123 {kind: "modFile", filepath: "new_file2.sh", newFilepath: "new_file2.sh"},
124 {kind: "endCommit", branch: "test", commit: "hello3", hash: "cccc"},
125 {kind: "finish"},
126 {kind: "check", aCheck: "grafts/test.md", bCheck: "test_ressources/graft_2.md"},
127 {kind: "replace", filepath: "grafts/test.md", oldContent: "status: draft", newContent: "status: review"},
128 {kind: "init"},
129 {kind: "commit", branch: "test", commit: "hello4", hash: "dddd"},
130 {kind: "modFile", filepath: "new_file2.sh", newFilepath: "new_file2.sh"},
131 {kind: "endCommit", branch: "test", commit: "hello4", hash: "dddd"},
132 {kind: "finish"},
133 {kind: "persoCheck", call: func() {
134 if fakeServer.NbCall.DiffWithParent != 1 {
135 t.Fatalf("DiffWithParent not called")
136 }
137 }},
138 {kind: "check", aCheck: "grafts/test.md", bCheck: "test_ressources/graft_3.md"},
139 {kind: "replace", filepath: "grafts/test.md", oldContent: "status: review", newContent: "status: merge"},
140 {kind: "init"},
141 {kind: "commit", branch: "test", commit: "hello5", hash: "eeee"},
142 {kind: "modFile", filepath: "grafts/test.md", newFilepath: "grafts/test.md"},
143 {kind: "endCommit", branch: "test", commit: "hello5", hash: "eeee"},
144 {kind: "finish"},
145 {kind: "persoCheck", call: func() {
146 nbModifyContent := 2 + 4 // 2 from test + 4 from plugin (commit)
147
148 if fakeServer.NbCall.ModifyContent != nbModifyContent { //don't call modify content for only change on graft
149 t.Fatalf("ModifyContent called %d", fakeServer.NbCall.ModifyContent)
150 }
151
152 if fakeServer.NbCall.Merge != 1 {
153 t.Fatalf("Merge not called")
154 }
155 }},
156 },
157 }, {
158 name: "erase",
159 commands: []command{
160 {kind: "init"},
161 {kind: "commit", branch: "erase", commit: "hello", hash: "aaaa"},
162 {kind: "addFile", filepath: "new_file.sh"},
163 {kind: "endCommit", branch: "erase", commit: "hello", hash: "aaaa"},
164 {kind: "finish"},
165 {kind: "check", aCheck: "grafts/erase.md", bCheck: "test_ressources/erase_1.md"},
166 {kind: "replaceAll", filepath: "grafts/erase.md", newContent: "# I want to merge\nBecause this is life\n"},
167 {kind: "init"},
168 {kind: "commit", branch: "erase", commit: "hello1", hash: "bbbb"},
169 {kind: "modFile", filepath: "grafts/erase.md", newFilepath: "grafts/erase.md"},
170 {kind: "modFile", filepath: "new_file.sh", newFilepath: "new_file.sh"},
171 {kind: "endCommit", branch: "erase", commit: "hello1", hash: "bbbb"},
172 {kind: "finish"},
173 {kind: "check", aCheck: "grafts/erase.md", bCheck: "test_ressources/erase_2.md"},
174 {kind: "replace", filepath: "grafts/erase.md", oldContent: "status: draft", newContent: "status: merge"},
175 {kind: "init"},
176 {kind: "commit", branch: "erase", commit: "hello2", hash: "cccc"},
177 {kind: "modFile", filepath: "grafts/erase.md", newFilepath: "grafts/erase.md"},
178 {kind: "endCommit", branch: "erase", commit: "hello2", hash: "cccc"},
179 {kind: "finish"},
180 {kind: "check", aCheck: "grafts/erase.md", bCheck: "test_ressources/erase_3.md"},
181 {kind: "persoCheck", call: func() {
182 nbModifyContent := 2 + 3 // 2 from test + 3 from plugin (commit)
183
184 if fakeServer.NbCall.ModifyContent != nbModifyContent { //don't call modify content for only change on graft
185 t.Fatalf("ModifyContent called %d", fakeServer.NbCall.ModifyContent)
186 }
187
188 if fakeServer.NbCall.Merge != 1 {
189 t.Fatalf("Merge not called %d", fakeServer.NbCall.Merge)
190 }
191 }},
192 },
193 }}
194
195 for _, data := range datas {
196 t.Run(data.name, func(t *testing.T) {
197 fakeServer.NbCall.ModifyContent = 0
198 fakeServer.NbCall.Merge = 0
199 for _, cmd := range data.commands {
200 cmd.Exec(p, t)
201 }
202 })
203 }
204}