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}
 43
 44type nbCall struct {
 45	PluginOption       int
 46	ForgeConf          int
 47	Worktree           int
 48	Webcontent         int
 49	Cache              int
 50	ModifyContent      int
 51	ModifyWebContent   int
 52	ReplaceWebContent  int
 53	ModifyCacheContent int
 54	CommitAllIfNeeded  int
 55	DiffWithParent     int
 56	Log                int
 57	LogError           int
 58	Merge              int
 59	Commits            int
 60	Exec               int
 61	Report             int
 62}
 63
 64func NewFakeServer(t *testing.T, o Override) *fakeServer {
 65	dirGit, _ := os.MkdirTemp("", "gitrootPluginTestGit*")
 66	dirWeb, _ := os.MkdirTemp("", "gitrootPluginTestWeb*")
 67	dirCache, _ := os.MkdirTemp("", "gitrootPluginTestCache*")
 68	return &fakeServer{
 69		t:        t,
 70		dirGit:   dirGit,
 71		dirWeb:   dirWeb,
 72		dirCache: dirCache,
 73		override: o,
 74		NbCall:   nbCall{},
 75	}
 76}
 77
 78func (s *fakeServer) PluginOption(opts ...model.PluginOptionWith) {
 79	s.NbCall.PluginOption++
 80	if s.override.PluginOption != nil {
 81		s.override.PluginOption(opts...)
 82	}
 83}
 84
 85func (s *fakeServer) ForgeConf() (model.ForgeConf, error) {
 86	s.NbCall.ForgeConf++
 87	if s.override.ForgeConf != nil {
 88		return s.override.ForgeConf()
 89	}
 90	return model.ForgeConf{
 91		Domain:             "test",
 92		ExternalSshAddr:    "ssh://127.0.0.1:4545/",
 93		ExternalHttpAddr:   "ssh://127.0.0.1:4546/",
 94		RootRepositoryName: "root",
 95	}, nil
 96}
 97
 98func (s *fakeServer) Worktree() fs.FS {
 99	s.NbCall.Worktree++
100	if s.override.Worktree != nil {
101		return s.override.Worktree()
102	}
103	return os.DirFS(s.dirGit)
104}
105
106func (s *fakeServer) Webcontent() fs.FS {
107	s.NbCall.Webcontent++
108	if s.override.Webcontent != nil {
109		return s.override.Webcontent()
110	}
111	return os.DirFS(s.dirWeb)
112}
113
114func (s *fakeServer) Cache() fs.FS {
115	s.NbCall.Cache++
116	if s.override.Cache != nil {
117		return s.override.Cache()
118	}
119	return os.DirFS(s.dirCache)
120}
121
122func (s *fakeServer) ModifyContent(path, content string) {
123	s.NbCall.ModifyContent++
124	if s.override.ModifyContent != nil {
125		s.override.ModifyContent(path, content)
126	} else {
127		os.MkdirAll(s.dirGit+"/"+filepath.Dir(path), fs.ModePerm)
128		if err := os.WriteFile(s.dirGit+"/"+path, []byte(content), fs.ModePerm); err != nil {
129			s.t.Fatal(err)
130		}
131	}
132}
133
134func (s *fakeServer) ModifyWebContent(path, content string) {
135	s.NbCall.ModifyWebContent++
136	s.t.Logf("ModifyWebContent called with path=%s", path)
137	if s.override.ModifyWebContent != nil {
138		s.override.ModifyWebContent(path, content)
139	} else {
140		os.MkdirAll(s.dirWeb+"/"+filepath.Dir(path), fs.ModePerm)
141		if err := os.WriteFile(s.dirWeb+"/"+path, []byte(content), fs.ModePerm); err != nil {
142			s.t.Fatal(err)
143		}
144	}
145}
146
147func (s *fakeServer) ReplaceWebContent(filepath, oldContent, content string) {
148	s.NbCall.ReplaceWebContent++
149	if s.override.ReplaceWebContent != nil {
150		s.override.ReplaceWebContent(filepath, oldContent, content)
151	} else {
152		content, err := os.ReadFile(filepath)
153		if err != nil {
154			s.t.Fatal(err)
155		}
156
157		if err := os.WriteFile(s.dirWeb+"/"+filepath, bytes.Replace(content, []byte(oldContent), []byte(content), 1), 0666); err != nil {
158			s.t.Fatal(err)
159		}
160	}
161}
162
163func (s *fakeServer) ModifyCacheContent(path, content string) {
164	s.NbCall.ModifyCacheContent++
165	if s.override.ModifyCacheContent != nil {
166		s.override.ModifyCacheContent(path, content)
167	} else {
168		os.MkdirAll(s.dirCache+"/"+filepath.Dir(path), fs.ModePerm)
169		if err := os.WriteFile(s.dirCache+"/"+path, []byte(content), fs.ModePerm); err != nil {
170			s.t.Fatal(err)
171		}
172	}
173}
174
175func (s *fakeServer) CommitAllIfNeeded(message string) {
176	s.NbCall.CommitAllIfNeeded++
177	if s.override.CommitAllIfNeeded != nil {
178		s.override.CommitAllIfNeeded(message)
179	}
180}
181
182func (s *fakeServer) DiffWithParent(hash string, oldFilepath string, newFilepath string) (string, error) {
183	s.NbCall.DiffWithParent++
184	if s.override.DiffWithParent != nil {
185		return s.override.DiffWithParent(hash, oldFilepath, newFilepath)
186	}
187	return "", nil
188}
189
190func (s *fakeServer) Log(message string) {
191	s.NbCall.Log++
192}
193
194func (s *fakeServer) LogError(message string, err error) {
195	s.NbCall.LogError++
196}
197
198func (s *fakeServer) Merge(from string, to string) {
199	s.NbCall.Merge++
200	if s.override.Merge != nil {
201		s.override.Merge(from, to)
202	}
203}
204
205func (s *fakeServer) Commits(from string, to string) ([]model.Commit, error) {
206	s.NbCall.Commits++
207	if s.override.Commits != nil {
208		return s.override.Commits(from, to)
209	}
210	return []model.Commit{}, nil
211}
212
213func (s *fakeServer) Exec(exec model.Exec) (model.ExecStatus, error) {
214	s.NbCall.Exec++
215	if s.override.Exec != nil {
216		return s.override.Exec(exec)
217	}
218	return model.ExecStatus{}, nil
219}
220
221func (s *fakeServer) Report(level model.ReportLevel, content []string) error {
222	s.NbCall.Report++
223	if s.override.Report != nil {
224		return s.override.Report(level, content)
225	}
226	return nil
227}