package main import ( "bytes" "embed" "io/fs" "strings" "testing" "time" "gitroot.dev/libs/golang/plugin/model" "gitroot.dev/libs/golang/plugin/test" "github.com/sergi/go-diff/diffmatchpatch" ) //go:embed test_ressources/* var fakeFs embed.FS type command struct { kind string //kind == commit/endCommit branch string commit string hash string //kind == addFile && replace && replaceAll filepath string //kind == modFile newFilepath string //kind == check aCheck string bCheck string //kind == replace oldContent string newContent string // kind == replaceAll //kind == persoCheck call func() } func (c command) Exec(p *Plugin, t *testing.T) { switch c.kind { case "init": p.Init("fake", false, `{"defaultTargetBranch":"dev"}`) case "commit": p.StartCommit(model.Commit{ Branch: c.branch, Hash: c.hash, Message: c.commit, Date: time.Now(), CommitterName: "me", }) case "endCommit": p.EndCommit(model.Commit{ Branch: c.branch, Hash: c.hash, Message: c.commit, Date: time.Now(), CommitterName: "me", }) case "addFile": p.AddFile(model.File{Path: c.filepath}) case "modFile": p.ModFile(model.File{OldPath: c.filepath, Path: c.newFilepath}) case "finish": p.Finish() case "check": file, err := fs.ReadFile(p.server.Worktree(), c.aCheck) if err != nil { t.Fatal(err) } goodfile, _ := fs.ReadFile(fakeFs, c.bCheck) if !bytes.Equal(file, goodfile) { //t.Fatalf("bad %s > %s", c.bCheck, file) dmp := diffmatchpatch.New() diffs := dmp.DiffMain(string(file), string(goodfile), false) t.Fatal(dmp.DiffPrettyText(diffs)) } case "replace": file, err := fs.ReadFile(p.server.Worktree(), c.filepath) if err != nil { t.Fatal(err) } p.server.ModifyContent(c.filepath, strings.Replace(string(file), c.oldContent, c.newContent, 1)) case "replaceAll": p.server.ModifyContent(c.filepath, c.newContent) case "persoCheck": c.call() } } type testData struct { name string commands []command } func TestStatus(t *testing.T) { fakeServer := test.NewFakeServer(t) p := &Plugin{ server: fakeServer, } datas := []testData{{ name: "simple", commands: []command{ {kind: "init"}, {kind: "commit", branch: "test", commit: "hello", hash: "aaaa"}, {kind: "addFile", filepath: "new_file.sh"}, {kind: "endCommit", branch: "test", commit: "hello", hash: "aaaa"}, {kind: "finish"}, {kind: "check", aCheck: "grafts/test.md", bCheck: "test_ressources/graft_1.md"}, {kind: "init"}, {kind: "commit", branch: "test", commit: "hello2", hash: "bbbb"}, {kind: "modFile", filepath: "new_file.sh", newFilepath: "new_file2.sh"}, {kind: "endCommit", branch: "test", commit: "hello2", hash: "bbbb"}, {kind: "commit", branch: "test", commit: "hello3", hash: "cccc"}, {kind: "modFile", filepath: "new_file2.sh", newFilepath: "new_file2.sh"}, {kind: "endCommit", branch: "test", commit: "hello3", hash: "cccc"}, {kind: "finish"}, {kind: "check", aCheck: "grafts/test.md", bCheck: "test_ressources/graft_2.md"}, {kind: "replace", filepath: "grafts/test.md", oldContent: "status: draft", newContent: "status: review"}, {kind: "init"}, {kind: "commit", branch: "test", commit: "hello4", hash: "dddd"}, {kind: "modFile", filepath: "new_file2.sh", newFilepath: "new_file2.sh"}, {kind: "endCommit", branch: "test", commit: "hello4", hash: "dddd"}, {kind: "finish"}, {kind: "persoCheck", call: func() { if fakeServer.NbCall.DiffWithParent != 1 { t.Fatalf("DiffWithParent not called") } }}, {kind: "check", aCheck: "grafts/test.md", bCheck: "test_ressources/graft_3.md"}, {kind: "replace", filepath: "grafts/test.md", oldContent: "status: review", newContent: "status: merge"}, {kind: "init"}, {kind: "commit", branch: "test", commit: "hello5", hash: "eeee"}, {kind: "modFile", filepath: "grafts/test.md", newFilepath: "grafts/test.md"}, {kind: "endCommit", branch: "test", commit: "hello5", hash: "eeee"}, {kind: "finish"}, {kind: "persoCheck", call: func() { nbModifyContent := 2 + 4 // 2 from test + 4 from plugin (commit) if fakeServer.NbCall.ModifyContent != nbModifyContent { //don't call modify content for only change on graft t.Fatalf("ModifyContent called %d", fakeServer.NbCall.ModifyContent) } if fakeServer.NbCall.Merge != 1 { t.Fatalf("Merge not called") } }}, }, }, { name: "erase", commands: []command{ {kind: "init"}, {kind: "commit", branch: "erase", commit: "hello", hash: "aaaa"}, {kind: "addFile", filepath: "new_file.sh"}, {kind: "endCommit", branch: "erase", commit: "hello", hash: "aaaa"}, {kind: "finish"}, {kind: "check", aCheck: "grafts/erase.md", bCheck: "test_ressources/erase_1.md"}, {kind: "replaceAll", filepath: "grafts/erase.md", newContent: "# I want to merge\nBecause this is life\n"}, {kind: "init"}, {kind: "commit", branch: "erase", commit: "hello1", hash: "bbbb"}, {kind: "modFile", filepath: "grafts/erase.md", newFilepath: "grafts/erase.md"}, {kind: "modFile", filepath: "new_file.sh", newFilepath: "new_file.sh"}, {kind: "endCommit", branch: "erase", commit: "hello1", hash: "bbbb"}, {kind: "finish"}, {kind: "check", aCheck: "grafts/erase.md", bCheck: "test_ressources/erase_2.md"}, {kind: "replace", filepath: "grafts/erase.md", oldContent: "status: draft", newContent: "status: merge"}, {kind: "init"}, {kind: "commit", branch: "erase", commit: "hello2", hash: "cccc"}, {kind: "modFile", filepath: "grafts/erase.md", newFilepath: "grafts/erase.md"}, {kind: "endCommit", branch: "erase", commit: "hello2", hash: "cccc"}, {kind: "finish"}, {kind: "check", aCheck: "grafts/erase.md", bCheck: "test_ressources/erase_3.md"}, {kind: "persoCheck", call: func() { nbModifyContent := 2 + 3 // 2 from test + 3 from plugin (commit) if fakeServer.NbCall.ModifyContent != nbModifyContent { //don't call modify content for only change on graft t.Fatalf("ModifyContent called %d", fakeServer.NbCall.ModifyContent) } if fakeServer.NbCall.Merge != 1 { t.Fatalf("Merge not called %d", fakeServer.NbCall.Merge) } }}, }, }} for _, data := range datas { t.Run(data.name, func(t *testing.T) { fakeServer.NbCall.ModifyContent = 0 fakeServer.NbCall.Merge = 0 for _, cmd := range data.commands { cmd.Exec(p, t) } }) } }