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