opentofu/internal/experiments/testing.go
Martin Atkins 26e5320a78 Move experiments/ to internal/experiments/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -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
}
}