forked from gastownhall/beads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvc_embedded_test.go
More file actions
231 lines (199 loc) · 6.84 KB
/
vc_embedded_test.go
File metadata and controls
231 lines (199 loc) · 6.84 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
//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdVC runs "bd vc" with extra args. Returns combined output.
func bdVC(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"vc"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd vc %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
// bdVCFail runs "bd vc" expecting failure.
func bdVCFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"vc"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd vc %s should have failed, got: %s", strings.Join(args, " "), out)
}
return string(out)
}
func TestEmbeddedVC(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt vc tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("status", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcst")
out := bdVC(t, bd, dir, "status")
if !strings.Contains(out, "main") {
t.Errorf("expected 'main' branch in status, got: %s", out)
}
})
t.Run("status_json", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcstj")
out := bdVC(t, bd, dir, "status", "--json")
var result map[string]interface{}
if err := json.Unmarshal([]byte(out), &result); err != nil {
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
}
if branch, _ := result["branch"].(string); branch != "main" {
t.Errorf("expected branch='main', got %q", branch)
}
if commit, _ := result["commit"].(string); commit == "" {
t.Error("expected non-empty commit hash")
}
})
t.Run("commit_with_message", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vccm")
// bd create auto-commits, so vc commit may see "nothing to commit".
// Both outcomes are valid.
bdCreateSilent(t, bd, dir, "commit test issue")
cmd := exec.Command(bd, "vc", "commit", "-m", "test commit message")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(out), "nothing to commit") {
t.Fatalf("bd vc commit failed unexpectedly: %v\n%s", err, out)
}
})
t.Run("commit_json", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vccj")
// bd create auto-commits, so vc commit may see "nothing to commit".
// Both committed=true and committed=false are valid outcomes.
bdCreateSilent(t, bd, dir, "commit json issue")
cmd := exec.Command(bd, "vc", "commit", "-m", "json commit", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd vc commit --json failed unexpectedly: %v\n%s", err, out)
}
// Verify valid JSON with committed field (true or false are both valid)
var result map[string]interface{}
if jsonErr := json.Unmarshal(out, &result); jsonErr != nil {
t.Fatalf("failed to parse JSON: %v\n%s", jsonErr, out)
}
if _, ok := result["committed"]; !ok {
t.Error("expected 'committed' field in JSON output")
}
})
t.Run("commit_stdin", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vccs")
bdCreateSilent(t, bd, dir, "stdin commit issue")
cmd := exec.Command(bd, "vc", "commit", "--stdin")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
cmd.Stdin = strings.NewReader("message from stdin")
out, err := cmd.CombinedOutput()
if err != nil && !strings.Contains(string(out), "nothing to commit") {
t.Fatalf("bd vc commit --stdin failed unexpectedly: %v\n%s", err, out)
}
})
t.Run("commit_no_message", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcnm")
out := bdVCFail(t, bd, dir, "commit")
if !strings.Contains(out, "commit message is required") {
t.Errorf("expected 'commit message is required', got: %s", out)
}
})
t.Run("merge_no_conflicts", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcmrg")
// Create a branch, switch to it, create an issue, switch back, merge
bdBranch(t, bd, dir, "feature-test")
// Create an issue on main (auto-committed)
bdCreateSilent(t, bd, dir, "main branch issue")
// Merge the feature branch (which is at the same base commit)
out := bdVC(t, bd, dir, "merge", "feature-test")
if !strings.Contains(out, "merged") && !strings.Contains(out, "Merged") {
t.Errorf("expected merge success message, got: %s", out)
}
})
t.Run("merge_json", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "vcmj")
bdBranch(t, bd, dir, "merge-json-branch")
out := bdVC(t, bd, dir, "merge", "merge-json-branch", "--json")
var result map[string]interface{}
if err := json.Unmarshal([]byte(out), &result); err != nil {
t.Fatalf("failed to parse JSON: %v\n%s", err, out)
}
if merged, _ := result["merged"].(string); merged != "merge-json-branch" {
t.Errorf("expected merged='merge-json-branch', got %q", merged)
}
})
}
func TestEmbeddedVCConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt vc tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "vcconc")
const numWorkers = 10
type result struct {
worker int
out string
err error
}
// Each worker creates an issue then commits
results := make([]result, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
// Create an issue
createCmd := exec.Command(bd, "create", "--silent", fmt.Sprintf("vc-conc-issue-%d", worker))
createCmd.Dir = dir
createCmd.Env = bdEnv(dir)
if out, err := createCmd.CombinedOutput(); err != nil {
results[worker] = result{worker: worker, out: string(out), err: err}
return
}
// Try to commit
cmd := exec.Command(bd, "vc", "commit", "-m", fmt.Sprintf("worker %d commit", worker))
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
results[worker] = result{worker: worker, out: string(out), err: err}
}(w)
}
wg.Wait()
successes := 0
for _, r := range results {
if strings.Contains(r.out, "panic") {
t.Errorf("worker %d panicked:\n%s", r.worker, r.out)
}
if r.err == nil {
successes++
} else if strings.Contains(r.out, "one writer at a time") ||
strings.Contains(r.out, "nothing to commit") {
// Expected: auto-commit means explicit commit often has nothing left
successes++ // still counts as successful execution
} else {
t.Errorf("worker %d failed with unexpected error: %v\n%s", r.worker, r.err, r.out)
}
}
if successes < 1 {
t.Errorf("expected at least 1 successful commit, got %d", successes)
}
t.Logf("%d/%d vc commit workers succeeded", successes, numWorkers)
}