From 4a7eaded01970426f654baa8311db9e044d78dcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Fri, 3 Oct 2025 15:33:54 +0200 Subject: [PATCH 1/6] Add resolve_dep_version function This commit introduces a new shell helper function, `resolve_dep_version`, which retrieves the version of a specified Go module from the project's `go.mod` file. As part of this change: - All usages of `knative.dev/toolbox/...@latest` have been pinned to a specific commit (`@bc7e152`) to ensure reproducible builds and prevent unexpected failures due to upstream changes. - The shell script test harness has been improved to more reliably locate the `go` executable, making the tests more robust across different environments. --- codegen-library.sh | 2 +- library.sh | 24 ++++++++++-- presubmit-tests.sh | 2 +- test/unit/library_test.go | 3 ++ test/unit/sharedlib_test.go | 69 +++++++++++++++++++++++++---------- test/unit/update_deps_test.go | 4 +- 6 files changed, 78 insertions(+), 26 deletions(-) diff --git a/codegen-library.sh b/codegen-library.sh index 46528a03..6b1fa4d0 100755 --- a/codegen-library.sh +++ b/codegen-library.sh @@ -36,7 +36,7 @@ else exit 42 fi -repodir="$(go_run knative.dev/toolbox/modscope@latest current --path)" +repodir="$(go_run knative.dev/toolbox/modscope@bc7e152 current --path)" function go-resolve-pkg-dir() { local pkg="${1:?Pass the package name}" diff --git a/library.sh b/library.sh index d487a149..330fb944 100755 --- a/library.sh +++ b/library.sh @@ -677,6 +677,24 @@ function start_knative_eventing_extension() { wait_until_pods_running "$2" || return 1 } +# Resolve the version of a dependency from the go.mod file at the project root. +# Parameters: $1 - name of the dependency (required) +# $2 - fallback version if not found (optional) +function resolve_dep_version() { + local dep="$1" + local fallback="${2:-}" + local version + version=$(go list -m -f '{{if eq .Path "'"$dep"'"}}{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}{{end}}' "$dep" 2>/dev/null) + if [[ -z "$version" ]]; then + if [[ -z "$fallback" ]]; then + abort "Dependency $dep not found in go.mod and no fallback version provided." + fi + echo "$fallback" + else + echo -n "$version" + fi +} + # Run a go utility without installing it. # Parameters: $1 - tool package for go run. # $2..$n - parameters passed to the tool. @@ -748,7 +766,7 @@ function foreach_go_module() { echo "Command '${cmd}' failed in module $gomod_dir: $failed" >&2 return $failed fi - done < <(go_run knative.dev/toolbox/modscope@latest ls -p) + done < <(go_run knative.dev/toolbox/modscope@bc7e152 ls -p) } # Update go deps. @@ -823,7 +841,7 @@ function __go_update_deps_for_module() { else group "Upgrading to release ${RELEASE}" fi - FLOATING_DEPS+=( $(go_run knative.dev/toolbox/buoy@latest float ./go.mod "${buoyArgs[@]}") ) + FLOATING_DEPS+=( $(go_run knative.dev/toolbox/buoy@bc7e152 float ./go.mod "${buoyArgs[@]}") ) if [[ ${#FLOATING_DEPS[@]} > 0 ]]; then echo "Floating deps to ${FLOATING_DEPS[@]}" go get -d ${FLOATING_DEPS[@]} @@ -878,7 +896,7 @@ function __go_update_deps_for_module() { # Intended to be used like: # export MODULE_NAME=$(go_mod_module_name) function go_mod_module_name() { - go_run knative.dev/toolbox/modscope@latest current + go_run knative.dev/toolbox/modscope@bc7e152 current } function __is_checkout_onto_gopath() { diff --git a/presubmit-tests.sh b/presubmit-tests.sh index b96ffadf..a22ddfd6 100755 --- a/presubmit-tests.sh +++ b/presubmit-tests.sh @@ -141,7 +141,7 @@ function __build_test_runner_for_module() { # Don't merge these two lines, or return code will always be 0. # Get all build tags in go code (ignore /vendor, /hack and /third_party) local tags - tags="$(go run knative.dev/toolbox/go-ls-tags@latest --joiner=,)" + tags="$(go run knative.dev/toolbox/go-ls-tags@bc7e152 --joiner=,)" local go_pkg_dirs go_pkg_dirs="$(go list -tags "${tags}" ./...)" || return $? if [[ -z "${go_pkg_dirs}" ]]; then diff --git a/test/unit/library_test.go b/test/unit/library_test.go index 2c956247..e32a113b 100644 --- a/test/unit/library_test.go +++ b/test/unit/library_test.go @@ -102,6 +102,9 @@ func TestHelperFunctions(t *testing.T) { }, { name: `hashCode ""`, stdout: equal("0"), + }, { + name: `resolve_dep_version k8s.io/code-generator`, + stdout: equal("v0.29.2"), }} for _, tc := range tcs { tc := tc diff --git a/test/unit/sharedlib_test.go b/test/unit/sharedlib_test.go index 4ec4b25a..5d147ee5 100644 --- a/test/unit/sharedlib_test.go +++ b/test/unit/sharedlib_test.go @@ -6,12 +6,15 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "os/exec" "path" + "path/filepath" "regexp" "runtime" "strings" + "syscall" "testing" "time" @@ -308,7 +311,7 @@ func (s simply) Invocations(_ string) []string { type callOriginal struct{} func (o callOriginal) Invocations(bin string) []string { - binPath, err := exec.LookPath(bin) + binPath, err := findExecutable(bin) if err != nil { panic(err) } @@ -317,6 +320,36 @@ func (o callOriginal) Invocations(bin string) []string { } } +func findExecutable(bin string) (string, error) { + goroot := runtime.GOROOT() + binPath := filepath.Join(goroot, "bin", bin) + if runtime.GOOS == "windows" { + binPath = binPath + ".exe" + } + if err := checkExecutable(binPath); err != nil { + binPath, err = exec.LookPath(bin) + if err != nil { + return "", err + } + } + return binPath, nil +} + +func checkExecutable(file string) error { + d, err := os.Stat(file) + if err != nil { + return err + } + m := d.Mode() + if m.IsDir() { + return syscall.EISDIR + } + if m&0111 != 0 { + return nil + } + return fs.ErrPermission +} + func mockBinary(name string, responses ...response) scriptlet { return fnScriptlet(func(t TestingT) string { code := make([]string, 0, len(responses)*10) @@ -369,8 +402,8 @@ func (a anyArgs) String() string { } func mockGo(responses ...response) scriptlet { - lstags := "knative.dev/toolbox/go-ls-tags@latest" - modscope := "knative.dev/toolbox/modscope@latest" + lstags := "knative.dev/toolbox/go-ls-tags@bc7e152" + modscope := "knative.dev/toolbox/modscope@bc7e152" gum := "github.com/charmbracelet/gum@v0.14.1" callOriginals := []args{ startsWith{"run " + lstags}, @@ -464,12 +497,13 @@ func (s shellScript) run(t TestingT, commands []string) (int, string, string, st } func (s shellScript) source(t TestingT, commands []string) string { + goroot := runtime.GOROOT() source := fmt.Sprintf(` set -Eeuo pipefail export TMPPATH='%s' -export PATH="${TMPPATH}:${PATH}" +export PATH="${TMPPATH}:%s:${PATH}" export KNATIVE_HACK_SCRIPT_MANUAL_VERBOSE=true -`, t.TempDir()) +`, t.TempDir(), fmt.Sprintf("%s/bin", goroot)) bashShebang := "#!/usr/bin/env bash\n" for _, sclet := range s.scriptlets { source += "\n" + strings.TrimPrefix(sclet.scriptlet(t), bashShebang) + "\n" @@ -512,24 +546,21 @@ func (f fnPrefetcher) prefetch(t TestingT) { // and compilation messages, which go prints will not influence the test. func goRunHelpPrefetcher(tool string) prefetcher { return fnPrefetcher(func(t TestingT) { - c := exec.Command("go", "run", tool, "--help") - var ( - stdout, stderr io.ReadCloser - err error - ) - stdout, err = c.StdoutPipe() - require.NoError(t, err) - stderr, err = c.StderrPipe() + stdout := bytes.NewBuffer(make([]byte, 0, 1024)) + stderr := bytes.NewBuffer(make([]byte, 0, 1024)) + gobin, err := findExecutable("go") require.NoError(t, err) + c := exec.Command(gobin, "run", tool, "--help") + c.Stdout = stdout + c.Stderr = stderr err = c.Run() if err != nil { - stdBytes, merr := io.ReadAll(stdout) - require.NoError(t, merr) - errBytes, rerr := io.ReadAll(stderr) - require.NoError(t, rerr) + errBytes, _ := io.ReadAll(stderr) + stdBytes, _ := io.ReadAll(stdout) require.NoError(t, err, - "------\nSTDOUT\n------", string(stdBytes), - "------\nSTDERR\n------", string(errBytes)) + "───── BEGIN STDOUT ─────\n%s\n────── END STDOUT ──────\n"+ + "───── BEGIN STDERR ─────\n%s\n────── END STDERR ──────", + string(stdBytes), string(errBytes)) } }) } diff --git a/test/unit/update_deps_test.go b/test/unit/update_deps_test.go index 85159236..b8107085 100644 --- a/test/unit/update_deps_test.go +++ b/test/unit/update_deps_test.go @@ -35,13 +35,13 @@ func TestUpdateDeps(t *testing.T) { }, { name: "go_update_deps --upgrade", stdout: []check{ - contains("👻 go run knative.dev/toolbox/buoy@latest float " + + contains("👻 go run knative.dev/toolbox/buoy@bc7e152 float " + "./go.mod --release v9000.1 --domain knative.dev"), }, }, { name: "go_update_deps --upgrade --release 1.25 --module-release 0.28", stdout: []check{ - contains("👻 go run knative.dev/toolbox/buoy@latest float " + + contains("👻 go run knative.dev/toolbox/buoy@bc7e152 float " + "./go.mod --release 1.25 --domain knative.dev " + "--module-release 0.28"), }, From e9d3c225a49b3a17dd8dc9b892e09617c34a8bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Mon, 6 Oct 2025 18:19:50 +0200 Subject: [PATCH 2/6] Fix: Use @latest for toolbox deps and set GOTOOLCHAIN The `go_run` helper function is updated to set `GOTOOLCHAIN=auto` when executing toolbox tools. --- codegen-library.sh | 2 +- library.sh | 31 +++++++++---------------------- presubmit-tests.sh | 2 +- test/unit/library_test.go | 3 --- test/unit/sharedlib_test.go | 14 ++++++++++---- test/unit/update_deps_test.go | 4 ++-- 6 files changed, 23 insertions(+), 33 deletions(-) diff --git a/codegen-library.sh b/codegen-library.sh index 6b1fa4d0..46528a03 100755 --- a/codegen-library.sh +++ b/codegen-library.sh @@ -36,7 +36,7 @@ else exit 42 fi -repodir="$(go_run knative.dev/toolbox/modscope@bc7e152 current --path)" +repodir="$(go_run knative.dev/toolbox/modscope@latest current --path)" function go-resolve-pkg-dir() { local pkg="${1:?Pass the package name}" diff --git a/library.sh b/library.sh index 330fb944..4564c0f2 100755 --- a/library.sh +++ b/library.sh @@ -677,29 +677,11 @@ function start_knative_eventing_extension() { wait_until_pods_running "$2" || return 1 } -# Resolve the version of a dependency from the go.mod file at the project root. -# Parameters: $1 - name of the dependency (required) -# $2 - fallback version if not found (optional) -function resolve_dep_version() { - local dep="$1" - local fallback="${2:-}" - local version - version=$(go list -m -f '{{if eq .Path "'"$dep"'"}}{{if .Replace}}{{.Replace.Version}}{{else}}{{.Version}}{{end}}{{end}}' "$dep" 2>/dev/null) - if [[ -z "$version" ]]; then - if [[ -z "$fallback" ]]; then - abort "Dependency $dep not found in go.mod and no fallback version provided." - fi - echo "$fallback" - else - echo -n "$version" - fi -} - # Run a go utility without installing it. # Parameters: $1 - tool package for go run. # $2..$n - parameters passed to the tool. function go_run() { - local package + local package gotoolchain package="$1" if [[ "$package" != *@* ]]; then abort 'Package for "go_run" needs to have @version' @@ -714,6 +696,11 @@ function go_run() { GORUN_PATH="$(mktemp -t -d -u gopath.XXXXXXXX)" fi export GORUN_PATH + gotoolchain="$(go env GOTOOLCHAIN)" + if [[ "$package" != knative.dev/toolbox/cmd/testgrid@* ]]; then + gotoolchain=auto + fi + GOTOOLCHAIN="${gotoolchain}" \ GOPATH="${GORUN_PATH}" \ GOFLAGS='' \ go run "$package" "$@" @@ -766,7 +753,7 @@ function foreach_go_module() { echo "Command '${cmd}' failed in module $gomod_dir: $failed" >&2 return $failed fi - done < <(go_run knative.dev/toolbox/modscope@bc7e152 ls -p) + done < <(go_run knative.dev/toolbox/modscope@latest ls -p) } # Update go deps. @@ -841,7 +828,7 @@ function __go_update_deps_for_module() { else group "Upgrading to release ${RELEASE}" fi - FLOATING_DEPS+=( $(go_run knative.dev/toolbox/buoy@bc7e152 float ./go.mod "${buoyArgs[@]}") ) + FLOATING_DEPS+=( $(go_run knative.dev/toolbox/buoy@latest float ./go.mod "${buoyArgs[@]}") ) if [[ ${#FLOATING_DEPS[@]} > 0 ]]; then echo "Floating deps to ${FLOATING_DEPS[@]}" go get -d ${FLOATING_DEPS[@]} @@ -896,7 +883,7 @@ function __go_update_deps_for_module() { # Intended to be used like: # export MODULE_NAME=$(go_mod_module_name) function go_mod_module_name() { - go_run knative.dev/toolbox/modscope@bc7e152 current + go_run knative.dev/toolbox/modscope@latest current } function __is_checkout_onto_gopath() { diff --git a/presubmit-tests.sh b/presubmit-tests.sh index a22ddfd6..f11585d4 100755 --- a/presubmit-tests.sh +++ b/presubmit-tests.sh @@ -141,7 +141,7 @@ function __build_test_runner_for_module() { # Don't merge these two lines, or return code will always be 0. # Get all build tags in go code (ignore /vendor, /hack and /third_party) local tags - tags="$(go run knative.dev/toolbox/go-ls-tags@bc7e152 --joiner=,)" + tags="$(go_run knative.dev/toolbox/go-ls-tags@latest --joiner=,)" local go_pkg_dirs go_pkg_dirs="$(go list -tags "${tags}" ./...)" || return $? if [[ -z "${go_pkg_dirs}" ]]; then diff --git a/test/unit/library_test.go b/test/unit/library_test.go index e32a113b..2c956247 100644 --- a/test/unit/library_test.go +++ b/test/unit/library_test.go @@ -102,9 +102,6 @@ func TestHelperFunctions(t *testing.T) { }, { name: `hashCode ""`, stdout: equal("0"), - }, { - name: `resolve_dep_version k8s.io/code-generator`, - stdout: equal("v0.29.2"), }} for _, tc := range tcs { tc := tc diff --git a/test/unit/sharedlib_test.go b/test/unit/sharedlib_test.go index 5d147ee5..7b2e7ca7 100644 --- a/test/unit/sharedlib_test.go +++ b/test/unit/sharedlib_test.go @@ -187,7 +187,7 @@ func retcode(code int) *returnCode { func (tc testCase) test(sc shellScript) func(t *testing.T) { return func(t *testing.T) { t.Parallel() - code, out, err, src := sc.run(t, tc.testCommands()) + code, out, errOut, src := sc.run(t, tc.testCommands()) tc.validRetcode(t, code) checkStream := func(output string, otype outputType, checks []check) { success := true @@ -200,7 +200,12 @@ func (tc testCase) test(sc shellScript) func(t *testing.T) { } } checkStream(out, outputTypeStdout, coalesce(tc.stdout, empty())) - checkStream(err, outputTypeStderr, coalesce(tc.stderr, empty())) + // skip go switching messages from asserting + errOut = regexp.MustCompile( + "go: knative\\.dev/toolbox@v0\\.0\\.0-\\d+-[0-9a-f]+ requires "+ + "go >= \\d\\.\\d+\\.\\d+; switching to go\\d\\.\\d+\\.\\d+\n", + ).ReplaceAllString(errOut, "") + checkStream(errOut, outputTypeStdout, coalesce(tc.stderr, empty())) if t.Failed() { failedScriptPath := path.Join(os.TempDir(), @@ -402,8 +407,8 @@ func (a anyArgs) String() string { } func mockGo(responses ...response) scriptlet { - lstags := "knative.dev/toolbox/go-ls-tags@bc7e152" - modscope := "knative.dev/toolbox/modscope@bc7e152" + lstags := "knative.dev/toolbox/go-ls-tags@latest" + modscope := "knative.dev/toolbox/modscope@latest" gum := "github.com/charmbracelet/gum@v0.14.1" callOriginals := []args{ startsWith{"run " + lstags}, @@ -551,6 +556,7 @@ func goRunHelpPrefetcher(tool string) prefetcher { gobin, err := findExecutable("go") require.NoError(t, err) c := exec.Command(gobin, "run", tool, "--help") + c.Env = append(os.Environ(), "GOTOOLCHAIN=auto") c.Stdout = stdout c.Stderr = stderr err = c.Run() diff --git a/test/unit/update_deps_test.go b/test/unit/update_deps_test.go index b8107085..85159236 100644 --- a/test/unit/update_deps_test.go +++ b/test/unit/update_deps_test.go @@ -35,13 +35,13 @@ func TestUpdateDeps(t *testing.T) { }, { name: "go_update_deps --upgrade", stdout: []check{ - contains("👻 go run knative.dev/toolbox/buoy@bc7e152 float " + + contains("👻 go run knative.dev/toolbox/buoy@latest float " + "./go.mod --release v9000.1 --domain knative.dev"), }, }, { name: "go_update_deps --upgrade --release 1.25 --module-release 0.28", stdout: []check{ - contains("👻 go run knative.dev/toolbox/buoy@bc7e152 float " + + contains("👻 go run knative.dev/toolbox/buoy@latest float " + "./go.mod --release 1.25 --domain knative.dev " + "--module-release 0.28"), }, From 28a9e43668d2a9222a051fe5f6bf6c0fbcf7565d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Mon, 6 Oct 2025 20:55:16 +0200 Subject: [PATCH 3/6] Test cases loop for Intellij discoverability --- test/unit/codegen_test.go | 4 ++-- test/unit/presubmit_test.go | 4 ++-- test/unit/release_test.go | 4 ++-- test/unit/report_go_test.go | 4 ++-- test/unit/update_deps_test.go | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/unit/codegen_test.go b/test/unit/codegen_test.go index c78aebb7..ea42d6b4 100644 --- a/test/unit/codegen_test.go +++ b/test/unit/codegen_test.go @@ -41,8 +41,8 @@ Deepcopy generation complete ), stderr: warned("Failed to determine the knative.dev/pkg package"), }} - for i := range tcs { - tc := tcs[i] + for _, tc := range tcs { + tc := tc t.Run(tc.name, tc.test(sc)) } } diff --git a/test/unit/presubmit_test.go b/test/unit/presubmit_test.go index bf002f5b..845217b6 100644 --- a/test/unit/presubmit_test.go +++ b/test/unit/presubmit_test.go @@ -43,8 +43,8 @@ func TestPresubmitTestMainFunc(t *testing.T) { header("UNIT TESTS PASSED"), }, }} - for i := range tcs { - tc := tcs[i] + for _, tc := range tcs { + tc := tc t.Run(tc.name, tc.test(sc)) } } diff --git a/test/unit/release_test.go b/test/unit/release_test.go index 07c152ad..82889d65 100644 --- a/test/unit/release_test.go +++ b/test/unit/release_test.go @@ -44,8 +44,8 @@ func TestBuildFromSource(t *testing.T) { }, stdout: append(outChecks, contains(checksumsContent)), }} - for i := range tcs { - tc := tcs[i] + for _, tc := range tcs { + tc := tc t.Run(tc.name, func(t *testing.T) { tmp := t.TempDir() sc := testReleaseShellScript( diff --git a/test/unit/report_go_test.go b/test/unit/report_go_test.go index e042c630..65f3b804 100644 --- a/test/unit/report_go_test.go +++ b/test/unit/report_go_test.go @@ -47,8 +47,8 @@ func TestReportGoTest(t *testing.T) { }, logChecks...), stderr: []check{contains("exit status 1")}, }} - for i := range tcs { - tc := tcs[i] + for _, tc := range tcs { + tc := tc t.Run(tc.name, tc.test(sc)) } } diff --git a/test/unit/update_deps_test.go b/test/unit/update_deps_test.go index 85159236..6748c01b 100644 --- a/test/unit/update_deps_test.go +++ b/test/unit/update_deps_test.go @@ -46,8 +46,8 @@ func TestUpdateDeps(t *testing.T) { "--module-release 0.28"), }, }} - for i := range tcs { - tc := tcs[i] + for _, tc := range tcs { + tc := tc t.Run(tc.name, tc.test(sc)) } } From 305b23ee62c1581eb99171098cd7a5b1288ffa3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Mon, 6 Oct 2025 21:00:28 +0200 Subject: [PATCH 4/6] Fix: Correctly set GOTOOLCHAIN for toolbox packages This change corrects the logic to set `GOTOOLCHAIN=auto` for any package within the `knative.dev/toolbox` module. --- library.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.sh b/library.sh index 4564c0f2..62139675 100755 --- a/library.sh +++ b/library.sh @@ -697,7 +697,7 @@ function go_run() { fi export GORUN_PATH gotoolchain="$(go env GOTOOLCHAIN)" - if [[ "$package" != knative.dev/toolbox/cmd/testgrid@* ]]; then + if [[ "$package" == knative.dev/toolbox/* ]]; then gotoolchain=auto fi GOTOOLCHAIN="${gotoolchain}" \ From ce37357257af9e4465b653c2225120a07ed049d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Mon, 6 Oct 2025 22:32:27 +0200 Subject: [PATCH 5/6] Fix(test): Correct stream type and handle read errors This commit makes two corrections to the test suite in `sharedlib_test.go`: 1. In the main test runner, the `errOut` variable (which captures stderr) was incorrectly being checked against the `outputTypeStdout` constant. This has been corrected to `outputTypeStderr` to ensure test failure messages accurately reflect the stream being checked. 2. Potential errors from `io.ReadAll` when capturing stdout and stderr in the `goRunHelpPrefetcher` were being ignored. Assertions have been added to ensure that any errors during these reads are caught, making the test more robust. --- test/unit/sharedlib_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/unit/sharedlib_test.go b/test/unit/sharedlib_test.go index 7b2e7ca7..94f787c6 100644 --- a/test/unit/sharedlib_test.go +++ b/test/unit/sharedlib_test.go @@ -5,7 +5,6 @@ import ( "embed" "errors" "fmt" - "io" "io/fs" "os" "os/exec" @@ -205,7 +204,7 @@ func (tc testCase) test(sc shellScript) func(t *testing.T) { "go: knative\\.dev/toolbox@v0\\.0\\.0-\\d+-[0-9a-f]+ requires "+ "go >= \\d\\.\\d+\\.\\d+; switching to go\\d\\.\\d+\\.\\d+\n", ).ReplaceAllString(errOut, "") - checkStream(errOut, outputTypeStdout, coalesce(tc.stderr, empty())) + checkStream(errOut, outputTypeStderr, coalesce(tc.stderr, empty())) if t.Failed() { failedScriptPath := path.Join(os.TempDir(), @@ -561,8 +560,8 @@ func goRunHelpPrefetcher(tool string) prefetcher { c.Stderr = stderr err = c.Run() if err != nil { - errBytes, _ := io.ReadAll(stderr) - stdBytes, _ := io.ReadAll(stdout) + errBytes := stderr.Bytes() + stdBytes := stdout.Bytes() require.NoError(t, err, "───── BEGIN STDOUT ─────\n%s\n────── END STDOUT ──────\n"+ "───── BEGIN STDERR ─────\n%s\n────── END STDERR ──────", From 6f0b2e9c0bf790d5e56c6d10cfcbbc45d9dc1d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Tue, 7 Oct 2025 09:49:49 +0200 Subject: [PATCH 6/6] refactor(test): Pre-compile regex in sharedlib test --- test/unit/sharedlib_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/unit/sharedlib_test.go b/test/unit/sharedlib_test.go index 94f787c6..bd94559d 100644 --- a/test/unit/sharedlib_test.go +++ b/test/unit/sharedlib_test.go @@ -183,6 +183,11 @@ func retcode(code int) *returnCode { return &rc } +var errOutStripGoSwitchingRe = regexp.MustCompile( + "go: knative\\.dev/toolbox@v0\\.0\\.0-\\d+-[0-9a-f]+ requires " + + "go >= \\d\\.\\d+\\.\\d+; switching to go\\d\\.\\d+\\.\\d+\n", +) + func (tc testCase) test(sc shellScript) func(t *testing.T) { return func(t *testing.T) { t.Parallel() @@ -200,10 +205,7 @@ func (tc testCase) test(sc shellScript) func(t *testing.T) { } checkStream(out, outputTypeStdout, coalesce(tc.stdout, empty())) // skip go switching messages from asserting - errOut = regexp.MustCompile( - "go: knative\\.dev/toolbox@v0\\.0\\.0-\\d+-[0-9a-f]+ requires "+ - "go >= \\d\\.\\d+\\.\\d+; switching to go\\d\\.\\d+\\.\\d+\n", - ).ReplaceAllString(errOut, "") + errOut = errOutStripGoSwitchingRe.ReplaceAllString(errOut, "") checkStream(errOut, outputTypeStderr, coalesce(tc.stderr, empty())) if t.Failed() {