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