refactor: let updaters define the files they want to run on (#233)

This change reverses the responsibility for which files the updaters are
run on. Now each updater can specify the list of files and wether the
files should be created when they do not exist yet. This simplifies the
handling of each update in releaserpleaser.go, as we can just iterate
over all updaters and call it for each file of that updater.

Also update the flags to allow users to easily define which updaters
should run.
This commit is contained in:
Julian Tölle 2025-08-23 22:14:34 +02:00 committed by GitHub
parent 1e9e0aa5d9
commit f1aa1a2ef4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 307 additions and 151 deletions

View file

@ -1,18 +1,24 @@
package updater
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPackageJsonUpdater(t *testing.T) {
func TestPackageJsonUpdater_Files(t *testing.T) {
assert.Equal(t, []string{"package.json"}, PackageJson().Files())
}
func TestPackageJsonUpdater_CreateNewFiles(t *testing.T) {
assert.False(t, PackageJson().CreateNewFiles())
}
func TestPackageJsonUpdater_Update(t *testing.T) {
tests := []updaterTestCase{
{
name: "simple package.json",
content: `{"name":"test","version":"1.0.0"}`,
filename: "package.json",
name: "simple package.json",
content: `{"name":"test","version":"1.0.0"}`,
info: ReleaseInfo{
Version: "v2.0.5",
},
@ -20,19 +26,8 @@ func TestPackageJsonUpdater(t *testing.T) {
wantErr: assert.NoError,
},
{
name: "simple package.json, wrong name",
content: `{"name":"test","version":"1.0.0"}`,
filename: "nopackage.json",
info: ReleaseInfo{
Version: "v2.0.5",
},
want: `{"name":"test","version":"1.0.0"}`,
wantErr: assert.NoError,
},
{
name: "complex package.json",
content: "{\n \"name\": \"test\",\n \"version\": \"1.0.0\",\n \"dependencies\": {\n \"foo\": \"^1.0.0\"\n }\n}",
filename: "package.json",
name: "complex package.json",
content: "{\n \"name\": \"test\",\n \"version\": \"1.0.0\",\n \"dependencies\": {\n \"foo\": \"^1.0.0\"\n }\n}",
info: ReleaseInfo{
Version: "v2.0.0",
},
@ -40,9 +35,8 @@ func TestPackageJsonUpdater(t *testing.T) {
wantErr: assert.NoError,
},
{
name: "invalid json",
content: `not json`,
filename: "package.json",
name: "invalid json",
content: `not json`,
info: ReleaseInfo{
Version: "v2.0.0",
},
@ -50,9 +44,8 @@ func TestPackageJsonUpdater(t *testing.T) {
wantErr: assert.NoError,
},
{
name: "json without version",
content: `{"name":"test"}`,
filename: "package.json",
name: "json without version",
content: `{"name":"test"}`,
info: ReleaseInfo{
Version: "v2.0.0",
},
@ -63,8 +56,7 @@ func TestPackageJsonUpdater(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fmt.Println("Running updater test for PackageJson")
runUpdaterTest(t, PackageJson, tt)
runUpdaterTest(t, PackageJson(), tt)
})
}
}