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

[chloggen] Use go embed to embed the template file #225

Merged
merged 4 commits into from
Jan 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/use-go-embed-for-chloggen-template.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: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. crosslink)
component: chloggen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Use go embed to embed the template file in chloggen

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

# (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:
10 changes: 0 additions & 10 deletions chloggen/internal/chlog/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
)

const (
Expand Down Expand Up @@ -65,12 +64,3 @@ func RepoRoot() string {
}
return dir
}

func moduleDir() string {
_, filename, _, ok := runtime.Caller(0)
if !ok {
// This is not expected, but just in case
fmt.Println("FAIL: Could not determine module directory")
}
return filepath.Dir(filename)
}
9 changes: 5 additions & 4 deletions chloggen/internal/chlog/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ package chlog

import (
"bytes"
_ "embed"
"fmt"
"path/filepath"
"sort"
"text/template"
)

//go:embed summary.tmpl
var tmpl []byte

type summary struct {
Version string
BreakingChanges []string
Expand Down Expand Up @@ -61,13 +64,11 @@ func GenerateSummary(version string, entries []*Entry) (string, error) {
}

func (s summary) String() (string, error) {
summaryTmpl := filepath.Join(moduleDir(), "summary.tmpl")

tmpl := template.Must(
template.
New("summary.tmpl").
Option("missingkey=error").
ParseFiles(summaryTmpl))
Parse(string(tmpl)))

buf := bytes.Buffer{}
if err := tmpl.Execute(&buf, s); err != nil {
Expand Down
59 changes: 59 additions & 0 deletions chloggen/internal/chlog/summary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 chlog

import (
"testing"

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

func Test_SummaryString(t *testing.T) {
s := summary{
Version: "1.0",
BreakingChanges: []string{"foo", "bar"},
Deprecations: []string{"foo", "bar"},
NewComponents: []string{"foo", "bar", "new component"},
Enhancements: []string{},
BugFixes: []string{"foo", "bar", "foobar"},
}
result, err := s.String()
assert.NoError(t, err)
assert.Equal(t, `
## 1.0

### 🛑 Breaking changes 🛑

foo
bar

### 🚩 Deprecations 🚩

foo
bar

### 🚀 New components 🚀

foo
bar
new component

### 🧰 Bug fixes 🧰

foo
bar
foobar
`, result)
}