-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_noop_test.go
More file actions
130 lines (112 loc) · 4 KB
/
cli_noop_test.go
File metadata and controls
130 lines (112 loc) · 4 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
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func runCommand(t *testing.T, dir string, name string, args ...string) string {
t.Helper()
cmd := exec.Command(name, args...)
cmd.Dir = dir
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s %v failed: %v\n%s", name, args, err, output)
}
return string(output)
}
func TestNoOpRunDoesNotChangeGitState(t *testing.T) {
binary := buildBinary(t)
tmpDir := t.TempDir()
runCommand(t, tmpDir, "git", "init", "-b", "main")
runCommand(t, tmpDir, "git", "config", "user.name", "Repver Test")
runCommand(t, tmpDir, "git", "config", "user.email", "repver@example.com")
repverContent := `commands:
- name: "goversion"
targets:
- path: "version.txt"
pattern: "^version: (?P<version>.*)$"
git:
create_branch: true
branch_name: "release-{{version}}"
commit: true
commit_message: "Update version to {{version}}"
return_to_original_branch: true
delete_branch: true
`
if err := os.WriteFile(filepath.Join(tmpDir, ".repver"), []byte(repverContent), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(tmpDir, "version.txt"), []byte("version: 1.2.3\n"), 0644); err != nil {
t.Fatal(err)
}
runCommand(t, tmpDir, "git", "add", ".")
runCommand(t, tmpDir, "git", "commit", "-m", "Initial commit")
beforeBranch := strings.TrimSpace(runCommand(t, tmpDir, "git", "rev-parse", "--abbrev-ref", "HEAD"))
cmd := exec.Command(binary, "--command=goversion", "--param-version=1.2.3")
cmd.Dir = tmpDir
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("repver returned error: %v\n%s", err, output)
}
if !strings.Contains(string(output), "No updates needed; target files already match the requested values.") {
t.Fatalf("expected no-op message, got:\n%s", output)
}
afterBranch := strings.TrimSpace(runCommand(t, tmpDir, "git", "rev-parse", "--abbrev-ref", "HEAD"))
if afterBranch != beforeBranch {
t.Fatalf("expected branch to remain %q, got %q", beforeBranch, afterBranch)
}
status := strings.TrimSpace(runCommand(t, tmpDir, "git", "status", "--porcelain"))
if status != "" {
t.Fatalf("expected clean git status, got:\n%s", status)
}
branches := runCommand(t, tmpDir, "git", "branch", "--list", "release-1.2.3")
if strings.TrimSpace(branches) != "" {
t.Fatalf("expected no release branch to be created, got:\n%s", branches)
}
}
func TestNoOpDryRunDoesNotPrintActionPreview(t *testing.T) {
binary := buildBinary(t)
tmpDir := t.TempDir()
runCommand(t, tmpDir, "git", "init", "-b", "main")
runCommand(t, tmpDir, "git", "config", "user.name", "Repver Test")
runCommand(t, tmpDir, "git", "config", "user.email", "repver@example.com")
repverContent := `commands:
- name: "goversion"
targets:
- path: "version.txt"
pattern: "^version: (?P<version>.*)$"
git:
create_branch: true
branch_name: "release-{{version}}"
commit: true
commit_message: "Update version to {{version}}"
return_to_original_branch: true
delete_branch: true
`
if err := os.WriteFile(filepath.Join(tmpDir, ".repver"), []byte(repverContent), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(tmpDir, "version.txt"), []byte("version: 1.2.3\n"), 0644); err != nil {
t.Fatal(err)
}
runCommand(t, tmpDir, "git", "add", ".")
runCommand(t, tmpDir, "git", "commit", "-m", "Initial commit")
cmd := exec.Command(binary, "--command=goversion", "--param-version=1.2.3", "--dry-run")
cmd.Dir = tmpDir
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("repver returned error: %v\n%s", err, output)
}
got := string(output)
if !strings.Contains(got, "No updates needed; target files already match the requested values.") {
t.Fatalf("expected no-op message, got:\n%s", got)
}
if strings.Contains(got, "[DRYRUN]") {
t.Fatalf("expected no dry-run action preview for a no-op, got:\n%s", got)
}
if strings.Contains(got, "DRY RUN MODE ENABLED") {
t.Fatalf("expected no dry-run banner for a no-op, got:\n%s", got)
}
}