Skip to content

Commit a820a71

Browse files
authored
feat(internal/command): add RequireCommand test helper (#2978)
A RequireCommand test helper function is added to skip tests when required external commands are not installed. This function is exported from the internal/command package and can be used across test files. The helper replaces the previous requireCommand, requireProtoc, and requireCargo functions that were defined in internal/sidekick/sidekick.
1 parent 6ef6eed commit a820a71

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

internal/command/testhelpers.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package command
16+
17+
import (
18+
"os/exec"
19+
"testing"
20+
)
21+
22+
// RequireCommand skips the test if the specified command is not found in PATH.
23+
// Use this to skip tests that depend on external tools like protoc, cargo, or
24+
// taplo, so that `go test ./...` will always pass on a fresh clone of the
25+
// repo.
26+
func RequireCommand(t *testing.T, command string) {
27+
t.Helper()
28+
if _, err := exec.LookPath(command); err != nil {
29+
t.Skipf("skipping test because %s is not installed", command)
30+
}
31+
}

internal/sidekick/sidekick/rust_bump_versions_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ package sidekick
1717
import (
1818
"testing"
1919

20+
cmdtest "github.com/googleapis/librarian/internal/command"
2021
"github.com/googleapis/librarian/internal/sidekick/config"
2122
)
2223

2324
func TestRustBumpVersions(t *testing.T) {
25+
cmdtest.RequireCommand(t, "taplo")
2426
config := &config.Config{
2527
Release: &config.Release{
2628
Preinstalled: map[string]string{

internal/sidekick/sidekick/sidekick_rust_prost_convert_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ package sidekick
1717
import (
1818
"path"
1919
"testing"
20+
21+
cmdtest "github.com/googleapis/librarian/internal/command"
2022
)
2123

2224
func TestRustProstConvert(t *testing.T) {
23-
requireProtoc(t)
25+
cmdtest.RequireCommand(t, "protoc")
2426
outDir := t.TempDir()
2527

2628
type TestConfig struct {

internal/sidekick/sidekick/sidekick_rust_prost_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ import (
1818
"os"
1919
"path"
2020
"testing"
21+
22+
cmdtest "github.com/googleapis/librarian/internal/command"
2123
)
2224

2325
func TestRustProstFromProtobuf(t *testing.T) {
24-
requireCargo(t)
25-
requireProtoc(t)
26+
cmdtest.RequireCommand(t, "cargo")
27+
cmdtest.RequireCommand(t, "protoc")
2628
outDir := t.TempDir()
2729
svcConfig := path.Join(testdataDir, "googleapis/google/type/type.yaml")
2830
specificationSource := path.Join(testdataDir, "googleapis/google/type")

internal/sidekick/sidekick/sidekick_sample_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import (
1818
"os"
1919
"path"
2020
"testing"
21+
22+
cmdtest "github.com/googleapis/librarian/internal/command"
2123
)
2224

2325
func TestSampleFromProtobuf(t *testing.T) {
24-
requireProtoc(t)
26+
cmdtest.RequireCommand(t, "protoc")
2527
outDir := t.TempDir()
2628
svcConfig := path.Join(testdataDir, "googleapis/google/type/type.yaml")
2729
specificationSource := path.Join(testdataDir, "googleapis/google/type")

internal/sidekick/sidekick/sidekick_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ package sidekick
1616

1717
import (
1818
"fmt"
19-
"os/exec"
2019
"path/filepath"
21-
"testing"
2220
)
2321

2422
const (
@@ -31,18 +29,3 @@ var (
3129
outputDir = fmt.Sprintf("%s/test-only", testdataDir)
3230
specificationSource = fmt.Sprintf("%s/openapi/secretmanager_openapi_v1.json", testdataDir)
3331
)
34-
35-
func requireCommand(t *testing.T, command string) {
36-
t.Helper()
37-
if _, err := exec.LookPath(command); err != nil {
38-
t.Skipf("skipping test because %s is not installed", command)
39-
}
40-
}
41-
42-
func requireProtoc(t *testing.T) {
43-
requireCommand(t, "protoc")
44-
}
45-
46-
func requireCargo(t *testing.T) {
47-
requireCommand(t, "cargo")
48-
}

0 commit comments

Comments
 (0)