2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-05-01 16:56:34 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RequireErrorContains is a test helper for asserting that an error occurred
|
|
|
|
// and the error message returned contains the expected error message as a
|
|
|
|
// substring.
|
2021-03-16 22:05:39 +00:00
|
|
|
func RequireErrorContains(t testing.TB, err error, expectedErrorMessage string) {
|
2020-05-01 16:56:34 +00:00
|
|
|
t.Helper()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("An error is expected but got nil.")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), expectedErrorMessage) {
|
2021-09-14 17:08:06 +00:00
|
|
|
t.Fatalf("expected err %v to contain %q", err, expectedErrorMessage)
|
2020-05-01 16:56:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-10 20:25:51 +00:00
|
|
|
|
|
|
|
// RunStep is a test helper to help you stop a series of subtests from
|
|
|
|
// executing after the first one that fails.
|
|
|
|
func RunStep(t *testing.T, name string, fn func(t *testing.T)) {
|
|
|
|
t.Helper()
|
|
|
|
if !t.Run(name, fn) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|