Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[builder] Close files immediately after writing is complete #9466

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,6 @@ func processAndWrite(cfg Config, tmpl *template.Template, outFile string, tmplPa
return err
}

defer out.Close()
return tmpl.Execute(out, tmplParams)
}
15 changes: 3 additions & 12 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -36,25 +35,21 @@ func TestGenerateInvalidOutputPath(t *testing.T) {
}

func TestSkipGenerate(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping the test on Windows, see https://github.com/open-telemetry/opentelemetry-collector/issues/5403")
}

cfg := NewDefaultConfig()
cfg.Distribution.OutputPath = t.TempDir()
cfg.SkipGenerate = true
err := Generate(cfg)
require.NoError(t, err)
outputFile, err := os.Open(cfg.Distribution.OutputPath)
defer func() {
require.NoError(t, outputFile.Close())
}()
require.NoError(t, err)
_, err = outputFile.Readdirnames(1)
require.ErrorIs(t, err, io.EOF, "skip generate should leave output directory empty")
}

func TestGenerateAndCompile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping the test on Windows, see https://github.com/open-telemetry/opentelemetry-collector/issues/5403")
}
// This test is dependent on the current file structure.
// The goal is find the root of the repo so we can replace the root module.
_, thisFile, _, _ := runtime.Caller(0)
Expand Down Expand Up @@ -129,8 +124,4 @@ func TestGenerateAndCompile(t *testing.T) {
require.NoError(t, GenerateAndCompile(cfg))
})
}

// Sleep for 1 second to make sure all processes using the files are completed
// (on Windows fail to delete temp dir otherwise).
time.Sleep(1 * time.Second)
}