Skip to content

Commit 28a7da6

Browse files
chore: move GetExecutablePath from config to command (#3383)
Migrates GetExecutablePath from config package to command package, to keep config as only a struct container. Fixes #3377 --------- Signed-off-by: ldetmer <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 0c594e9 commit 28a7da6

File tree

7 files changed

+79
-81
lines changed

7 files changed

+79
-81
lines changed

internal/command/command.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ func RunWithEnv(ctx context.Context, env map[string]string, command string, arg
4545
}
4646
return nil
4747
}
48+
49+
// GetExecutablePath finds the path for a given command, checking for an
50+
// override in the provided commandOverrides map first.
51+
func GetExecutablePath(commandOverrides map[string]string, commandName string) string {
52+
if exe, ok := commandOverrides[commandName]; ok {
53+
return exe
54+
}
55+
return commandName
56+
}

internal/command/command_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
"fmt"
1919
"strings"
2020
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
"github.com/googleapis/librarian/internal/config"
2124
)
2225

2326
func TestRun(t *testing.T) {
@@ -60,3 +63,48 @@ func TestRunWithEnv_VariableNotSetFailsValidation(t *testing.T) {
6063
t.Fatalf("RunWithEnv() = %v, want non-nil", err)
6164
}
6265
}
66+
67+
func TestGetExecutablePath(t *testing.T) {
68+
tests := []struct {
69+
name string
70+
releaseConfig *config.Release
71+
executableName string
72+
want string
73+
}{
74+
{
75+
name: "Preinstalled tool found",
76+
releaseConfig: &config.Release{
77+
Preinstalled: map[string]string{
78+
"cargo": "/usr/bin/cargo",
79+
"git": "/usr/bin/git",
80+
},
81+
},
82+
executableName: "cargo",
83+
want: "/usr/bin/cargo",
84+
},
85+
{
86+
name: "Preinstalled tool not found",
87+
releaseConfig: &config.Release{
88+
Preinstalled: map[string]string{
89+
"git": "/usr/bin/git",
90+
},
91+
},
92+
executableName: "cargo",
93+
want: "cargo",
94+
},
95+
{
96+
name: "No preinstalled section",
97+
releaseConfig: &config.Release{},
98+
executableName: "cargo",
99+
want: "cargo",
100+
},
101+
}
102+
for _, test := range tests {
103+
t.Run(test.name, func(t *testing.T) {
104+
got := GetExecutablePath(test.releaseConfig.Preinstalled, test.executableName)
105+
if diff := cmp.Diff(test.want, got); diff != "" {
106+
t.Errorf("mismatch (-want +got):\n%s", diff)
107+
}
108+
})
109+
}
110+
}

internal/config/config.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@ type Release struct {
6464
Tools map[string][]Tool `yaml:"tools,omitempty"`
6565
}
6666

67-
// GetExecutablePath finds the path for a given command, checking for an
68-
// override in the configuration first.
69-
func (r *Release) GetExecutablePath(commandName string) string {
70-
if r != nil && r.Preinstalled != nil {
71-
if exe, ok := r.Preinstalled[commandName]; ok {
72-
return exe
73-
}
74-
}
75-
return commandName
76-
}
77-
7867
// Tool defines the configuration required to install helper tools.
7968
type Tool struct {
8069
// Name is the name of the tool e.g. nox.

internal/config/config_test.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -137,54 +137,3 @@ func TestWrite(t *testing.T) {
137137
t.Errorf("mismatch (-want +got):\n%s", diff)
138138
}
139139
}
140-
141-
func TestReleaseGetExecutablePath(t *testing.T) {
142-
tests := []struct {
143-
name string
144-
releaseConfig *Release
145-
executableName string
146-
want string
147-
}{
148-
{
149-
name: "Preinstalled tool found",
150-
releaseConfig: &Release{
151-
Preinstalled: map[string]string{
152-
"cargo": "/usr/bin/cargo",
153-
"git": "/usr/bin/git",
154-
},
155-
},
156-
executableName: "cargo",
157-
want: "/usr/bin/cargo",
158-
},
159-
{
160-
name: "Preinstalled tool not found",
161-
releaseConfig: &Release{
162-
Preinstalled: map[string]string{
163-
"git": "/usr/bin/git",
164-
},
165-
},
166-
executableName: "cargo",
167-
want: "cargo",
168-
},
169-
{
170-
name: "No preinstalled section",
171-
releaseConfig: &Release{},
172-
executableName: "cargo",
173-
want: "cargo",
174-
},
175-
{
176-
name: "Nil release config",
177-
releaseConfig: nil,
178-
executableName: "cargo",
179-
want: "cargo",
180-
},
181-
}
182-
for _, test := range tests {
183-
t.Run(test.name, func(t *testing.T) {
184-
got := test.releaseConfig.GetExecutablePath(test.executableName)
185-
if diff := cmp.Diff(test.want, got); diff != "" {
186-
t.Errorf("GetExecutablePath() mismatch (-want +got):\n%s", diff)
187-
}
188-
})
189-
}
190-
}

