mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-02-06 17:57:05 +00:00
refactor: move things to packages
This commit is contained in:
parent
44184a77f9
commit
11063b2d2c
32 changed files with 923 additions and 892 deletions
32
internal/updater/changelog.go
Normal file
32
internal/updater/changelog.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const (
|
||||
ChangelogHeader = "# Changelog"
|
||||
ChangelogFile = "CHANGELOG.md"
|
||||
)
|
||||
|
||||
var (
|
||||
ChangelogUpdaterHeaderRegex = regexp.MustCompile(`^# Changelog\n`)
|
||||
)
|
||||
|
||||
func Changelog(info ReleaseInfo) Updater {
|
||||
return func(content string) (string, error) {
|
||||
headerIndex := ChangelogUpdaterHeaderRegex.FindStringIndex(content)
|
||||
if headerIndex == nil && len(content) != 0 {
|
||||
return "", fmt.Errorf("unexpected format of CHANGELOG.md, header does not match")
|
||||
}
|
||||
if headerIndex != nil {
|
||||
// Remove the header from the content
|
||||
content = content[headerIndex[1]:]
|
||||
}
|
||||
|
||||
content = ChangelogHeader + "\n\n" + info.ChangelogEntry + content
|
||||
|
||||
return content, nil
|
||||
}
|
||||
}
|
||||
60
internal/updater/changelog_test.go
Normal file
60
internal/updater/changelog_test.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestChangelogUpdater_UpdateContent(t *testing.T) {
|
||||
tests := []updaterTestCase{
|
||||
{
|
||||
name: "empty file",
|
||||
content: "",
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n"},
|
||||
want: "# Changelog\n\n## v1.0.0\n",
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "well-formatted changelog",
|
||||
content: `# Changelog
|
||||
|
||||
## v0.0.1
|
||||
|
||||
- Bazzle
|
||||
|
||||
## v0.1.0
|
||||
|
||||
### Bazuuum
|
||||
`,
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
||||
want: `# Changelog
|
||||
|
||||
## v1.0.0
|
||||
|
||||
- Version 1, juhu.
|
||||
|
||||
## v0.0.1
|
||||
|
||||
- Bazzle
|
||||
|
||||
## v0.1.0
|
||||
|
||||
### Bazuuum
|
||||
`,
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "error on invalid header",
|
||||
content: "What even is this file?",
|
||||
info: ReleaseInfo{ChangelogEntry: "## v1.0.0\n\n- Version 1, juhu.\n"},
|
||||
want: "",
|
||||
wantErr: assert.Error,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
runUpdaterTest(t, Changelog, tt)
|
||||
})
|
||||
}
|
||||
}
|
||||
17
internal/updater/generic.go
Normal file
17
internal/updater/generic.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var GenericUpdaterSemVerRegex = regexp.MustCompile(`\d+\.\d+\.\d+(-[\w.]+)?(.*x-releaser-pleaser-version)`)
|
||||
|
||||
func Generic(info ReleaseInfo) Updater {
|
||||
return func(content string) (string, error) {
|
||||
// We strip the "v" prefix to avoid adding/removing it from the users input.
|
||||
version := strings.TrimPrefix(info.Version, "v")
|
||||
|
||||
return GenericUpdaterSemVerRegex.ReplaceAllString(content, version+"${2}"), nil
|
||||
}
|
||||
}
|
||||
53
internal/updater/generic_test.go
Normal file
53
internal/updater/generic_test.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGenericUpdater_UpdateContent(t *testing.T) {
|
||||
tests := []updaterTestCase{
|
||||
{
|
||||
name: "single line",
|
||||
content: "v1.0.0 // x-releaser-pleaser-version",
|
||||
info: ReleaseInfo{
|
||||
Version: "v1.2.0",
|
||||
},
|
||||
want: "v1.2.0 // x-releaser-pleaser-version",
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "multiline line",
|
||||
content: "Foooo\n\v1.2.0\nv1.0.0 // x-releaser-pleaser-version\n",
|
||||
info: ReleaseInfo{
|
||||
Version: "v1.2.0",
|
||||
},
|
||||
want: "Foooo\n\v1.2.0\nv1.2.0 // x-releaser-pleaser-version\n",
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "invalid existing version",
|
||||
content: "1.0 // x-releaser-pleaser-version",
|
||||
info: ReleaseInfo{
|
||||
Version: "v1.2.0",
|
||||
},
|
||||
want: "1.0 // x-releaser-pleaser-version",
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
{
|
||||
name: "complicated line",
|
||||
content: "version: v1.2.0-alpha.1 => Awesome, isnt it? x-releaser-pleaser-version foobar",
|
||||
info: ReleaseInfo{
|
||||
Version: "v1.2.0",
|
||||
},
|
||||
want: "version: v1.2.0 => Awesome, isnt it? x-releaser-pleaser-version foobar",
|
||||
wantErr: assert.NoError,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
runUpdaterTest(t, Generic, tt)
|
||||
})
|
||||
}
|
||||
}
|
||||
19
internal/updater/updater.go
Normal file
19
internal/updater/updater.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package updater
|
||||
|
||||
type ReleaseInfo struct {
|
||||
Version string
|
||||
ChangelogEntry string
|
||||
}
|
||||
|
||||
type Updater func(string) (string, error)
|
||||
|
||||
type NewUpdater func(ReleaseInfo) Updater
|
||||
|
||||
func WithInfo(info ReleaseInfo, constructors ...NewUpdater) []Updater {
|
||||
updaters := make([]Updater, 0, len(constructors))
|
||||
for _, constructor := range constructors {
|
||||
updaters = append(updaters, constructor(info))
|
||||
}
|
||||
|
||||
return updaters
|
||||
}
|
||||
26
internal/updater/updater_test.go
Normal file
26
internal/updater/updater_test.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type updaterTestCase struct {
|
||||
name string
|
||||
content string
|
||||
info ReleaseInfo
|
||||
want string
|
||||
wantErr assert.ErrorAssertionFunc
|
||||
}
|
||||
|
||||
func runUpdaterTest(t *testing.T, constructor NewUpdater, tt updaterTestCase) {
|
||||
t.Helper()
|
||||
|
||||
got, err := constructor(tt.info)(tt.content)
|
||||
if !tt.wantErr(t, err, fmt.Sprintf("Updater(%v, %v)", tt.content, tt.info)) {
|
||||
return
|
||||
}
|
||||
assert.Equalf(t, tt.want, got, "Updater(%v, %v)", tt.content, tt.info)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue