-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_extra_test.go
More file actions
360 lines (281 loc) · 9.09 KB
/
service_extra_test.go
File metadata and controls
360 lines (281 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package git
import (
"context"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"dappco.re/go/core"
)
// --- validatePath tests ---
func TestService_ValidatePath_Bad_RelativePath(t *testing.T) {
svc := &Service{opts: ServiceOptions{WorkDir: "/home/repos"}}
err := svc.validatePath("relative/path")
assert.Error(t, err)
assert.Contains(t, err.Error(), "path must be absolute")
}
func TestService_ValidatePath_Bad_OutsideWorkDir(t *testing.T) {
svc := &Service{opts: ServiceOptions{WorkDir: "/home/repos"}}
err := svc.validatePath("/etc/passwd")
assert.Error(t, err)
assert.Contains(t, err.Error(), "outside of allowed WorkDir")
}
func TestService_ValidatePath_Good_InsideWorkDir(t *testing.T) {
svc := &Service{opts: ServiceOptions{WorkDir: "/home/repos"}}
err := svc.validatePath("/home/repos/my-project")
assert.NoError(t, err)
}
func TestService_ValidatePath_Good_NoWorkDir(t *testing.T) {
svc := &Service{opts: ServiceOptions{}}
err := svc.validatePath("/any/absolute/path")
assert.NoError(t, err)
}
// --- handleQuery path validation ---
func TestService_HandleQuery_Bad_InvalidPath(t *testing.T) {
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{WorkDir: "/home/repos"}),
opts: ServiceOptions{WorkDir: "/home/repos"},
}
result := svc.handleQuery(c, QueryStatus{
Paths: []string{"/outside/path"},
Names: map[string]string{"/outside/path": "bad"},
})
assert.False(t, result.OK)
}
// --- handleTask path validation ---
func TestService_HandleTask_Bad_PushInvalidPath(t *testing.T) {
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{WorkDir: "/home/repos"}),
opts: ServiceOptions{WorkDir: "/home/repos"},
}
result := svc.handleTask(c, TaskPush{Path: "relative/path"})
assert.False(t, result.OK)
}
func TestService_HandleTask_Bad_PullInvalidPath(t *testing.T) {
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{WorkDir: "/home/repos"}),
opts: ServiceOptions{WorkDir: "/home/repos"},
}
result := svc.handleTask(c, TaskPull{Path: "/etc/passwd"})
assert.False(t, result.OK)
}
func TestService_HandleTask_Bad_PushMultipleInvalidPath(t *testing.T) {
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{WorkDir: "/home/repos"}),
opts: ServiceOptions{WorkDir: "/home/repos"},
}
result := svc.handleTask(c, TaskPushMultiple{
Paths: []string{"/home/repos/ok", "/etc/bad"},
Names: map[string]string{},
})
assert.False(t, result.OK)
}
func TestNewService_Good(t *testing.T) {
opts := ServiceOptions{WorkDir: t.TempDir()}
factory := NewService(opts)
assert.NotNil(t, factory)
// Create a minimal Core to test the factory.
c := core.New()
svc, err := factory(c)
require.NoError(t, err)
assert.NotNil(t, svc)
service, ok := svc.(*Service)
require.True(t, ok)
assert.NotNil(t, service)
}
func TestService_OnStartup_Good(t *testing.T) {
c := core.New()
opts := ServiceOptions{WorkDir: t.TempDir()}
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, opts),
opts: opts,
}
err := svc.OnStartup(context.Background())
assert.NoError(t, err)
}
func TestService_HandleQuery_Good_Status(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{}),
}
// Call handleQuery directly.
result := svc.handleQuery(c, QueryStatus{
Paths: []string{dir},
Names: map[string]string{dir: "test-repo"},
})
assert.True(t, result.OK)
statuses, ok := result.Value.([]RepoStatus)
require.True(t, ok)
require.Len(t, statuses, 1)
assert.Equal(t, "test-repo", statuses[0].Name)
// Verify lastStatus was updated.
assert.Len(t, svc.lastStatus, 1)
}
func TestService_HandleQuery_Good_DirtyRepos(t *testing.T) {
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{}),
lastStatus: []RepoStatus{
{Name: "clean"},
{Name: "dirty", Modified: 1},
},
}
result := svc.handleQuery(c, QueryDirtyRepos{})
assert.True(t, result.OK)
dirty, ok := result.Value.([]RepoStatus)
require.True(t, ok)
assert.Len(t, dirty, 1)
assert.Equal(t, "dirty", dirty[0].Name)
}
func TestService_HandleQuery_Good_AheadRepos(t *testing.T) {
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{}),
lastStatus: []RepoStatus{
{Name: "synced"},
{Name: "ahead", Ahead: 3},
},
}
result := svc.handleQuery(c, QueryAheadRepos{})
assert.True(t, result.OK)
ahead, ok := result.Value.([]RepoStatus)
require.True(t, ok)
assert.Len(t, ahead, 1)
assert.Equal(t, "ahead", ahead[0].Name)
}
func TestService_HandleQuery_Good_UnknownQuery(t *testing.T) {
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{}),
}
result := svc.handleQuery(c, "unknown query type")
assert.False(t, result.OK)
assert.Nil(t, result.Value)
}
func TestService_HandleTask_Bad_PushNoRemote(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{}),
}
result := svc.handleTask(c, TaskPush{Path: dir, Name: "test"})
assert.False(t, result.OK, "push without remote should fail")
}
func TestService_HandleTask_Bad_PullNoRemote(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{}),
}
result := svc.handleTask(c, TaskPull{Path: dir, Name: "test"})
assert.False(t, result.OK, "pull without remote should fail")
}
func TestService_HandleTask_Good_PushMultiple(t *testing.T) {
dir, _ := filepath.Abs(initTestRepo(t))
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{}),
}
result := svc.handleTask(c, TaskPushMultiple{
Paths: []string{dir},
Names: map[string]string{dir: "test"},
})
// PushMultiple returns results even when individual pushes fail.
assert.True(t, result.OK)
results, ok := result.Value.([]PushResult)
require.True(t, ok)
assert.Len(t, results, 1)
assert.False(t, results[0].Success) // No remote
}
func TestService_HandleTask_Good_UnknownTask(t *testing.T) {
c := core.New()
svc := &Service{
ServiceRuntime: core.NewServiceRuntime(c, ServiceOptions{}),
}
result := svc.handleTask(c, "unknown task")
assert.False(t, result.OK)
assert.Nil(t, result.Value)
}
// --- Additional git operation tests ---
func TestGetStatus_Good_AheadBehindNoUpstream(t *testing.T) {
// A repo without a tracking branch should return 0 ahead/behind.
dir, _ := filepath.Abs(initTestRepo(t))
status := getStatus(context.Background(), dir, "no-upstream")
require.NoError(t, status.Error)
assert.Equal(t, 0, status.Ahead)
assert.Equal(t, 0, status.Behind)
}
func TestPushMultiple_Good_Empty(t *testing.T) {
results, err := PushMultiple(context.Background(), []string{}, map[string]string{})
assert.NoError(t, err)
assert.Empty(t, results)
}
func TestPushMultiple_Good_MultiplePaths(t *testing.T) {
dir1, _ := filepath.Abs(initTestRepo(t))
dir2, _ := filepath.Abs(initTestRepo(t))
results, err := PushMultiple(context.Background(), []string{dir1, dir2}, map[string]string{
dir1: "repo-1",
dir2: "repo-2",
})
assert.Error(t, err)
require.Len(t, results, 2)
assert.Equal(t, "repo-1", results[0].Name)
assert.Equal(t, "repo-2", results[1].Name)
// Both should fail (no remote).
assert.False(t, results[0].Success)
assert.False(t, results[1].Success)
}
func TestPush_Good_WithRemote(t *testing.T) {
// Create a bare remote and a clone.
bareDir, _ := filepath.Abs(t.TempDir())
cloneDir, _ := filepath.Abs(t.TempDir())
cmd := exec.Command("git", "init", "--bare")
cmd.Dir = bareDir
require.NoError(t, cmd.Run())
cmd = exec.Command("git", "clone", bareDir, cloneDir)
require.NoError(t, cmd.Run())
for _, args := range [][]string{
{"git", "config", "user.email", "[email protected]"},
{"git", "config", "user.name", "Test User"},
} {
cmd = exec.Command(args[0], args[1:]...)
cmd.Dir = cloneDir
require.NoError(t, cmd.Run())
}
require.NoError(t, os.WriteFile(filepath.Join(cloneDir, "file.txt"), []byte("v1"), 0644))
for _, args := range [][]string{
{"git", "add", "."},
{"git", "commit", "-m", "initial"},
{"git", "push", "origin", "HEAD"},
} {
cmd = exec.Command(args[0], args[1:]...)
cmd.Dir = cloneDir
out, err := cmd.CombinedOutput()
require.NoError(t, err, "failed: %v: %s", args, string(out))
}
// Make a local commit.
require.NoError(t, os.WriteFile(filepath.Join(cloneDir, "file.txt"), []byte("v2"), 0644))
for _, args := range [][]string{
{"git", "add", "."},
{"git", "commit", "-m", "second commit"},
} {
cmd = exec.Command(args[0], args[1:]...)
cmd.Dir = cloneDir
require.NoError(t, cmd.Run())
}
// Push should succeed.
err := Push(context.Background(), cloneDir)
assert.NoError(t, err)
// Verify ahead count is now 0.
ahead, behind, err := getAheadBehind(context.Background(), cloneDir)
assert.NoError(t, err)
assert.Equal(t, 0, ahead)
assert.Equal(t, 0, behind)
}