internal/git/git_test.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestGetLastTag(t *testing.T) {
3939
Remote: "origin",
4040
Branch: "main",
4141
}
42-
got, err := GetLastTag(t.Context(), cfg.GetExecutablePath("git"), cfg.Remote, cfg.Branch)
42+
got, err := GetLastTag(t.Context(), command.GetExecutablePath(cfg.Preinstalled, "git"), cfg.Remote, cfg.Branch)
4343
if err != nil {
4444
t.Fatal(err)
4545
}
@@ -54,7 +54,7 @@ func TestLastTagGitError(t *testing.T) {
5454
Remote: "origin",
5555
Branch: "main",
5656
}
57-
_, err := GetLastTag(t.Context(), cfg.GetExecutablePath("git"), cfg.Remote, cfg.Branch)
57+
_, err := GetLastTag(t.Context(), command.GetExecutablePath(cfg.Preinstalled, "git"), cfg.Remote, cfg.Branch)
5858
if err == nil {
5959
t.Fatal("expected an error but got none")
6060
}
@@ -77,7 +77,7 @@ func TestIsNewFileSuccess(t *testing.T) {
7777
t.Fatal(err)
7878
}
7979
cfg := &config.Release{}
80-
gitExe := cfg.GetExecutablePath("git")
80+
gitExe := command.GetExecutablePath(cfg.Preinstalled, "git")
8181

