From 71f08775c06ac96c7342b2e788eb4e4ac6c625ab Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Tue, 3 Jan 2023 13:45:47 +0000 Subject: [PATCH] Updating defer func to log error and fail test if closing file containing output from stdout returns an error (#16) --- helper/resource/testing_new.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/helper/resource/testing_new.go b/helper/resource/testing_new.go index 10572700f..2b6493d0e 100644 --- a/helper/resource/testing_new.go +++ b/helper/resource/testing_new.go @@ -125,15 +125,21 @@ 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() + // stdoutFile is used as the handle to the file that contains + // json output from running Terraform commands. + var stdoutFile *os.File + defer func() { + if stdoutFile != nil { + err := stdoutFile.Close() if err != nil { - return err + logging.HelperResourceError(ctx, + "Error closing file containing json output from Terraform commands", + map[string]interface{}{logging.KeyError: err}, + ) + t.Fatalf("Error closing file containing json from Terraform commands: %s", err.Error()) + return } } - return nil }() for stepIndex, step := range c.Steps { @@ -338,7 +344,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest if step.ExpectWarning != nil { logging.HelperResourceDebug(ctx, "Checking TestStep ExpectWarning") - file, err = os.Open(filepath.Join(wd.GetBaseDir(), "stdout.txt")) + stdoutFile, err = os.Open(filepath.Join(wd.GetBaseDir(), "stdout.txt")) if err != nil { log.Fatal(err) } @@ -346,7 +352,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest warningFound := false // TODO: Use MultiLineJSONDecode providing entire contents of stdout.txt is valid JSON. - scanner := bufio.NewScanner(file) + scanner := bufio.NewScanner(stdoutFile) // optionally, resize scanner's capacity for lines over 64K, see next example for scanner.Scan() { var outer struct { @@ -356,6 +362,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest if json.Unmarshal([]byte(scanner.Text()), &outer) == nil { if step.ExpectWarning.MatchString(outer.Diagnostic.Summary) && outer.Diagnostic.Severity == tfjson.DiagnosticSeverityWarning { warningFound = true + break } } }