GitRoot

craft your forge, build your project, grow your community freely
  1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
  2//
  3// SPDX-License-Identifier: MIT
  4
  5package test
  6
  7import (
  8	"bytes"
  9	"io/fs"
 10	"os"
 11	"path/filepath"
 12	"testing"
 13
 14	"gitroot.dev/libs/golang/plugin/model"
 15)
 16
 17type fakeServer struct {
 18	t        *testing.T
 19	dirGit   string
 20	dirWeb   string
 21	dirCache string
 22	override Override
 23	NbCall   nbCall
 24}
 25
 26type Override struct {
 27	PluginOption       func(opts ...model.PluginOptionWith)
 28	ForgeConf          func() (model.ForgeConf, error)
 29	Worktree           func() fs.FS
 30	Webcontent         func() fs.FS
 31	Cache              func() fs.FS
 32	ModifyContent      func(path string, content string)
 33	ModifyWebContent   func(filepath, content string)
 34	ReplaceWebContent  func(filepath, oldContent, content string)
 35	ModifyCacheContent func(filepath, content string)
 36	CommitAllIfNeeded  func(message string)
 37	DiffWithParent     func(hash string, oldFilepath string, newFilepath string) (string, error)
 38	Merge              func(from string, to string)
 39	Commits            func(from string, to string) ([]model.Commit, error)
 40	Exec               func(exec model.Exec) (model.ExecStatus, error)
 41	Report             func(level model.ReportLevel, content []string) error
 42	ExportFunc         func(name string, callback func(args map[string]string) (map[string]string, error))
 43	CallFunc           func(plugin string, name string, args map[string]string) (map[string]string, error)
 44}
 45
 46type nbCall struct {
 47	PluginOption       int
 48	ForgeConf          int
 49	Worktree           int
 50	Webcontent         int
 51	Cache              int
 52	ModifyContent      int
 53	ModifyWebContent   int
 54	ReplaceWebContent  int
 55	ModifyCacheContent int
 56	CommitAllIfNeeded  int
 57	DiffWithParent     int
 58	Log                int
 59	LogError           int
 60	Merge              int
 61	Commits            int
 62	Exec               int
 63	Report             int
 64	ExportFunc         int
 65	CallFunc           int
 66}
 67
 68func NewFakeServer(t *testing.T, o Override) *fakeServer {
 69	dirGit, _ := os.MkdirTemp("", "gitrootPluginTestGit*")
 70	dirWeb, _ := os.MkdirTemp("", "gitrootPluginTestWeb*")
 71	dirCache, _ := os.MkdirTemp("", "gitrootPluginTestCache*")
 72	return &fakeServer{
 73		t:        t,
 74		dirGit:   dirGit,
 75		dirWeb:   dirWeb,
 76		dirCache: dirCache,
 77		override: o,
 78		NbCall:   nbCall{},
 79	}
 80}
 81
 82func (s *fakeServer) PluginOption(opts ...model.PluginOptionWith) {
 83	s.NbCall.PluginOption++
 84	if s.override.PluginOption != nil {
 85		s.override.PluginOption(opts...)
 86	}
 87}
 88
 89func (s *fakeServer) ForgeConf() (model.ForgeConf, error) {
 90	s.NbCall.ForgeConf++
 91	if s.override.ForgeConf != nil {
 92		return s.override.ForgeConf()
 93	}
 94	return model.ForgeConf{
 95		Domain:             "test",
 96		ExternalSshAddr:    "ssh://127.0.0.1:4545/",
 97		ExternalHttpAddr:   "ssh://127.0.0.1:4546/",
 98		RootRepositoryName: "root",
 99	}, nil
100}
101
102func (s *fakeServer) Worktree() fs.FS {
103	s.NbCall.Worktree++
104	if s.override.Worktree != nil {
105		return s.override.Worktree()
106	}
107	return os.DirFS(s.dirGit)
108}
109
110func (s *fakeServer) Webcontent() fs.FS {
111	s.NbCall.Webcontent++
112	if s.override.Webcontent != nil {
113		return s.override.Webcontent()
114	}
115	return os.DirFS(s.dirWeb)
116}
117
118func (s *fakeServer) Cache() fs.FS {
119	s.NbCall.Cache++
120	if s.override.Cache != nil {
121		return s.override.Cache()
122	}
123	return os.DirFS(s.dirCache)
124}
125
126func (s *fakeServer) ModifyContent(path, content string) {
127	s.NbCall.ModifyContent++
128	if s.override.ModifyContent != nil {
129		s.override.ModifyContent(path, content)
130	} else {
131		os.MkdirAll(s.dirGit+"/"+filepath.Dir(path), fs.ModePerm)
132		if err := os.WriteFile(s.dirGit+"/"+path, []byte(content), fs.ModePerm); err != nil {
133			s.t.Fatal(err)
134		}
135	}
136}
137
138func (s *fakeServer) ModifyWebContent(path, content string) {
139	s.NbCall.ModifyWebContent++
140	s.t.Logf("ModifyWebContent called with path=%s", path)
141	if s.override.ModifyWebContent != nil {
142		s.override.ModifyWebContent(path, content)
143	} else {
144		os.MkdirAll(s.dirWeb+"/"+filepath.Dir(path), fs.ModePerm)
145		if err := os.WriteFile(s.dirWeb+"/"+path, []byte(content), fs.ModePerm); err != nil {
146			s.t.Fatal(err)
147		}
148	}
149}
150
151func (s *fakeServer) ReplaceWebContent(filepath, oldContent, content string) {
152	s.NbCall.ReplaceWebContent++
153	if s.override.ReplaceWebContent != nil {
154		s.override.ReplaceWebContent(filepath, oldContent, content)
155	} else {
156		content, err := os.ReadFile(filepath)
157		if err != nil {
158			s.t.Fatal(err)
159		}
160
161		if err := os.WriteFile(s.dirWeb+"/"+filepath, bytes.Replace(content, []byte(oldContent), []byte(content), 1), 0666); err != nil {
162			s.t.Fatal(err)
163		}
164	}
165}
166
167func (s *fakeServer) ModifyCacheContent(path, content string) {
168	s.NbCall.ModifyCacheContent++
169	if s.override.ModifyCacheContent != nil {
170		s.override.ModifyCacheContent(path, content)
171	} else {
172		os.MkdirAll(s.dirCache+"/"+filepath.Dir(path), fs.ModePerm)
173		if err := os.WriteFile(s.dirCache+"/"+path, []byte(content), fs.ModePerm); err != nil {
174			s.t.Fatal(err)
175		}
176	}
177}
178
179func (s *fakeServer) CommitAllIfNeeded(message string) {
180	s.NbCall.CommitAllIfNeeded++
181	if s.override.CommitAllIfNeeded != nil {
182		s.override.CommitAllIfNeeded(message)
183	}
184}
185
186func (s *fakeServer) DiffWithParent(hash string, oldFilepath string, newFilepath string) (string, error) {
187	s.NbCall.DiffWithParent++
188	if s.override.DiffWithParent != nil {
189		return s.override.DiffWithParent(hash, oldFilepath, newFilepath)
190	}
191	return "", nil
192}
193
194func (s *fakeServer) Log(message string) {
195	s.NbCall.Log++
196}
197
198func (s *fakeServer) LogError(message string, err error) {
199	s.NbCall.LogError++
200}
201
202func (s *fakeServer) Merge(from string, to string) {
203	s.NbCall.Merge++
204	if s.override.Merge != nil {
205		s.override.Merge(from, to)
206	}
207}
208
209func (s *fakeServer) Commits(from string, to string) ([]model.Commit, error) {
210	s.NbCall.Commits++
211	if s.override.Commits != nil {
212		return s.override.Commits(from, to)
213	}
214	return []model.Commit{}, nil
215}
216
217func (s *fakeServer) Exec(exec model.Exec) (model.ExecStatus, error) {
218	s.NbCall.Exec++
219	if s.override.Exec != nil {
220		return s.override.Exec(exec)
221	}
222	return model.ExecStatus{}, nil
223}
224
225func (s *fakeServer) Report(level model.ReportLevel, content []string) error {
226	s.NbCall.Report++
227	if s.override.Report != nil {
228		return s.override.Report(level, content)
229	}
230	return nil
231}
232
233func (s *fakeServer) ExportFunc(name string, callback func(args map[string]string) (map[string]string, error)) {
234	s.NbCall.ExportFunc++
235	if s.override.ExportFunc != nil {
236		s.override.ExportFunc(name, callback)
237	}
238}
239
240func (s *fakeServer) CallFunc(plugin string, name string, args map[string]string) (map[string]string, error) {
241	s.NbCall.CallFunc++
242	if s.override.CallFunc != nil {
243		return s.override.CallFunc(plugin, name, args)
244	}
245	return map[string]string{}, nil
246}