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 "io/fs"
9 "os"
10 "path/filepath"
11 "testing"
12
13 "gitroot.dev/libs/golang/plugin/model"
14)
15
16type fakeServer struct {
17 t *testing.T
18 dirGit string
19 dirWeb string
20 dirCache string
21 override Override
22 NbCall nbCall
23}
24
25type Override struct {
26 Worktree func() fs.FS
27 Webcontent func() fs.FS
28 Cache func() fs.FS
29 ModifyContent func(path string, content string)
30 ModifyWebContent func(filepath, content string)
31 ModifyCacheContent func(filepath, content string)
32 CommitAllIfNeeded func(message string)
33 DiffWithParent func(hash string, oldFilepath string, newFilepath string) (string, error)
34 Merge func(from string, to string)
35 Commits func(from string, to string) ([]model.Commit, error)
36}
37
38type nbCall struct {
39 Worktree int
40 Webcontent int
41 Cache int
42 ModifyContent int
43 ModifyWebContent int
44 ModifyCacheContent int
45 CommitAllIfNeeded int
46 DiffWithParent int
47 Log int
48 LogError int
49 Merge int
50 Commits int
51}
52
53func NewFakeServer(t *testing.T, o Override) *fakeServer {
54 dirGit, _ := os.MkdirTemp("", "gitrootPluginTestGit*")
55 dirWeb, _ := os.MkdirTemp("", "gitrootPluginTestWeb*")
56 dirCache, _ := os.MkdirTemp("", "gitrootPluginTestCache*")
57 return &fakeServer{
58 t: t,
59 dirGit: dirGit,
60 dirWeb: dirWeb,
61 dirCache: dirCache,
62 override: o,
63 NbCall: nbCall{},
64 }
65}
66
67func (s *fakeServer) Worktree() fs.FS {
68 s.NbCall.Worktree++
69 if s.override.Worktree != nil {
70 return s.override.Worktree()
71 }
72 return os.DirFS(s.dirGit)
73}
74
75func (s *fakeServer) Webcontent() fs.FS {
76 s.NbCall.Webcontent++
77 if s.override.Webcontent != nil {
78 return s.override.Webcontent()
79 }
80 return os.DirFS(s.dirWeb)
81}
82
83func (s *fakeServer) Cache() fs.FS {
84 s.NbCall.Cache++
85 if s.override.Cache != nil {
86 return s.override.Cache()
87 }
88 return os.DirFS(s.dirCache)
89}
90
91func (s *fakeServer) ModifyContent(path, content string) {
92 s.NbCall.ModifyContent++
93 if s.override.ModifyContent != nil {
94 s.override.ModifyContent(path, content)
95 } else {
96 os.MkdirAll(s.dirGit+"/"+filepath.Dir(path), fs.ModePerm)
97 if err := os.WriteFile(s.dirGit+"/"+path, []byte(content), fs.ModePerm); err != nil {
98 s.t.Fatal(err)
99 }
100 }
101}
102
103func (s *fakeServer) ModifyWebContent(path, content string) {
104 s.NbCall.ModifyWebContent++
105 if s.override.ModifyWebContent != nil {
106 s.override.ModifyWebContent(path, content)
107 } else {
108 os.MkdirAll(s.dirWeb+"/"+filepath.Dir(path), fs.ModePerm)
109 if err := os.WriteFile(s.dirWeb+"/"+path, []byte(content), fs.ModePerm); err != nil {
110 s.t.Fatal(err)
111 }
112 }
113}
114
115func (s *fakeServer) ModifyCacheContent(path, content string) {
116 s.NbCall.ModifyCacheContent++
117 if s.override.ModifyCacheContent != nil {
118 s.override.ModifyCacheContent(path, content)
119 } else {
120 os.MkdirAll(s.dirCache+"/"+filepath.Dir(path), fs.ModePerm)
121 if err := os.WriteFile(s.dirCache+"/"+path, []byte(content), fs.ModePerm); err != nil {
122 s.t.Fatal(err)
123 }
124 }
125}
126
127func (s *fakeServer) CommitAllIfNeeded(message string) {
128 s.NbCall.CommitAllIfNeeded++
129 if s.override.CommitAllIfNeeded != nil {
130 s.override.CommitAllIfNeeded(message)
131 }
132}
133
134func (s *fakeServer) DiffWithParent(hash string, oldFilepath string, newFilepath string) (string, error) {
135 s.NbCall.DiffWithParent++
136 if s.override.DiffWithParent != nil {
137 return s.override.DiffWithParent(hash, oldFilepath, newFilepath)
138 }
139 return "", nil
140}
141
142func (s *fakeServer) Log(message string) {
143 s.NbCall.Log++
144}
145
146func (s *fakeServer) LogError(message string, err error) {
147 s.NbCall.LogError++
148}
149
150func (s *fakeServer) Merge(from string, to string) {
151 s.NbCall.Merge++
152 if s.override.Merge != nil {
153 s.override.Merge(from, to)
154 }
155}
156
157func (s *fakeServer) Commits(from string, to string) ([]model.Commit, error) {
158 s.NbCall.Commits++
159 if s.override.Commits != nil {
160 return s.override.Commits(from, to)
161 }
162 return []model.Commit{}, nil
163}