-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
526 lines (470 loc) · 16.5 KB
/
cli_test.go
File metadata and controls
526 lines (470 loc) · 16.5 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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package main_test
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"testing"
)
func TestCLIBinaryIntegration(t *testing.T) {
// 1. Build the CLI binary.
// Create a temporary directory for the build.
tmpBuildDir, err := os.MkdirTemp("", "goversion_build")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpBuildDir)
// The built binary will be written to "goversion" in tmpBuildDir.
binPath := filepath.Join(tmpBuildDir, "goversion")
// Build the CLI binary from the main package.
// Since this test resides in cmd/integration, the main package is in its parent directory ("../").
buildCmd := exec.Command("go", "build", "-o", binPath, "./")
buildOutput, err := buildCmd.CombinedOutput()
if err != nil {
t.Fatalf("failed to build CLI binary: %v; build output: %s", err, string(buildOutput))
}
// 2. Set up a temporary git repository for testing.
tmpRepo, err := os.MkdirTemp("", "goversion_integration")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpRepo)
// Initialize a new git repository.
initCmd := exec.Command("git", "init")
initCmd.Dir = tmpRepo
if output, err := initCmd.CombinedOutput(); err != nil {
t.Fatalf("git init failed: %v; output: %s", err, string(output))
}
// Configure git user.
configCmds := [][]string{
{"git", "config", "user.email", "test@example.com"},
{"git", "config", "user.name", "Test User"},
}
for _, args := range configCmds {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = tmpRepo
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git config failed: %v; output: %s", err, string(output))
}
}
// 3. Create the version file in a pkg directory.
pkgDir := filepath.Join(tmpRepo, "pkg")
if err := os.MkdirAll(pkgDir, 0755); err != nil {
t.Fatalf("failed to create pkg directory: %v", err)
}
versionFilePath := filepath.Join(pkgDir, "version.go")
initialVersionContent := `package version
var (
Version = "1.2.3"
)
`
if err := os.WriteFile(versionFilePath, []byte(initialVersionContent), 0644); err != nil {
t.Fatalf("failed to write version file: %v", err)
}
// 4. Stage and commit the initial version file.
gitAddCmd := exec.Command("git", "add", ".")
gitAddCmd.Dir = tmpRepo
if output, err := gitAddCmd.CombinedOutput(); err != nil {
t.Fatalf("git add failed: %v; output: %s", err, string(output))
}
gitCommitCmd := exec.Command("git", "commit", "-m", "initial commit")
gitCommitCmd.Dir = tmpRepo
if output, err := gitCommitCmd.CombinedOutput(); err != nil {
t.Fatalf("git commit failed: %v; output: %s", err, string(output))
}
// 5. Run the CLI binary.
// Use the -version-file flag to point to our version file and use "patch" as the positional argument.
cliCmd := exec.Command(binPath, "-version-file", versionFilePath, "patch")
cliCmd.Dir = tmpRepo
var cliStdout, cliStderr bytes.Buffer
cliCmd.Stdout = &cliStdout
cliCmd.Stderr = &cliStderr
if err := cliCmd.Run(); err != nil {
t.Fatalf("CLI command failed: %v; stdout: %s; stderr: %s", err, cliStdout.String(), cliStderr.String())
}
// 6. Verify the version file was updated to "1.2.4".
updatedContent, err := os.ReadFile(versionFilePath)
if err != nil {
t.Fatalf("failed to read version file: %v", err)
}
if !strings.Contains(string(updatedContent), `Version = "1.2.4"`) {
t.Errorf("version file not updated; expected 'Version = \"1.2.4\"' in content, got:\n%s", string(updatedContent))
}
// 7. Verify that a git tag "v1.2.4" was created.
gitTagCmd := exec.Command("git", "tag")
gitTagCmd.Dir = tmpRepo
tagOutput, err := gitTagCmd.CombinedOutput()
if err != nil {
t.Fatalf("git tag command failed: %v; output: %s", err, string(tagOutput))
}
tags := strings.Split(strings.TrimSpace(string(tagOutput)), "\n")
expectedTag := "v1.2.4"
if !slices.Contains(tags, expectedTag) {
t.Errorf("expected git tag %q not found; got tags: %v", expectedTag, tags)
}
}
// TestCLIBinaryMajorBumpIntegration builds the goversion CLI and then
// runs a major bump against a temp repo with both version.go and go.mod,
// asserting that version.go and go.mod are updated and that a v2.0.0 tag is created.
func TestCLIBinaryMajorBumpIntegration(t *testing.T) {
// 1. Build the CLI binary into a temp directory.
tmpBuildDir, err := os.MkdirTemp("", "goversion_build_major")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpBuildDir)
binPath := filepath.Join(tmpBuildDir, "goversion")
buildCmd := exec.Command("go", "build", "-o", binPath, "./")
if out, err := buildCmd.CombinedOutput(); err != nil {
t.Fatalf("failed to build CLI binary: %v; output: %s", err, out)
}
// 2. Initialize a fresh git repo for the test.
tmpRepo, err := os.MkdirTemp("", "goversion_major_integration")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpRepo)
runGit := func(args ...string) {
cmd := exec.Command("git", args...)
cmd.Dir = tmpRepo
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
}
runGit("init")
runGit("config", "user.email", "test@example.com")
runGit("config", "user.name", "Test User")
// 3. Create go.mod at the repo root.
modContent := `module example.com/m
go 1.18
`
if err := os.WriteFile(filepath.Join(tmpRepo, "go.mod"), []byte(modContent), 0644); err != nil {
t.Fatalf("failed to write go.mod: %v", err)
}
// 4. Create version.go in pkg/.
pkgDir := filepath.Join(tmpRepo, "pkg")
if err := os.MkdirAll(pkgDir, 0755); err != nil {
t.Fatalf("failed to mkdir pkg: %v", err)
}
relVer := filepath.Join("pkg", "version.go")
absVer := filepath.Join(tmpRepo, relVer)
initial := `package version
var (
Version = "1.2.3"
)
`
if err := os.WriteFile(absVer, []byte(initial), 0644); err != nil {
t.Fatalf("failed to write version.go: %v", err)
}
// 5. Commit both files.
runGit("add", ".")
runGit("commit", "-m", "initial commit")
// 6. Run the CLI binary with "major".
cmd := exec.Command(binPath, "-version-file", relVer, "major")
cmd.Dir = tmpRepo
cmd.Env = append(os.Environ(),
"GO_HELPER_PROCESS=1",
"GIT_AUTHOR_NAME=Test User",
"GIT_AUTHOR_EMAIL=test@example.com",
"GIT_COMMITTER_NAME=Test User",
"GIT_COMMITTER_EMAIL=test@example.com",
)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("CLI major bump failed: %v\nstdout: %s\nstderr: %s", err, stdout.String(), stderr.String())
}
// 7. Verify version.go updated to 2.0.0.
updatedVer, err := os.ReadFile(absVer)
if err != nil {
t.Fatalf("reading version.go failed: %v", err)
}
if !strings.Contains(string(updatedVer), `Version = "2.0.0"`) {
t.Errorf("version.go not updated; got:\n%s", updatedVer)
}
// 8. Verify go.mod module line now includes /v2.
modGot, err := os.ReadFile(filepath.Join(tmpRepo, "go.mod"))
if err != nil {
t.Fatalf("reading go.mod failed: %v", err)
}
first := strings.SplitN(string(modGot), "\n", 2)[0]
if !strings.Contains(first, "/v2") {
t.Errorf("go.mod module line = %q; want it to include \"/v2\"", first)
}
// 9. Verify git tag "v2.0.0" exists in this repo.
tagCmd := exec.Command("git", "tag")
tagCmd.Dir = tmpRepo
tagOutput, err := tagCmd.CombinedOutput()
if err != nil {
t.Fatalf("git tag failed: %v; output: %s", err, tagOutput)
}
tags := strings.Split(strings.TrimSpace(string(tagOutput)), "\n")
if !slices.Contains(tags, "v2.0.0") {
t.Errorf("expected git tag v2.0.0; got tags: %v", tags)
}
}
// TestCLIBumpFileIntegration tests the -bump-file flag functionality.
func TestCLIBumpFileIntegration(t *testing.T) {
// Build the CLI binary
tmpBuildDir, err := os.MkdirTemp("", "goversion_bumpfile_build")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpBuildDir)
binPath := filepath.Join(tmpBuildDir, "goversion")
buildCmd := exec.Command("go", "build", "-o", binPath, "./")
if out, err := buildCmd.CombinedOutput(); err != nil {
t.Fatalf("failed to build CLI binary: %v; output: %s", err, out)
}
// Set up test repository
tmpRepo, err := os.MkdirTemp("", "goversion_bumpfile_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpRepo)
// Initialize git
initCmd := exec.Command("git", "init")
initCmd.Dir = tmpRepo
if output, err := initCmd.CombinedOutput(); err != nil {
t.Fatalf("git init failed: %v; output: %s", err, string(output))
}
// Configure git
configCmds := [][]string{
{"git", "config", "user.email", "test@example.com"},
{"git", "config", "user.name", "Test User"},
}
for _, args := range configCmds {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = tmpRepo
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git config failed: %v; output: %s", err, string(output))
}
}
// Create version.go
versionFile := filepath.Join(tmpRepo, "version.go")
versionContent := `package main
var (
Version = "1.2.3"
)
`
if err := os.WriteFile(versionFile, []byte(versionContent), 0644); err != nil {
t.Fatalf("failed to write version file: %v", err)
}
// Create package.json
packageFile := filepath.Join(tmpRepo, "package.json")
packageContent := `{
"name": "test-package",
"version": "1.2.3",
"dependencies": {
"express": "4.18.0",
"lodash": "4.17.21"
}
}`
if err := os.WriteFile(packageFile, []byte(packageContent), 0644); err != nil {
t.Fatalf("failed to write package.json: %v", err)
}
// Create Cargo.toml
cargoFile := filepath.Join(tmpRepo, "Cargo.toml")
cargoContent := `[package]
name = "test-crate"
version = "1.2.3"
[dependencies]
serde = "1.0.130"
tokio = { version = "1.21.0", features = ["full"] }`
if err := os.WriteFile(cargoFile, []byte(cargoContent), 0644); err != nil {
t.Fatalf("failed to write Cargo.toml: %v", err)
}
// Stage and commit initial files
gitAddCmd := exec.Command("git", "add", ".")
gitAddCmd.Dir = tmpRepo
if output, err := gitAddCmd.CombinedOutput(); err != nil {
t.Fatalf("git add failed: %v; output: %s", err, string(output))
}
gitCommitCmd := exec.Command("git", "commit", "-m", "initial commit")
gitCommitCmd.Dir = tmpRepo
if output, err := gitCommitCmd.CombinedOutput(); err != nil {
t.Fatalf("git commit failed: %v; output: %s", err, string(output))
}
// Run CLI with bump-file flags
cliCmd := exec.Command(binPath, "-version-file", versionFile, "-bump-file", packageFile, "-bump-file", cargoFile, "minor")
cliCmd.Dir = tmpRepo
var stdout, stderr bytes.Buffer
cliCmd.Stdout = &stdout
cliCmd.Stderr = &stderr
if err := cliCmd.Run(); err != nil {
t.Fatalf("CLI command failed: %v; stdout: %s; stderr: %s", err, stdout.String(), stderr.String())
}
// Verify version.go was updated
versionResult, err := os.ReadFile(versionFile)
if err != nil {
t.Fatalf("failed to read version file: %v", err)
}
if !strings.Contains(string(versionResult), `Version = "1.3.0"`) {
t.Errorf("version.go not updated correctly; got:\n%s", versionResult)
}
// Verify package.json was updated
packageResult, err := os.ReadFile(packageFile)
if err != nil {
t.Fatalf("failed to read package.json: %v", err)
}
if !strings.Contains(string(packageResult), `"version": "1.3.0"`) {
t.Errorf("package.json not updated correctly; got:\n%s", packageResult)
}
// Verify dependencies unchanged
if !strings.Contains(string(packageResult), `"express": "4.18.0"`) {
t.Errorf("package.json dependencies were modified")
}
// Verify Cargo.toml was updated
cargoResult, err := os.ReadFile(cargoFile)
if err != nil {
t.Fatalf("failed to read Cargo.toml: %v", err)
}
if !strings.Contains(string(cargoResult), `version = "1.3.0"`) {
t.Errorf("Cargo.toml not updated correctly; got:\n%s", cargoResult)
}
// Verify dependencies unchanged
if !strings.Contains(string(cargoResult), `serde = "1.0.130"`) {
t.Errorf("Cargo.toml dependencies were modified")
}
// Verify git tag
gitTagCmd := exec.Command("git", "tag")
gitTagCmd.Dir = tmpRepo
tagOutput, err := gitTagCmd.CombinedOutput()
if err != nil {
t.Fatalf("git tag command failed: %v; output: %s", err, string(tagOutput))
}
tags := strings.Split(strings.TrimSpace(string(tagOutput)), "\n")
if !slices.Contains(tags, "v1.3.0") {
t.Errorf("expected git tag v1.3.0 not found; got tags: %v", tags)
}
}
// TestCLIPostBumpIntegration tests the -post-bump flag functionality.
func TestCLIPostBumpIntegration(t *testing.T) {
// Build the CLI binary
tmpBuildDir, err := os.MkdirTemp("", "goversion_postbump_build")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpBuildDir)
binPath := filepath.Join(tmpBuildDir, "goversion")
buildCmd := exec.Command("go", "build", "-o", binPath, "./")
if out, err := buildCmd.CombinedOutput(); err != nil {
t.Fatalf("failed to build CLI binary: %v; output: %s", err, out)
}
// Set up test repository
tmpRepo, err := os.MkdirTemp("", "goversion_postbump_cli_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpRepo)
// Initialize git
initCmd := exec.Command("git", "init")
initCmd.Dir = tmpRepo
if output, err := initCmd.CombinedOutput(); err != nil {
t.Fatalf("git init failed: %v; output: %s", err, string(output))
}
// Configure git
configCmds := [][]string{
{"git", "config", "user.email", "test@example.com"},
{"git", "config", "user.name", "Test User"},
}
for _, args := range configCmds {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = tmpRepo
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git config failed: %v; output: %s", err, string(output))
}
}
// Create version.go
versionFile := filepath.Join(tmpRepo, "version.go")
versionContent := `package main
var (
Version = "1.0.0"
)
`
if err := os.WriteFile(versionFile, []byte(versionContent), 0644); err != nil {
t.Fatalf("failed to write version file: %v", err)
}
// Create a post-bump script that generates a file
scriptFile := filepath.Join(tmpRepo, "update-docs.sh")
scriptContent := `#!/bin/sh
cat > VERSION.md << EOF
# Version Information
Current Version: $GOVERSION_NEW_VERSION
Previous Version: $GOVERSION_OLD_VERSION
EOF
echo "Generated VERSION.md"
`
if err := os.WriteFile(scriptFile, []byte(scriptContent), 0755); err != nil {
t.Fatalf("failed to write post-bump script: %v", err)
}
// Stage and commit initial files
gitAddCmd := exec.Command("git", "add", ".")
gitAddCmd.Dir = tmpRepo
if output, err := gitAddCmd.CombinedOutput(); err != nil {
t.Fatalf("git add failed: %v; output: %s", err, string(output))
}
gitCommitCmd := exec.Command("git", "commit", "-m", "initial commit")
gitCommitCmd.Dir = tmpRepo
if output, err := gitCommitCmd.CombinedOutput(); err != nil {
t.Fatalf("git commit failed: %v; output: %s", err, string(output))
}
// Run CLI with post-bump flag and -file for the generated VERSION.md
cliCmd := exec.Command(binPath, "-version-file", versionFile, "-post-bump", scriptFile, "-file", filepath.Join(tmpRepo, "VERSION.md"), "minor")
cliCmd.Dir = tmpRepo
var stdout, stderr bytes.Buffer
cliCmd.Stdout = &stdout
cliCmd.Stderr = &stderr
if err := cliCmd.Run(); err != nil {
t.Fatalf("CLI command failed: %v; stdout: %s; stderr: %s", err, stdout.String(), stderr.String())
}
// Verify the script output was shown
if !strings.Contains(stdout.String(), "Generated VERSION.md") {
t.Errorf("expected script output in stdout; got: %s", stdout.String())
}
// Verify version.go was updated
versionResult, err := os.ReadFile(versionFile)
if err != nil {
t.Fatalf("failed to read version file: %v", err)
}
if !strings.Contains(string(versionResult), `Version = "1.1.0"`) {
t.Errorf("version.go not updated correctly; got:\n%s", versionResult)
}
// Verify VERSION.md was created by the script
versionMdPath := filepath.Join(tmpRepo, "VERSION.md")
versionMdContent, err := os.ReadFile(versionMdPath)
if err != nil {
t.Fatalf("VERSION.md not created by post-bump script: %v", err)
}
expectedContent := `# Version Information
Current Version: 1.1.0
Previous Version: 1.0.0
`
if string(versionMdContent) != expectedContent {
t.Errorf("VERSION.md content mismatch; got:\n%s\nwant:\n%s", versionMdContent, expectedContent)
}
// Verify git tag
gitTagCmd := exec.Command("git", "tag")
gitTagCmd.Dir = tmpRepo
tagOutput, err := gitTagCmd.CombinedOutput()
if err != nil {
t.Fatalf("git tag command failed: %v; output: %s", err, string(tagOutput))
}
tags := strings.Split(strings.TrimSpace(string(tagOutput)), "\n")
if !slices.Contains(tags, "v1.1.0") {
t.Errorf("expected git tag v1.1.0 not found; got tags: %v", tags)
}
// Verify VERSION.md was committed
logCmd := exec.Command("git", "log", "--name-only", "--oneline", "-1")
logCmd.Dir = tmpRepo
logOutput, err := logCmd.CombinedOutput()
if err != nil {
t.Fatalf("git log failed: %v; output: %s", err, string(logOutput))
}
if !strings.Contains(string(logOutput), "VERSION.md") {
t.Errorf("VERSION.md was not included in commit; log output:\n%s", logOutput)
}
}