package main import ( "errors" "strings" "testing" ) func tags(t *testing.T, names ...string) []version { t.Helper() out := parseTags(names) if len(out) != len(names) { t.Fatalf("invalid tags in %v", names) } return out } func commits(subjects ...string) []commit { var out []commit for i, s := range subjects { subject, body, _ := strings.Cut(s, "\n\n") out = append(out, commit{Hash: strings.Repeat(string(rune('a'+i)), 40), Subject: subject, Body: body}) } return out } func TestNext(t *testing.T) { cases := []struct { name string tags []string commits []string channel string want string }{ {"first release", nil, []string{"feat: monitors"}, "stable", "v0.1.0"}, {"fix", []string{"v0.3.1"}, []string{"fix: retry", "chore: deps"}, "stable", "v0.3.2"}, {"feature", []string{"v0.3.1"}, []string{"fix: retry", "feat(api): paging"}, "stable", "v0.4.0"}, {"breaking before 1.0", []string{"v0.3.1"}, []string{"feat!: new config"}, "stable", "v0.4.0"}, {"breaking footer", []string{"v1.3.1"}, []string{"refactor: config\n\nBREAKING CHANGE: UPTIME_X is gone"}, "stable", "v2.0.0"}, {"breaking after 1.0", []string{"v1.3.1"}, []string{"feat(api)!: drop v0"}, "stable", "v2.0.0"}, {"first beta", []string{"v1.3.1"}, []string{"feat: x"}, "beta", "v1.4.0-beta.1"}, {"next beta", []string{"v1.3.1", "v1.4.0-beta.1", "v1.4.0-beta.2"}, []string{"feat: x", "fix: y"}, "beta", "v1.4.0-beta.3"}, {"stable after betas", []string{"v1.3.1", "v1.4.0-beta.2"}, []string{"feat: x"}, "stable", "v1.4.0"}, {"betas of another version", []string{"v1.3.1", "v1.3.2-beta.4"}, []string{"feat: x"}, "beta", "v1.4.0-beta.1"}, {"perf", []string{"v1.0.0"}, []string{"perf: cache"}, "stable", "v1.0.1"}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { got, err := next(tags(t, c.tags...), commits(c.commits...), c.channel) if err != nil || got.String() != c.want { t.Errorf("next = %v, %v; want %s", got, err, c.want) } }) } if _, err := next(nil, commits("chore: x", "docs: y", "Merge branch"), "stable"); !errors.Is(err, errNothingToRelease) { t.Errorf("nothing to release: err = %v", err) } if _, err := next(nil, commits("feat: x"), "nightly"); err == nil { t.Error("unknown channel accepted") } } func TestVersion(t *testing.T) { for _, bad := range []string{"1.0.0", "v1.0", "v01.0.0", "v1.0.0-rc.1", "v1.0.0-beta.0", "v1.0.0-beta", "v1.0.0\n"} { if _, err := parseVersion(bad); err == nil { t.Errorf("%q accepted", bad) } } got := tags(t, "v1.0.0", "v0.9.0", "v1.0.0-beta.10", "v1.0.0-beta.2", "v0.10.0") var names []string for _, v := range got { names = append(names, v.String()) } if want := "v0.9.0 v0.10.0 v1.0.0-beta.2 v1.0.0-beta.10 v1.0.0"; strings.Join(names, " ") != want { t.Errorf("order = %v, want %s", names, want) } } func TestPrevious(t *testing.T) { all := tags(t, "v0.1.0", "v0.2.0-beta.1", "v0.2.0-beta.2", "v0.2.0", "v0.3.0-beta.1") for tag, want := range map[string]string{ "v0.2.0": "v0.1.0", "v0.2.0-beta.2": "v0.2.0-beta.1", "v0.3.0-beta.1": "v0.2.0", "v0.3.0": "v0.2.0", "v0.2.0-beta.1": "v0.1.0", } { v, _ := parseVersion(tag) if got, ok := previous(all, v); !ok || got.String() != want { t.Errorf("previous(%s) = %v, want %s", tag, got, want) } } first, _ := parseVersion("v0.1.0") if _, ok := previous(all, first); ok { t.Error("the first release has a predecessor") } } func TestNotes(t *testing.T) { v, _ := parseVersion("v0.4.0") prev, _ := parseVersion("v0.3.1") got := notes(v, &prev, commits( "feat(api): keyset paging", "fix: retry DNS", "chore: bump deps", "feat!: config in one file\n\nBREAKING CHANGE: UPTIME_FOO is\nnow UPTIME_BAR.\n\nCo-Authored-By: x", "perf(check): reuse connections", ), "https://git.example/o/r") for _, want := range []string{ "### Breaking changes\n\n- config in one file: UPTIME_FOO is now UPTIME_BAR. ([ddddddd](https://git.example/o/r/commit/" + strings.Repeat("d", 40) + "))", "### Features\n\n- **api:** keyset paging ([aaaaaaa]", "- config in one file ([ddddddd]", "### Fixes\n\n- retry DNS ([bbbbbbb]", "### Performance\n\n- **check:** reuse connections", "[v0.3.1...v0.4.0](https://git.example/o/r/compare/v0.3.1...v0.4.0)", } { if !strings.Contains(got, want) { t.Errorf("notes lack %q:\n%s", want, got) } } if strings.Contains(got, "bump deps") || strings.Contains(got, "Co-Authored") { t.Errorf("notes contain internal changes:\n%s", got) } if got := notes(v, nil, commits("ci: x"), ""); got != "No user-facing changes.\n\n" { t.Errorf("empty notes = %q", got) } }