8282
newName := path.Join("src", "storage", "src", "new.rs")
8383
if err := os.MkdirAll(path.Dir(newName), 0755); err != nil {
@@ -105,7 +105,7 @@ func TestIsNewFileDiffError(t *testing.T) {
105105
t.Chdir(t.TempDir())
106106
testhelper.SetupForVersionBump(t, wantTag)
107107
cfg := &config.Release{}
108-
gitExe := cfg.GetExecutablePath("git")
108+
gitExe := command.GetExecutablePath(cfg.Preinstalled, "git")
109109
existingName := path.Join("src", "storage", "src", "lib.rs")
110110
if IsNewFile(t.Context(), gitExe, "invalid-tag", existingName) {
111111
t.Errorf("diff errors should return false for isNewFile(): %s", existingName)
@@ -114,14 +114,10 @@ func TestIsNewFileDiffError(t *testing.T) {
114114

115115
func TestFilesChangedSuccess(t *testing.T) {
116116
const wantTag = "release-2001-02-03"
117-
release := &config.Release{
118-
Remote: "origin",
119-
Branch: "main",
120-
}
121117
remoteDir := testhelper.SetupRepoWithChange(t, wantTag)
122118
testhelper.CloneRepository(t, remoteDir)
123119

124-
got, err := FilesChangedSince(t.Context(), wantTag, release.GetExecutablePath("git"), release.IgnoredChanges)
120+
got, err := FilesChangedSince(t.Context(), wantTag, command.GetExecutablePath(nil, "git"), nil)
125121
if err != nil {
126122
t.Fatal(err)
127123
}
@@ -133,13 +129,9 @@ func TestFilesChangedSuccess(t *testing.T) {
133129

134130
func TestFilesBadRef(t *testing.T) {
135131
const wantTag = "release-2002-03-04"
136-
release := &config.Release{
137-
Remote: "origin",
138-
Branch: "main",
139-
}
140132
remoteDir := testhelper.SetupRepoWithChange(t, wantTag)
141133
testhelper.CloneRepository(t, remoteDir)
142-
if got, err := FilesChangedSince(t.Context(), "--invalid--", release.GetExecutablePath("git"), release.IgnoredChanges); err == nil {
134+
if got, err := FilesChangedSince(t.Context(), "--invalid--", command.GetExecutablePath(nil, "git"), nil); err == nil {
143135
t.Errorf("expected an error with invalid tag, got=%v", got)
144136
}
145137
}
@@ -245,7 +237,7 @@ func TestAssertGitStatusClean(t *testing.T) {
245237
tmpDir := t.TempDir()
246238
t.Chdir(tmpDir)
247239
test.setup(t)
248-
err := AssertGitStatusClean(t.Context(), cfg.GetExecutablePath("git"))
240+
err := AssertGitStatusClean(t.Context(), command.GetExecutablePath(cfg.Preinstalled, "git"))
249241
if (err != nil) != test.wantErr {
250242
t.Errorf("AssertGitStatusClean() error = %v, wantErr %v", err, test.wantErr)
251243
}

internal/librarian/publish.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020

21+
"github.com/googleapis/librarian/internal/command"
2122
"github.com/googleapis/librarian/internal/config"
2223
"github.com/googleapis/librarian/internal/git"
2324
"github.com/googleapis/librarian/internal/librarian/rust"
@@ -62,7 +63,10 @@ func publish(ctx context.Context, cfg *config.Config, dryRun bool, skipSemverChe
6263
if err := verifyRequiredTools(ctx, cfg.Language, cfg.Release); err != nil {
6364
return err
6465
}
65-
gitExe := cfg.Release.GetExecutablePath("git")
66+
gitExe := "git"
67+
if cfg.Release != nil {
68+
gitExe = command.GetExecutablePath(cfg.Release.Preinstalled, "git")
69+
}
6670
if err := git.AssertGitStatusClean(ctx, gitExe); err != nil {
6771
return err
6872
}
@@ -86,7 +90,10 @@ func publish(ctx context.Context, cfg *config.Config, dryRun bool, skipSemverChe
8690

8791
// verifyRequiredTools verifies all the necessary language-agnostic tools are installed.
8892
func verifyRequiredTools(ctx context.Context, language string, cfg *config.Release) error {
89-
gitExe := cfg.GetExecutablePath("git")
93+
gitExe := "git"
94+
if cfg != nil {
95+
gitExe = command.GetExecutablePath(cfg.Preinstalled, "git")
96+
}
9097
if err := git.GitVersion(ctx, gitExe); err != nil {
9198
return err
9299
}

internal/librarian/release.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"errors"
2020
"fmt"
2121

22+
"github.com/googleapis/librarian/internal/command"
2223
"github.com/googleapis/librarian/internal/config"
2324
"github.com/googleapis/librarian/internal/git"
2425
"github.com/googleapis/librarian/internal/librarian/rust"
@@ -78,7 +79,10 @@ func runRelease(ctx context.Context, cmd *cli.Command) error {
7879
if err != nil {
7980
return err
8081
}
81-
gitExe := cfg.Release.GetExecutablePath("git")
82+
gitExe := "git"
83+
if cfg.Release != nil {
84+
gitExe = command.GetExecutablePath(cfg.Release.Preinstalled, "git")
85+
}
8286
if err := git.AssertGitStatusClean(ctx, gitExe); err != nil {
8387
return err
8488
}
@@ -174,7 +178,7 @@ func shouldReleaseLibrary(ctx context.Context, cfg *config.Config, path string)
174178
if cfg.Release == nil {
175179
return false, errReleaseConfigEmpty
176180
}
177-
gitExe := cfg.Release.GetExecutablePath("git")
181+
gitExe := command.GetExecutablePath(cfg.Release.Preinstalled, "git")
178182
lastTag, err := git.GetLastTag(ctx, gitExe, cfg.Release.Remote, cfg.Release.Branch)
179183
if err != nil {
180184
return false, err

0 commit comments

Comments
 (0)