opentofu/internal/experiments/testing.go
Martin Atkins 783a07d9e8 build: Use Go 1.19
Go 1.19's "fmt" has some awareness of the new doc comment formatting
conventions and adjusts the presentation of the source comments to make
it clearer how godoc would interpret them. Therefore this commit includes
various updates made by "go fmt" to acheve that.

In line with our usual convention that we make stylistic/grammar/spelling
tweaks typically only when we're "in the area" changing something else
anyway, I also took this opportunity to review most of the comments that
this updated to see if there were any other opportunities to improve them.
2022-08-22 10:59:12 -07:00

34 lines
1.2 KiB
Go

package experiments
import (
"testing"
)
// OverrideForTesting temporarily overrides the global tables
// of experiments in order to allow for a predictable set when unit testing
// the experiments infrastructure code.
//
// The correct way to use this function is to defer a call to its result so
// that the original tables can be restored at the conclusion of the calling
// test:
//
// defer experiments.OverrideForTesting(t, current, concluded)()
//
// This function modifies global variables that are normally fixed throughout
// our execution, so this function must not be called from non-test code and
// any test using it cannot safely run concurrently with other tests.
func OverrideForTesting(t *testing.T, current Set, concluded map[Experiment]string) func() {
// We're not currently using the given *testing.T in here, but we're
// requiring it anyway in case we might need it in future, and because
// it hopefully reinforces that only test code should be calling this.
realCurrents := currentExperiments
realConcludeds := concludedExperiments
currentExperiments = current
concludedExperiments = concluded
return func() {
currentExperiments = realCurrents
concludedExperiments = realConcludeds
}
}