Skip to content

Commit

Permalink
[cmd/mdatagen] Ensure templates are built as part of the application (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MovieStoreGuy authored Jan 9, 2023
1 parent dc47c10 commit 7711ef4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
16 changes: 16 additions & 0 deletions .chloggen/msg_fix-embed-metadatagen-templates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: cmd/metadata

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Ensure template files are downloaded as part of the `go get` and embeded into the application

# One or more tracking issues related to the change
issues: [17442]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
24 changes: 24 additions & 0 deletions cmd/mdatagen/embeded_templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import "embed"

// templateFS ensures that the files needed
// to generate metadata as an embedded filesystem since
// `go get` doesn't require these files to be downloaded.
//
//go:embed templates/*.tmpl templates/testdata/*.tmpl
var templateFS embed.FS
51 changes: 51 additions & 0 deletions cmd/mdatagen/embeded_templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"io/fs"
"path"
"testing"

"github.com/stretchr/testify/assert"
)

func TestEnsureTemplatesLoaded(t *testing.T) {
t.Parallel()

const (
rootDir = "templates"
)

var (
templateFiles = map[string]struct{}{
path.Join(rootDir, "documentation.md.tmpl"): {},
path.Join(rootDir, "metrics_test.go.tmpl"): {},
path.Join(rootDir, "metrics.go.tmpl"): {},
path.Join(rootDir, "testdata", "config.yaml.tmpl"): {},
}
count = 0
)
assert.NoError(t, fs.WalkDir(templateFS, ".", func(path string, d fs.DirEntry, err error) error {
if d != nil && d.IsDir() {
return nil
}
count++
assert.Contains(t, templateFiles, path)
return nil
}))
assert.Equal(t, len(templateFiles), count, "Must match the expected number of calls")

}
9 changes: 2 additions & 7 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"log"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
)
Expand All @@ -48,11 +47,7 @@ func run(ymlPath string) error {
return fmt.Errorf("failed loading %v: %w", ymlPath, err)
}

_, filename, _, ok := runtime.Caller(0)
if !ok {
return errors.New("unable to determine filename")
}
tmplDir := filepath.Join(filepath.Dir(filename), "templates")
tmplDir := "templates"

codeDir := filepath.Join(ymlDir, "internal", "metadata")
if err = os.MkdirAll(filepath.Join(codeDir, "testdata"), 0700); err != nil {
Expand Down Expand Up @@ -104,7 +99,7 @@ func generateFile(tmplFile string, outputFile string, md metadata) error {
},
"stringsJoin": strings.Join,
"inc": func(i int) int { return i + 1 },
}).ParseFiles(tmplFile))
}).ParseFS(templateFS, tmplFile))

buf := bytes.Buffer{}

Expand Down

0 comments on commit 7711ef4

Please sign in to comment.