diff --git a/internal/test/test.go b/internal/test/test.go index 202e015ab..e3c5b9c68 100644 --- a/internal/test/test.go +++ b/internal/test/test.go @@ -27,6 +27,15 @@ import ( // UpdateGolden writes out the golden files with the latest values, rather than failing the test. var updateGolden = flag.Bool("update", false, "update golden files") +// IsTestMode reports whether the binary was built with -tags helmtest. +// General-purpose signal that the binary was built for tests; consult it +// from production code paths that need to behave differently under test. +// Backed by a compile-time const (see test_mode_on.go / test_mode_off.go) +// so branches gated on it dead-code-eliminate in release builds. +func IsTestMode() bool { + return testMode +} + // TestingT describes a testing object compatible with the critical functions from the testing.T type type TestingT interface { Fatal(...any) diff --git a/internal/test/test_mode_off.go b/internal/test/test_mode_off.go new file mode 100644 index 000000000..62534ed12 --- /dev/null +++ b/internal/test/test_mode_off.go @@ -0,0 +1,21 @@ +//go:build !helmtest + +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package test + +const testMode = false diff --git a/internal/test/test_mode_on.go b/internal/test/test_mode_on.go new file mode 100644 index 000000000..8a2978699 --- /dev/null +++ b/internal/test/test_mode_on.go @@ -0,0 +1,21 @@ +//go:build helmtest + +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package test + +const testMode = true