Stitch any CI into Tangled
1package main
2
3// Test fake for KnotConsumer.
4//
5// Lives in a non-_test.go file so it can be referenced from tests across
6// any future test files (and, if we ever split tack into subpackages, can
7// be promoted to an exported helper without moving code around).
8//
9// It does no I/O: AddKnot and RemoveKnot just record the knot they were
10// handed so tests can assert on the side effect.
11
12import (
13 "context"
14 "sync"
15)
16
17// fakeKnotConsumer is an in-memory KnotConsumer suitable for tests. The
18// zero value is ready to use; concurrent calls are safe.
19type fakeKnotConsumer struct {
20 mu sync.Mutex
21 added []string
22 removed []string
23}
24
25// Compile-time interface conformance check — keeps the fake honest if
26// the KnotConsumer surface ever grows a new method.
27var _ KnotConsumer = (*fakeKnotConsumer)(nil)
28
29// AddKnot records the knot for later inspection via Added().
30func (f *fakeKnotConsumer) AddKnot(_ context.Context, knot string) {
31 f.mu.Lock()
32 defer f.mu.Unlock()
33 f.added = append(f.added, knot)
34}
35
36// RemoveKnot records the knot for later inspection via Removed().
37func (f *fakeKnotConsumer) RemoveKnot(_ context.Context, knot string) {
38 f.mu.Lock()
39 defer f.mu.Unlock()
40 f.removed = append(f.removed, knot)
41}
42
43// Added returns a copy of the knots passed to AddKnot, in call order.
44// A copy is returned so callers can't accidentally mutate the fake's
45// internal slice while comparing.
46func (f *fakeKnotConsumer) Added() []string {
47 f.mu.Lock()
48 defer f.mu.Unlock()
49 out := make([]string, len(f.added))
50 copy(out, f.added)
51 return out
52}
53
54// Removed returns a copy of the knots passed to RemoveKnot, in call
55// order. See Added() for the rationale behind copying.
56func (f *fakeKnotConsumer) Removed() []string {
57 f.mu.Lock()
58 defer f.mu.Unlock()
59 out := make([]string, len(f.removed))
60 copy(out, f.removed)
61 return out
62}