Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Release Notes Generator - Test cases for main.go and ref.go #11882

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions hack/tools/release/notes/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,138 @@ func Test_defaultBranchForNewTag(t *testing.T) {
})
}
}

func Test_validateConfig(t *testing.T) {
tests := []struct {
name string
args *notesCmdConfig
wantErr bool
errorMessage string
}{
{
name: "Missing fromRef or newTag when branch is set only",
args: &notesCmdConfig{
branch: "main",
},
wantErr: true,
errorMessage: "at least one of --from or --release need to be set",
},
{
name: "Missing branch or newTag when fromRef is set only",
args: &notesCmdConfig{
fromRef: "ref1/tags",
},
wantErr: true,
errorMessage: "at least one of --branch or --release need to be set",
},
{
name: "Invalid fromRef",
args: &notesCmdConfig{
fromRef: "invalid",
branch: "main",
},
wantErr: true,
errorMessage: "invalid ref invalid: must follow [type]/[value]",
},
{
name: "Invalid toRef",
args: &notesCmdConfig{
toRef: "invalid",
branch: "main",
fromRef: "ref1/tags",
},
wantErr: true,
errorMessage: "invalid ref invalid: must follow [type]/[value]",
},
{
name: "Valid fromRef, toRef, and newTag",
args: &notesCmdConfig{
fromRef: "ref1/tags",
toRef: "ref2/tags",
newTag: "v1.0.0",
},
wantErr: false,
},
{
name: "Missing branch when newTag is set",
args: &notesCmdConfig{
branch: "main",
toRef: "ref2/tags",
newTag: "v1.0.0",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateConfig(tt.args)
if tt.wantErr {
if err == nil || err.Error() != tt.errorMessage {
t.Errorf("expected error '%s', got '%v'", tt.errorMessage, err)
}
} else if err != nil {
t.Errorf("unexpected error: %v", err)
}
})
}
}

func Test_computeConfigDefaults(t *testing.T) {
tests := []struct {
name string
args *notesCmdConfig
want *notesCmdConfig
wantErr bool
}{
{
name: "Calculate fromRef when newTag is a new minor release and toRef",
args: &notesCmdConfig{
branch: "develop",
newTag: "v1.1.0",
},
want: &notesCmdConfig{
fromRef: "tags/v1.0.0",
branch: "develop",
toRef: "heads/develop",
newTag: "v1.1.0",
},
wantErr: false,
},
{
name: "Calculate fromRef when newTag is not a new minor release, branch and toRef",
args: &notesCmdConfig{
newTag: "v1.1.3",
},
want: &notesCmdConfig{
fromRef: "tags/v1.1.2",
branch: "release-1.1",
toRef: "heads/release-1.1",
newTag: "v1.1.3",
},
wantErr: false,
},
{
name: "Fail when newTag is not a valid semver",
args: &notesCmdConfig{
newTag: "invalid-tag",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := computeConfigDefaults(tt.args)
g := NewWithT(t)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("invalid --release, is not a semver:"))
} else {
g.Expect(err).ToNot(HaveOccurred())
g.Expect(tt.args.fromRef).To(Equal(tt.want.fromRef))
g.Expect(tt.args.branch).To(Equal(tt.want.branch))
g.Expect(tt.args.toRef).To(Equal(tt.want.toRef))
}
})
}
}