From 5c29f9641cd8a7045bc8fed5edf039eba1b44415 Mon Sep 17 00:00:00 2001 From: Thomas Rooney Date: Thu, 10 Oct 2024 15:20:07 +0100 Subject: [PATCH] fix: maintain backwards compatibility of RELEASES.md and ensure dir=. when .speakeasy/gen.lock in root of github repo (#174) --- internal/actions/release.go | 43 +++++++++------ internal/actions/release_test.go | 89 ++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 internal/actions/release_test.go diff --git a/internal/actions/release.go b/internal/actions/release.go index d461f778..bf84b360 100644 --- a/internal/actions/release.go +++ b/internal/actions/release.go @@ -57,23 +57,7 @@ func Release() error { } } - for _, file := range files { - // Maintain Support for RELEASES.MD for backward compatibility with existing publishing actions - if strings.Contains(file, "RELEASES.md") { - dir = filepath.Dir(file) - logging.Info("Found RELEASES.md in %s\n", dir) - usingReleasesMd = true - break - } - - if strings.Contains(file, "gen.lock") { - dir = filepath.Dir(file) - dir = strings.ReplaceAll(dir, "/.speakeasy", "") - dir = strings.ReplaceAll(dir, ".speakeasy", "") - logging.Info("Found gen.lock in %s\n", dir) - break - } - } + dir, usingReleasesMd = GetDirAndShouldUseReleasesMD(files, dir, usingReleasesMd) } var latestRelease *releases.ReleasesInfo @@ -111,6 +95,31 @@ func Release() error { return nil } +func GetDirAndShouldUseReleasesMD(files []string, dir string, usingReleasesMd bool) (string, bool) { + for _, file := range files { + // Maintain Support for RELEASES.MD for backward compatibility with existing publishing actions + if strings.Contains(file, "RELEASES.md") { + // file = ./RELEASES.md + // dir = . + dir = filepath.Dir(file) + logging.Info("Found RELEASES.md in %s\n", dir) + usingReleasesMd = true + break + } + + if strings.Contains(file, "gen.lock") { + // file = .speakeasy/gen.lock + dir = filepath.Dir(file) + if strings.Contains(dir, ".speakeasy") { + dir = filepath.Dir(dir) + } + + logging.Info("Found gen.lock in %s\n", dir) + } + } + return dir, usingReleasesMd +} + func addPublishOutputs(dir string, outputs map[string]string) error { wf, err := configuration.GetWorkflowAndValidateLanguages(false) if err != nil { diff --git a/internal/actions/release_test.go b/internal/actions/release_test.go new file mode 100644 index 00000000..91aa42d6 --- /dev/null +++ b/internal/actions/release_test.go @@ -0,0 +1,89 @@ +package actions + +import "testing" + +func TestGetDirAndShouldUseReleasesMD(t *testing.T) { + type args struct { + files []string + dir string + usingReleasesMd bool + } + tests := []struct { + name string + args args + want string + want1 bool + }{ + { + name: "RELEASES.md found", + args: args{ + files: []string{"./RELEASES.md", "some/other/file.go"}, + dir: ".", + usingReleasesMd: false, + }, + want: ".", + want1: true, + }, + { + name: "RELEASES.md found in subdirectory", + args: args{ + files: []string{"subdir/RELEASES.md", "some/other/file.go"}, + dir: ".", + usingReleasesMd: false, + }, + want: "subdir", + want1: true, + }, + { + name: "gen.lock found", + args: args{ + files: []string{".speakeasy/gen.lock", "some/other/file.go"}, + dir: ".", + usingReleasesMd: false, + }, + want: ".", + want1: false, + }, + { + name: "gen.lock found in subdirectory", + args: args{ + files: []string{"subdir/.speakeasy/gen.lock", "some/other/file.go"}, + dir: ".", + usingReleasesMd: false, + }, + want: "subdir", + want1: false, + }, + { + name: "no relevant files found", + args: args{ + files: []string{"some/file.go", "another/file.js"}, + dir: ".", + usingReleasesMd: false, + }, + want: ".", + want1: false, + }, + { + name: "RELEASES.md takes precedence over gen.lock", + args: args{ + files: []string{".speakeasy/gen.lock", "RELEASES.md"}, + dir: ".", + usingReleasesMd: false, + }, + want: ".", + want1: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1 := GetDirAndShouldUseReleasesMD(tt.args.files, tt.args.dir, tt.args.usingReleasesMd) + if got != tt.want { + t.Errorf("GetDirAndShouldUseReleasesMD() got = %v, want %v", got, tt.want) + } + if got1 != tt.want1 { + t.Errorf("GetDirAndShouldUseReleasesMD() got1 = %v, want %v", got1, tt.want1) + } + }) + } +}