Skip to content

Commit

Permalink
Moving defer file.Close outside for loop (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jan 3, 2023
1 parent e8f0f74 commit eb37ad8
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit eb37ad8

Please sign in to comment.