Skip to content

Commit

Permalink
[Builder]: Generate test file for config struct check (#4594)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Dec 21, 2021
1 parent 1aa81d7 commit 8e1b819
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 31 deletions.
35 changes: 9 additions & 26 deletions cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,16 @@ func Generate(cfg Config) error {
return fmt.Errorf("failed to create output path: %w", err)
}

for _, file := range []struct {
outFile string
tmpl *template.Template
}{
{
"main.go",
mainTemplate,
},
{
"main_others.go",
mainOthersTemplate,
},
{
"main_windows.go",
mainWindowsTemplate,
},
{
"components.go",
componentsTemplate,
},
{
"go.mod",
goModTemplate,
},
for _, tmpl := range []*template.Template{
mainTemplate,
mainOthersTemplate,
mainWindowsTemplate,
componentsTemplate,
componentsTestTemplate,
goModTemplate,
} {
if err := processAndWrite(cfg, file.tmpl, file.outFile, cfg); err != nil {
return fmt.Errorf("failed to generate source file with destination %q, source: %q: %w", file.outFile, file.tmpl.Name(), err)
if err := processAndWrite(cfg, tmpl, tmpl.Name(), cfg); err != nil {
return fmt.Errorf("failed to generate source file %q: %w", tmpl.Name(), err)
}
}

Expand Down
14 changes: 9 additions & 5 deletions cmd/builder/internal/builder/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,27 @@ import (
var (
//go:embed templates/components.go.tmpl
componentsBytes []byte
componentsTemplate = parseTemplate("component_header.go.tmpl", componentsBytes)
componentsTemplate = parseTemplate("components.go", componentsBytes)

//go:embed templates/components_test.go.tmpl
componentsTestBytes []byte
componentsTestTemplate = parseTemplate("components_test.go", componentsTestBytes)

//go:embed templates/main.go.tmpl
mainBytes []byte
mainTemplate = parseTemplate("main.go.tmpl", mainBytes)
mainTemplate = parseTemplate("main.go", mainBytes)

//go:embed templates/main_others.go.tmpl
mainOthersBytes []byte
mainOthersTemplate = parseTemplate("main_others.go.tmpl", mainOthersBytes)
mainOthersTemplate = parseTemplate("main_others.go", mainOthersBytes)

//go:embed templates/main_windows.go.tmpl
mainWindowsBytes []byte
mainWindowsTemplate = parseTemplate("main_windows.go.tmpl", mainWindowsBytes)
mainWindowsTemplate = parseTemplate("main_windows.go", mainWindowsBytes)

//go:embed templates/go.mod.tmpl
goModBytes []byte
goModTemplate = parseTemplate("go.mod.tmpl", goModBytes)
goModTemplate = parseTemplate("go.mod", goModBytes)
)

func parseTemplate(name string, bytes []byte) *template.Template {
Expand Down
29 changes: 29 additions & 0 deletions cmd/builder/internal/builder/templates/components_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Code generated by "go.opentelemetry.io/collector/cmd/builder". DO NOT EDIT.

package main

import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/config/configtest"
)

func TestValidateConfigs(t *testing.T) {
factories, err := components()
assert.NoError(t, err)

for _, factory := range factories.Receivers {
assert.NoError(t, configtest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for _, factory := range factories.Processors {
assert.NoError(t, configtest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for _, factory := range factories.Exporters {
assert.NoError(t, configtest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
for _, factory := range factories.Extensions {
assert.NoError(t, configtest.CheckConfigStruct(factory.CreateDefaultConfig()))
}
}
29 changes: 29 additions & 0 deletions cmd/otelcorecol/components_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/otelcorecol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module go.opentelemetry.io/collector/cmd/otelcorecol
go 1.17

require (
github.com/stretchr/testify v1.7.0
go.opentelemetry.io/collector v0.41.0
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d
)
Expand All @@ -14,6 +15,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/go-kit/log v0.1.0 // indirect
github.com/go-logfmt/logfmt v0.5.0 // indirect
Expand All @@ -36,6 +38,7 @@ require (
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.1.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down Expand Up @@ -72,6 +75,7 @@ require (
google.golang.org/grpc v1.43.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

replace go.opentelemetry.io/collector => ../../
Expand Down

0 comments on commit 8e1b819

Please sign in to comment.