-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagefile_test.go
More file actions
132 lines (121 loc) · 4.62 KB
/
Copy pathmagefile_test.go
File metadata and controls
132 lines (121 loc) · 4.62 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
//go:build mage
package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestParseReleaseVersion(t *testing.T) {
t.Parallel()
for input, expected := range map[string]string{
"1.2.3": "1.2.3",
"v1.2.3": "1.2.3",
"1.2.3-rc.2": "1.2.3-rc.2",
"v2.0.0-beta": "2.0.0-beta",
} {
actual, err := parseReleaseVersion(input)
if err != nil {
t.Errorf("parseReleaseVersion(%q) error = %v", input, err)
continue
}
if actual != expected {
t.Errorf("parseReleaseVersion(%q) = %q, want %q", input, actual, expected)
}
}
}
func TestParseReleaseVersionRejectsNonCanonicalOrContainerUnsafeVersions(t *testing.T) {
t.Parallel()
for _, input := range []string{
"", "dev", "1.2", "01.2.3", "1.2.3+build", "v1.2.3+build",
" v1.2.3", "v1.2.3 ", "1.2.3-" + strings.Repeat("a", 129),
} {
if _, err := parseReleaseVersion(input); err == nil {
t.Errorf("parseReleaseVersion(%q) accepted an invalid release", input)
}
}
}
func TestPublishRequiresGitHubActions(t *testing.T) {
t.Setenv(githubActionsEnvironment, "")
err := Publish(context.Background())
if err == nil || !strings.Contains(err.Error(), "GitHub Actions release workflow") {
t.Fatalf("Publish() error = %v, want the GitHub Actions guard", err)
}
}
func TestReleaseConfigurationUsesReplaceableDrafts(t *testing.T) {
t.Parallel()
for _, expected := range []string{"draft: true", "replace_existing_draft: true", "prerelease: auto"} {
if !strings.Contains(goReleaserConfig, expected) {
t.Errorf("GoReleaser configuration does not contain %q", expected)
}
}
}
func TestCurrentReleaseTagRequiresAnAnnotatedSemanticTagAtHead(t *testing.T) {
directory := t.TempDir()
runMageGit(t, directory, "init")
runMageGit(t, directory, "config", "user.name", "Sable Test")
runMageGit(t, directory, "config", "user.email", "sable@example.test")
if err := os.WriteFile(filepath.Join(directory, "README.md"), []byte("release test\n"), 0o600); err != nil {
t.Fatal(err)
}
runMageGit(t, directory, "add", "README.md")
runMageGit(t, directory, "commit", "-m", "initial")
workingDirectory, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
if err := os.Chdir(directory); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chdir(workingDirectory) })
if err := requireCurrentReleaseTag(context.Background(), "v1.2.3"); err == nil {
t.Fatal("requireCurrentReleaseTag accepted a missing release tag")
}
runMageGit(t, directory, "tag", "v1.2.3")
if err := requireCurrentReleaseTag(context.Background(), "v1.2.3"); err == nil || !strings.Contains(err.Error(), "must be annotated") {
t.Fatalf("requireCurrentReleaseTag lightweight-tag error = %v", err)
}
runMageGit(t, directory, "tag", "--delete", "v1.2.3")
runMageGit(t, directory, "tag", "--annotate", "v1.2.3", "--message", "Sable v1.2.3")
if err := requireCurrentReleaseTag(context.Background(), "v1.2.3"); err != nil {
t.Fatalf("requireCurrentReleaseTag() error = %v", err)
}
if err := os.WriteFile(filepath.Join(directory, "README.md"), []byte("new commit\n"), 0o600); err != nil {
t.Fatal(err)
}
runMageGit(t, directory, "commit", "--all", "-m", "advance")
if err := requireCurrentReleaseTag(context.Background(), "v1.2.3"); err == nil || !strings.Contains(err.Error(), "not current commit") {
t.Fatalf("requireCurrentReleaseTag stale-tag error = %v", err)
}
}
func runMageGit(t *testing.T, directory string, arguments ...string) {
t.Helper()
command := exec.Command("git", arguments...)
command.Dir = directory
if output, err := command.CombinedOutput(); err != nil {
t.Fatalf("git %s: %v: %s", strings.Join(arguments, " "), err, output)
}
}
func TestReleaseNotesRequireAnExactNonemptyCuratedSection(t *testing.T) {
t.Parallel()
path := filepath.Join(t.TempDir(), "CHANGELOG.md")
contents := "# Changelog\n\n## [1.2.0] - Unreleased\n\n- A useful improvement.\n\n## [1.2.0-rc.1]\n\n- Candidate notes.\n\n## [1.3.0]\n\n## [1.4.0]\n\n- Later notes.\n"
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
t.Fatal(err)
}
for _, release := range []string{"1.2.0", "v1.2.0", "1.2.0-rc.1", "1.3.0", "1.2", "2.0.0"} {
output, err := exec.Command("bash", "scripts/release-notes.sh", release, path).Output()
valid := release == "1.2.0" || release == "v1.2.0" || release == "1.2.0-rc.1"
if (err == nil) != valid {
t.Errorf("%s notes = %q, %v", release, output, err)
}
if valid && (strings.Contains(string(output), "Later notes") || strings.Contains(string(output), "## [")) {
t.Errorf("notes included another release: %s", output)
}
}
if !strings.Contains(goReleaserConfig, "changelog:\n disable: true") {
t.Fatal("release builds still generate commit lists")
}
}