-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecsync_test.go
More file actions
274 lines (246 loc) · 7.71 KB
/
Copy pathspecsync_test.go
File metadata and controls
274 lines (246 loc) · 7.71 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
package specsync
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
func TestLoadChanges(t *testing.T) {
root := t.TempDir()
cdir := filepath.Join(root, "changes", "my-change")
mustWrite(t, filepath.Join(cdir, "proposal.md"), "# My Title\n\nthe body\n")
mustWrite(t, filepath.Join(cdir, "tasks.md"), "- [ ] do the thing\n")
mustWrite(t, filepath.Join(cdir, ".status"), "planned\n")
mustWrite(t, filepath.Join(cdir, ".specsync", "metadata.json"), `{"version":1,"priority":2}`)
// An archived change projects as closed.
adir := filepath.Join(root, "changes", "archive", "old-change")
mustWrite(t, filepath.Join(adir, "proposal.md"), "# Old\n")
changes, err := LoadChanges(root)
if err != nil {
t.Fatalf("LoadChanges: %v", err)
}
if len(changes) != 2 {
t.Fatalf("want 2 changes, got %d", len(changes))
}
byslug := map[string]Change{}
for _, c := range changes {
byslug[c.Slug] = c
}
c := byslug["my-change"]
if c.Title != "My Title" {
t.Errorf("title = %q, want %q", c.Title, "My Title")
}
if c.Stage != "planned" {
t.Errorf("stage = %q, want planned (from .status)", c.Stage)
}
if c.Priority == nil || *c.Priority != 2 {
var got int
if c.Priority != nil {
got = *c.Priority
}
t.Errorf("priority = %d, want 2", got)
}
if c.Archived {
t.Errorf("my-change should not be archived")
}
if !strings.Contains(c.TasksMarkdown, "do the thing") {
t.Errorf("tasks not loaded: %q", c.TasksMarkdown)
}
old := byslug["old-change"]
if !old.Archived || old.Stage != StageArchived {
t.Errorf("old-change should be archived, got archived=%v stage=%q", old.Archived, old.Stage)
}
}
func TestGitHubPushCreate(t *testing.T) {
var calls [][]string
p := &GitHubProvider{run: func(_ context.Context, args ...string) (string, error) {
calls = append(calls, args)
switch {
case args[0] == "issue" && args[1] == "list":
return "[]", nil // no existing issue
case args[0] == "issue" && args[1] == "create":
return "https://github.com/o/r/issues/7", nil
default:
return "", nil
}
}}
ref, err := p.Push(context.Background(), WorkItem{
Slug: "my-change", Title: "T", Body: "B", Stage: "planned", Priority: 2,
}, nil)
if err != nil {
t.Fatalf("Push: %v", err)
}
if ref.ID != "7" {
t.Errorf("ref.ID = %q, want 7", ref.ID)
}
create := findCall(calls, "issue", "create")
if create == nil {
t.Fatal("no issue create call")
}
body := flagValue(create, "--body")
if !strings.Contains(body, "specsync:change=my-change") {
t.Errorf("body missing identity marker: %q", body)
}
if !hasLabel(create, "stage:planned") || !hasLabel(create, "priority:2") {
t.Errorf("create missing expected labels: %v", create)
}
}
func TestGitHubPushUpdateReconcilesLabels(t *testing.T) {
var editArgs []string
p := &GitHubProvider{run: func(_ context.Context, args ...string) (string, error) {
switch {
case args[0] == "issue" && args[1] == "view":
// current labels include a stale stage that must be removed.
return `{"labels":[{"name":"specsync"},{"name":"stage:triaged"}]}`, nil
case args[0] == "issue" && args[1] == "edit":
editArgs = args
return "", nil
default:
return "", nil
}
}}
_, err := p.Push(context.Background(), WorkItem{
Slug: "my-change", Title: "T", Body: "B", Stage: "planned",
}, &Ref{Provider: "github", ID: "7"})
if err != nil {
t.Fatalf("Push: %v", err)
}
if !hasFlagValue(editArgs, "--add-label", "stage:planned") {
t.Errorf("expected add stage:planned, got %v", editArgs)
}
if !hasFlagValue(editArgs, "--remove-label", "stage:triaged") {
t.Errorf("expected remove stale stage:triaged, got %v", editArgs)
}
}
func TestGitHubPushReopensManagedActiveIssue(t *testing.T) {
var calls [][]string
p := &GitHubProvider{run: func(_ context.Context, args ...string) (string, error) {
calls = append(calls, args)
if args[0] == "issue" && args[1] == "view" {
return `{"state":"CLOSED","labels":[{"name":"stage:complete"}]}`, nil
}
return "", nil
}}
_, err := p.Push(context.Background(), WorkItem{
Slug: "my-change", Title: "T", Stage: StageActive, ManageClosed: true,
}, &Ref{Provider: "github", ID: "7"})
if err != nil {
t.Fatalf("Push: %v", err)
}
if findCall(calls, "issue", "reopen", "7") == nil {
t.Fatalf("expected managed active issue to reopen; calls: %v", calls)
}
}
func TestGitHubFindRequiresExactMarker(t *testing.T) {
p := &GitHubProvider{run: func(_ context.Context, args ...string) (string, error) {
if args[0] == "issue" && args[1] == "list" {
// First hit is a fuzzy body match, second is the exact marker.
return `[
{"number":1,"url":"https://github.com/o/r/issues/1","body":"mentions specsync:change=issue-first-intake in prose"},
{"number":2,"url":"https://github.com/o/r/issues/2","body":"<!-- specsync:change=issue-first-intake -->\n\nreal marker"}
]`, nil
}
return "", nil
}}
ref, err := p.Find(context.Background(), "issue-first-intake")
if err != nil {
t.Fatalf("Find: %v", err)
}
if ref == nil {
t.Fatal("Find returned nil ref, want exact marker match")
}
if ref.ID != "2" {
t.Fatalf("ref.ID = %q, want 2", ref.ID)
}
}
func TestGitHubFindReturnsNilWithoutExactMarker(t *testing.T) {
p := &GitHubProvider{run: func(_ context.Context, args ...string) (string, error) {
if args[0] == "issue" && args[1] == "list" {
return `[
{"number":1,"url":"https://github.com/o/r/issues/1","body":"specsync:change=issue-first-intake appears only in text"}
]`, nil
}
return "", nil
}}
ref, err := p.Find(context.Background(), "issue-first-intake")
if err != nil {
t.Fatalf("Find: %v", err)
}
if ref != nil {
t.Fatalf("Find returned ref %#v, want nil without exact marker", ref)
}
}
// stubProvider returns a fixed ref and records nothing else.
type stubProvider struct{ ref Ref }
func (s stubProvider) Name() string { return "github" }
func (s stubProvider) Push(context.Context, WorkItem, *Ref) (Ref, error) {
return s.ref, nil
}
func (s stubProvider) Find(context.Context, string) (*Ref, error) { return nil, nil }
func TestDryRunDoesNotWriteCache(t *testing.T) {
root := t.TempDir()
cdir := filepath.Join(root, "changes", "c1")
mustWrite(t, filepath.Join(cdir, "proposal.md"), "# C1\n")
prov := stubProvider{ref: Ref{Provider: "github", ID: "0"}}
if _, err := Sync(context.Background(), Options{OpenSpecDir: root, Provider: prov, DryRun: true}); err != nil {
t.Fatalf("dry-run Sync: %v", err)
}
if _, err := os.Stat(refCachePath(cdir)); !os.IsNotExist(err) {
t.Fatalf("dry-run must not write the ref cache, but %s exists", refCachePath(cdir))
}
// A real run must write it.
if _, err := Sync(context.Background(), Options{OpenSpecDir: root, Provider: prov}); err != nil {
t.Fatalf("real Sync: %v", err)
}
if _, err := os.Stat(refCachePath(cdir)); err != nil {
t.Fatalf("real run should write the ref cache: %v", err)
}
}
// --- helpers ---
func mustWrite(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}
func findCall(calls [][]string, prefix ...string) []string {
for _, c := range calls {
if len(c) < len(prefix) {
continue
}
match := true
for i, p := range prefix {
if c[i] != p {
match = false
break
}
}
if match {
return c
}
}
return nil
}
func flagValue(args []string, flag string) string {
for i := 0; i < len(args)-1; i++ {
if args[i] == flag {
return args[i+1]
}
}
return ""
}
func hasFlagValue(args []string, flag, value string) bool {
for i := 0; i < len(args)-1; i++ {
if args[i] == flag && args[i+1] == value {
return true
}
}
return false
}
func hasLabel(args []string, label string) bool {
return hasFlagValue(args, "--label", label)
}