Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.idea/
/dist/
since
.claude/*.local.json
138 changes: 138 additions & 0 deletions changelog/read_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package changelog

import (
"os"
"path"
"reflect"
"testing"
)

func TestResolveChangelogFile(t *testing.T) {
tests := []struct {
name string
dir string
fileName string
want string
}{
{
name: "simple filename",
dir: "/repo",
fileName: "CHANGELOG.md",
want: "/repo/CHANGELOG.md",
},
{
name: "absolute path with forward slash",
dir: "/repo",
fileName: "/other/CHANGELOG.md",
want: "/other/CHANGELOG.md",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ResolveChangelogFile(tt.dir, tt.fileName); got != tt.want {
t.Errorf("ResolveChangelogFile() = %v, want %v", got, tt.want)
}
})
}
}

func TestReadFile(t *testing.T) {
dir := t.TempDir()
filePath := path.Join(dir, "test.md")
err := os.WriteFile(filePath, []byte("line1\nline2\nline3"), 0644)
if err != nil {
t.Fatal(err)
}

got, err := ReadFile(filePath)
if err != nil {
t.Fatalf("ReadFile() error = %v", err)
}
want := []string{"line1", "line2", "line3"}
if !reflect.DeepEqual(got, want) {
t.Errorf("ReadFile() = %v, want %v", got, want)
}
}

func TestReadFile_nonExistent(t *testing.T) {
_, err := ReadFile("/nonexistent/file.md")
if err == nil {
t.Error("ReadFile() expected error for non-existent file")
}
}

func TestParseChangelog(t *testing.T) {
dir := t.TempDir()
filePath := path.Join(dir, "CHANGELOG.md")
content := `# Changelog

## [1.0.0] - 2024-01-01
### Added
- feat: something new

## [0.9.0] - 2023-12-01
### Fixed
- fix: old bug
`
err := os.WriteFile(filePath, []byte(content), 0644)
if err != nil {
t.Fatal(err)
}

got, err := ParseChangelog(filePath, "1.0.0", false)
if err != nil {
t.Fatalf("ParseChangelog() error = %v", err)
}
want := []string{"### Added", "- feat: something new"}
if !reflect.DeepEqual(got, want) {
t.Errorf("ParseChangelog() = %v, want %v", got, want)
}
}

func TestParseChangelog_withHeader(t *testing.T) {
dir := t.TempDir()
filePath := path.Join(dir, "CHANGELOG.md")
content := `# Changelog

## [1.0.0] - 2024-01-01
### Added
- feat: something new

## [0.9.0] - 2023-12-01
### Fixed
- fix: old bug
`
err := os.WriteFile(filePath, []byte(content), 0644)
if err != nil {
t.Fatal(err)
}

got, err := ParseChangelog(filePath, "1.0.0", true)
if err != nil {
t.Fatalf("ParseChangelog() error = %v", err)
}
if len(got) == 0 {
t.Fatal("ParseChangelog() returned empty result")
}
if got[0] != "## [1.0.0] - 2024-01-01" {
t.Errorf("ParseChangelog() first line = %v, want version header", got[0])
}
}

func Test_readChanges_firstVersion(t *testing.T) {
lines := []string{
"# Changelog",
"",
"## [1.0.0] - 2024-01-01",
"### Added",
"- feat: foo",
"",
"## [0.9.0] - 2023-12-01",
"- fix: bar",
}
got := readChanges(lines, "", false)
want := []string{"### Added", "- feat: foo"}
if !reflect.DeepEqual(got, want) {
t.Errorf("readChanges() = %v, want %v", got, want)
}
}
54 changes: 54 additions & 0 deletions changelog/write_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package changelog

import (
"os"
"path"
"testing"
)

func TestWriteChangelog(t *testing.T) {
dir := t.TempDir()
filePath := path.Join(dir, "CHANGELOG.md")

content := "# Changelog\n\n## [1.0.0]\n- feat: foo\n"
err := WriteChangelog(filePath, content)
if err != nil {
t.Fatalf("WriteChangelog() error = %v", err)
}

got, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
// WriteChangelog appends a newline
want := content + "\n"
if string(got) != want {
t.Errorf("WriteChangelog() file content = %q, want %q", string(got), want)
}
}

func TestWriteChangelog_overwrite(t *testing.T) {
dir := t.TempDir()
filePath := path.Join(dir, "CHANGELOG.md")

// write initial content
err := os.WriteFile(filePath, []byte("old content"), 0644)
if err != nil {
t.Fatal(err)
}

newContent := "# New Changelog"
err = WriteChangelog(filePath, newContent)
if err != nil {
t.Fatalf("WriteChangelog() error = %v", err)
}

got, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
want := newContent + "\n"
if string(got) != want {
t.Errorf("WriteChangelog() file content = %q, want %q", string(got), want)
}
}
52 changes: 52 additions & 0 deletions convcommits/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package convcommits

import (
"reflect"
"sort"
"testing"
)

Expand Down Expand Up @@ -56,3 +57,54 @@ func TestCategoriseByType(t *testing.T) {
})
}
}

func TestCategoriseByType_scopedCommits(t *testing.T) {
commits := []string{"feat(api): add endpoint", "fix(ui): correct layout"}
got := CategoriseByType(commits)

if len(got) != 2 {
t.Fatalf("expected 2 categories, got %d", len(got))
}
if _, ok := got["feat"]; !ok {
t.Errorf("expected 'feat' category, got keys: %v", got)
}
if _, ok := got["fix"]; !ok {
t.Errorf("expected 'fix' category, got keys: %v", got)
}
}

func TestCategoriseByType_breakingChange(t *testing.T) {
commits := []string{"feat!: breaking feature"}
got := CategoriseByType(commits)

if _, ok := got["BREAKING CHANGE"]; !ok {
t.Errorf("expected 'BREAKING CHANGE' category, got keys: %v", got)
}
}

func TestCategoriseByType_noPrefix(t *testing.T) {
commits := []string{"some random commit message"}
got := CategoriseByType(commits)

if _, ok := got[""]; !ok {
t.Errorf("expected empty prefix category, got keys: %v", got)
}
}

func TestDetermineTypes(t *testing.T) {
commits := []string{"feat: foo", "fix: bar", "feat: baz"}
got := DetermineTypes(commits)
sort.Strings(got)

want := []string{"feat", "fix"}
sort.Strings(want)

if len(got) != len(want) {
t.Fatalf("DetermineTypes() length = %d, want %d", len(got), len(want))
}
for i := range got {
if got[i] != want[i] {
t.Errorf("DetermineTypes()[%d] = %v, want %v", i, got[i], want[i])
}
}
}
77 changes: 77 additions & 0 deletions semver/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,80 @@ func TestDetermineChangeType(t *testing.T) {
})
}
}

func TestGetNextVersion_withVPrefix(t *testing.T) {
got := GetNextVersion("1.2.3", true, []string{"feat: new feature"})
want := "v1.3.0"
if got != want {
t.Errorf("GetNextVersion() with vPrefix = %v, want %v", got, want)
}
}

func TestGetNextVersion_noChanges(t *testing.T) {
got := GetNextVersion("1.2.3", false, []string{"unknown: something"})
want := ""
if got != want {
t.Errorf("GetNextVersion() with no recognised changes = %v, want %v", got, want)
}
}

func TestDetermineChangeType_allPatchTypes(t *testing.T) {
patchTypes := []string{"build", "chore", "ci", "docs", "fix", "refactor", "security", "style", "test"}
for _, pt := range patchTypes {
t.Run(pt, func(t *testing.T) {
got := DetermineChangeType([]string{pt})
if got != ComponentPatch {
t.Errorf("DetermineChangeType(%v) = %v, want %v", pt, got, ComponentPatch)
}
})
}
}

func TestDetermineChangeType_none(t *testing.T) {
got := DetermineChangeType([]string{"unknown"})
if got != ComponentNone {
t.Errorf("DetermineChangeType(unknown) = %v, want %v", got, ComponentNone)
}
}

func TestDetermineChangeType_majorTakesPrecedence(t *testing.T) {
got := DetermineChangeType([]string{"feat", "BREAKING CHANGE", "fix"})
if got != ComponentMajor {
t.Errorf("DetermineChangeType() = %v, want %v", got, ComponentMajor)
}
}

func TestBumpVersion(t *testing.T) {
tests := []struct {
name string
version string
component Component
want string
}{
{
name: "bump major resets minor and patch",
version: "1.5.9",
component: ComponentMajor,
want: "2.0.0",
},
{
name: "bump minor resets patch",
version: "1.5.9",
component: ComponentMinor,
want: "1.6.0",
},
{
name: "bump patch only",
version: "1.5.9",
component: ComponentPatch,
want: "1.5.10",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := bumpVersion(tt.version, tt.component); got != tt.want {
t.Errorf("bumpVersion() = %v, want %v", got, tt.want)
}
})
}
}
Loading
Loading