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