diff --git a/helper/resource/testing_new.go b/helper/resource/testing_new.go index beaa358b6..17a1dca17 100644 --- a/helper/resource/testing_new.go +++ b/helper/resource/testing_new.go @@ -122,6 +122,17 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest // acts as default for import tests var appliedCfg string + var file *os.File + defer func() error { + if file != nil { + err := file.Close() + if err != nil { + return err + } + } + return nil + }() + for stepIndex, step := range c.Steps { stepNumber := stepIndex + 1 // 1-based indexing for humans ctx = logging.TestStepNumberContext(ctx, stepNumber) @@ -324,12 +335,10 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest if step.ExpectWarning != nil { logging.HelperResourceDebug(ctx, "Checking TestStep ExpectWarning") - // TODO: Move file open and defer close outside of for ... {} loop. - file, err := os.Open(filepath.Join(wd.GetBaseDir(), "stdout.txt")) + file, err = os.Open(filepath.Join(wd.GetBaseDir(), "stdout.txt")) if err != nil { log.Fatal(err) } - defer file.Close() warningFound := false