-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_top_level_test.go
More file actions
473 lines (396 loc) · 15.9 KB
/
Copy pathhelpers_top_level_test.go
File metadata and controls
473 lines (396 loc) · 15.9 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package lsremote
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hiddeco/go-ls-remote/pktline"
"github.com/hiddeco/go-ls-remote/transport"
httpt "github.com/hiddeco/go-ls-remote/transport/http"
)
// closeRecordingTransport wraps the HTTP [transport.Transport] so a
// test can observe how many times the returned [transport.Conn] is
// closed. The wrapping is transparent: `Open` delegates to the inner
// transport and yields a [closeRecordingConn] over the resulting
// [transport.Conn] with a shared atomic counter. The counter is
// exposed via [closeRecordingTransport.closeCount].
type closeRecordingTransport struct {
inner transport.Transport
count atomic.Int64
}
// newCloseRecordingRegistry returns a `*transport.Registry` that
// resolves `https` and `http` to a `closeRecordingTransport` wrapping
// the package's default HTTP transport, plus the recording wrapper
// itself so the caller can read `closeCount()`.
func newCloseRecordingRegistry(t *testing.T) (*transport.Registry, *closeRecordingTransport) {
t.Helper()
rec := &closeRecordingTransport{inner: httpt.New()}
return transport.NewRegistry(rec), rec
}
func (r *closeRecordingTransport) Schemes() []string { return r.inner.Schemes() }
func (r *closeRecordingTransport) Open(ctx context.Context, u *transport.URL,
opts transport.OpenOptions,
) (transport.Conn, error) {
conn, err := r.inner.Open(ctx, u, opts)
if err != nil {
return nil, err
}
return &closeRecordingConn{inner: conn, count: &r.count}, nil
}
func (r *closeRecordingTransport) closeCount() int { return int(r.count.Load()) }
// closeRecordingConn delegates every method to the inner
// [transport.Conn] and increments the shared counter on `Close`. The
// counter increment lives outside `inner.Close()` so a re-entrant
// `Close` (the idempotency contract permits one) still records the
// extra call — which is what the test wants to assert against.
type closeRecordingConn struct {
inner transport.Conn
count *atomic.Int64
}
func (c *closeRecordingConn) Advertisement() *pktline.Reader { return c.inner.Advertisement() }
func (c *closeRecordingConn) Command(ctx context.Context, name string,
body transport.CommandBody,
) (*pktline.Reader, error) {
return c.inner.Command(ctx, name, body)
}
func (c *closeRecordingConn) Close() error {
c.count.Add(1)
return c.inner.Close()
}
// TestRefs_topLevel pins the one-shot `Refs` helper's happy path: it
// dials the in-process v2 server, returns an iterator over the
// advertised refs, and yields at least HEAD and `refs/heads/main` with
// no error.
func TestRefs_topLevel(t *testing.T) {
t.Parallel()
store, _ := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
seq, err := Refs(t.Context(), srv.URL+"/repo.git", RefsRequest{})
require.NoError(t, err)
var got []Ref
for ref, err := range seq {
require.NoError(t, err)
got = append(got, ref)
}
require.NotEmpty(t, got)
var sawHEAD, sawMain bool
for _, r := range got {
switch r.Name {
case "HEAD":
sawHEAD = true
case "refs/heads/main":
sawMain = true
}
}
assert.True(t, sawHEAD, "the one-shot Refs must yield HEAD")
assert.True(t, sawMain, "the one-shot Refs must yield refs/heads/main")
}
// TestRefs_topLevel_closesSessionOnDrain pins the lifecycle contract:
// after the caller drains the iterator returned by `Refs`, the
// underlying Session must close its `transport.Conn` exactly once. The
// test plugs a `closeRecordingTransport` into the registry so it can
// observe the `Close` count without reaching into Session internals.
func TestRefs_topLevel_closesSessionOnDrain(t *testing.T) {
t.Parallel()
store, _ := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
reg, rec := newCloseRecordingRegistry(t)
seq, err := Refs(t.Context(), srv.URL+"/repo.git",
RefsRequest{}, WithTransports(reg))
require.NoError(t, err)
var n int
for _, err := range seq {
require.NoError(t, err)
n++
}
assert.Positive(t, n, "the iterator must yield at least one ref")
assert.Equal(t, 1, rec.closeCount(),
"the underlying Conn must be closed exactly once when the iter drains")
}
// TestRefs_topLevel_closesSessionOnEarlyStop pins that an early `break`
// from the caller's `for range` still closes the Session: the wrapper's
// `defer Close` must fire when `yield` returns false.
func TestRefs_topLevel_closesSessionOnEarlyStop(t *testing.T) {
t.Parallel()
store, _ := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
reg, rec := newCloseRecordingRegistry(t)
seq, err := Refs(t.Context(), srv.URL+"/repo.git",
RefsRequest{}, WithTransports(reg))
require.NoError(t, err)
// Take exactly one ref and bail.
var taken int
for range seq {
taken++
break
}
assert.Equal(t, 1, taken, "the iter must yield at least one ref before the early stop")
assert.Equal(t, 1, rec.closeCount(),
"an early-stop iter must still close the underlying Conn")
}
// TestRefs_topLevel_dialError propagates a dial-time failure verbatim:
// the helper returns `(nil, err)` and does not open or close any
// Session.
func TestRefs_topLevel_dialError(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
srv := httptest.NewServer(mux)
defer srv.Close()
seq, err := Refs(t.Context(), srv.URL+"/repo.git", RefsRequest{})
assert.Nil(t, seq)
require.Error(t, err)
assert.ErrorIs(t, err, ErrNotFound,
"a 404 on the discovery probe must reach ErrNotFound via errors.Is")
}
// TestListRefs_topLevel pins the slice-collecting one-shot: it returns
// every advertised ref in a single slice, and the in-process server's
// HEAD and main both appear.
func TestListRefs_topLevel(t *testing.T) {
t.Parallel()
store, _ := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
refs, err := ListRefs(t.Context(), srv.URL+"/repo.git", RefsRequest{})
require.NoError(t, err)
require.NotEmpty(t, refs)
names := make([]string, 0, len(refs))
for _, r := range refs {
names = append(names, r.Name)
}
assert.Contains(t, names, "HEAD")
assert.Contains(t, names, "refs/heads/main")
}
// TestObjectInfos_topLevel pins the one-shot `object-info` helper:
// against a v2 server, a query for a packed commit OID with `Size:
// true` returns one row whose Hash matches and whose Size is positive.
func TestObjectInfos_topLevel(t *testing.T) {
t.Parallel()
store, commitOID := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
got, err := ObjectInfos(t.Context(), srv.URL+"/repo.git",
[]string{commitOID}, ObjectInfoRequest{Size: true})
require.NoError(t, err)
require.Len(t, got, 1)
assert.Equal(t, commitOID, got[0].Hash)
assert.Positive(t, got[0].Size)
}
// TestExists_success pins the success branch: a reachable repository
// produces `(true, nil)`.
func TestExists_success(t *testing.T) {
t.Parallel()
store, _ := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
ok, err := Exists(t.Context(), srv.URL+"/repo.git")
require.NoError(t, err)
assert.True(t, ok, "a reachable v2 repo must produce Exists=true")
}
// TestExists_notFound pins the `ErrNotFound` branch: a 404 on the
// discovery probe collapses to `(false, nil)` without surfacing the
// underlying error.
func TestExists_notFound(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
srv := httptest.NewServer(mux)
defer srv.Close()
ok, err := Exists(t.Context(), srv.URL+"/repo.git")
require.NoError(t, err, "ErrNotFound must collapse to (false, nil), not propagate")
assert.False(t, ok)
}
// TestExists_otherError pins the catch-all branch: an error that does
// NOT match `ErrNotFound` propagates verbatim with `(false, err)`. A
// malformed URL — empty rawURL — triggers `transport.ParseURL` to
// return `transport.ErrEmptyURL`, which is not an `ErrNotFound`.
func TestExists_otherError(t *testing.T) {
t.Parallel()
ok, err := Exists(t.Context(), "")
require.Error(t, err)
assert.False(t, ok)
assert.NotErrorIs(t, err, ErrNotFound,
"the empty-URL error must not collapse to ErrNotFound")
}
// TestDefaultBranch_v2 pins the v2 path: the helper issues `ls-refs`
// with `ref-prefix HEAD` and `symrefs`, finds HEAD's symref target, and
// returns `refs/heads/main`.
func TestDefaultBranch_v2(t *testing.T) {
t.Parallel()
store, _ := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
got, err := DefaultBranch(t.Context(), srv.URL+"/repo.git")
require.NoError(t, err)
assert.Equal(t, "refs/heads/main", got)
}
// TestDefaultBranch_v0 pins the v0 fallback: a v0 server advertises
// `symref=HEAD:refs/heads/main` on its capability list, and the helper
// resolves the target without issuing any command.
func TestDefaultBranch_v0(t *testing.T) {
t.Parallel()
store, _ := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV0(t, store, "/repo.git"))
defer srv.Close()
got, err := DefaultBranch(t.Context(), srv.URL+"/repo.git")
require.NoError(t, err)
assert.Equal(t, "refs/heads/main", got)
}
// TestDefaultBranch_v2_unborn pins that an unborn HEAD on a v2 server
// surfaces its symref target rather than collapsing to
// [ErrNoDefaultBranch]. Canonical Git's discovery client passes the
// `unborn` argument on its `ls-refs` request ([connect.c:591-592]),
// gated by the server's `ls-refs=unborn` capability advertisement;
// without it, the server's [ls-refs.c:135-136] skips HEAD entirely on
// an unborn repository. `DefaultBranch` must mirror canonical Git and
// set `Unborn: true` on its internal request so the symref target is
// reported on every repository state — born, detached, or unborn.
//
// [connect.c:591-592]: https://github.com/git/git/blob/v2.54.0/connect.c#L591-L592
// [ls-refs.c:135-136]: https://github.com/git/git/blob/v2.54.0/ls-refs.c#L135-L136
func TestDefaultBranch_v2_unborn(t *testing.T) {
t.Parallel()
store := openFixtureStore(t, "unborn-head")
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
got, err := DefaultBranch(t.Context(), srv.URL+"/repo.git")
require.NoError(t, err,
"unborn HEAD must surface its symref target, not ErrNoDefaultBranch")
assert.Equal(t, "refs/heads/main", got,
"the unborn-head fixture's HEAD points at refs/heads/main")
}
// TestDefaultBranch_v2_noSymref pins the error path for a v2 server
// that does not attach a `symref-target:` attribute to HEAD and has no
// capability-level symref entry. The helper must return a
// [*ProtocolError] whose chain matches [ErrNoDefaultBranch] (the
// repository is reachable but HEAD has no symbolic target) and must NOT
// match [ErrNotFound] (which is reserved for "repository missing").
// The [*ProtocolError.Op] must be `"ls-refs"` because the failure is
// detected after the `ls-refs` command exchange.
func TestDefaultBranch_v2_noSymref(t *testing.T) {
t.Parallel()
conn := &commandStubConn{
adv: pktline.NewReader(bytes.NewReader(buildV2Advertisement(t))),
cmdRdr: pktline.NewReader(bytes.NewReader(buildV2LSRefsNoSymrefResponse(t))),
}
capt := &captureTransport{schemes: []string{"https"}, conn: conn}
reg := transport.NewRegistry(capt)
_, err := DefaultBranch(t.Context(), "https://example.com/repo.git",
WithTransports(reg))
require.Error(t, err)
require.ErrorIs(t, err, ErrNoDefaultBranch,
"v2 headless must match ErrNoDefaultBranch via errors.Is; got %v", err)
require.NotErrorIs(t, err, ErrNotFound,
"v2 headless must NOT match ErrNotFound; got %v", err)
var pe *ProtocolError
require.ErrorAs(t, err, &pe,
"v2 headless must surface as *ProtocolError; got %T", err)
assert.Equal(t, "ls-refs", pe.Op,
"v2 failure is detected after the ls-refs exchange; Op must be \"ls-refs\"")
}
// TestDefaultBranch_v0_noSymref pins the error path for a v0 server
// whose capability list omits the `symref=HEAD:...` entry. The helper
// must return [ErrNoDefaultBranch] and the [*ProtocolError.Op] must be
// `"advertisement"` because the failure is detected at capability-scan
// time, not via a command.
func TestDefaultBranch_v0_noSymref(t *testing.T) {
t.Parallel()
conn := &stubConn{
adv: pktline.NewReader(bytes.NewReader(buildV0NoSymrefAdvertisement(t))),
}
capt := &captureTransport{schemes: []string{"https"}, conn: conn}
reg := transport.NewRegistry(capt)
_, err := DefaultBranch(t.Context(), "https://example.com/repo.git",
WithTransports(reg))
require.Error(t, err)
require.ErrorIs(t, err, ErrNoDefaultBranch,
"v0 headless must match ErrNoDefaultBranch via errors.Is; got %v", err)
require.NotErrorIs(t, err, ErrNotFound,
"v0 headless must NOT match ErrNotFound; got %v", err)
var pe *ProtocolError
require.ErrorAs(t, err, &pe,
"v0 headless must surface as *ProtocolError; got %T", err)
assert.Equal(t, "advertisement", pe.Op,
"v0 failure is detected at advertisement time; Op must be \"advertisement\"")
}
// TestDefaultBranch_repoNotFound pins that a dial failure caused by a
// missing repository (404 on the discovery probe) still surfaces
// [ErrNotFound], NOT [ErrNoDefaultBranch]. The two sentinels are
// mutually exclusive: [ErrNotFound] means the repository itself is
// absent, while [ErrNoDefaultBranch] means the repository is present
// but HEAD has no symbolic target.
func TestDefaultBranch_repoNotFound(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
srv := httptest.NewServer(mux)
defer srv.Close()
_, err := DefaultBranch(t.Context(), srv.URL+"/repo.git")
require.Error(t, err)
require.ErrorIs(t, err, ErrNotFound,
"a 404 on discovery must reach ErrNotFound via errors.Is; got %v", err)
assert.NotErrorIs(t, err, ErrNoDefaultBranch,
"a 404 on discovery must NOT match ErrNoDefaultBranch; got %v", err)
}
// TestTags_topLevel pins the `Tags` shorthand: it restricts the
// response to `refs/tags/` and asks for peeled values. The
// `packed-only` fixture has an annotated `refs/tags/v1` with a peel
// recorded in `packed-refs`, so the response carries `Peeled` populated.
func TestTags_topLevel(t *testing.T) {
t.Parallel()
store := openFixtureStore(t, "packed-only")
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
seq, err := Tags(t.Context(), srv.URL+"/repo.git")
require.NoError(t, err)
var got []Ref
for ref, err := range seq {
require.NoError(t, err)
got = append(got, ref)
}
require.NotEmpty(t, got, "the packed-only fixture has refs/tags/v1")
var sawTag bool
for _, r := range got {
assert.True(t, len(r.Name) >= len("refs/tags/") && r.Name[:len("refs/tags/")] == "refs/tags/",
"every Tags ref must live under refs/tags/; got %q", r.Name)
if r.Name == "refs/tags/v1" {
sawTag = true
assert.NotEmpty(t, r.Peeled,
"an annotated tag fetched via Tags must carry a non-empty Peeled value")
}
}
assert.True(t, sawTag, "the packed-only fixture must surface refs/tags/v1")
}
// TestHeads_topLevel pins the `Heads` shorthand: it restricts the
// response to `refs/heads/` so neither HEAD nor any tag survives.
func TestHeads_topLevel(t *testing.T) {
t.Parallel()
store, _ := openObjectInfoFixture(t)
srv := httptest.NewServer(serveHandlerV2(t, store, "/repo.git"))
defer srv.Close()
seq, err := Heads(t.Context(), srv.URL+"/repo.git")
require.NoError(t, err)
var got []Ref
for ref, err := range seq {
require.NoError(t, err)
got = append(got, ref)
}
require.NotEmpty(t, got)
for _, r := range got {
assert.True(t, len(r.Name) >= len("refs/heads/") && r.Name[:len("refs/heads/")] == "refs/heads/",
"Heads must restrict to refs/heads/; got %q", r.Name)
}
}