Skip to content

Commit

Permalink
[chore] Add test coverage for chloggen/internal/chlog package
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Jul 14, 2023
1 parent 1f85bac commit 093a41c
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 41 deletions.
22 changes: 13 additions & 9 deletions chloggen/internal/chlog/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@ import (
"path/filepath"
"testing"

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

func TestNew(t *testing.T) {
root := "/tmp"
ctx := New(root)
require.Equal(t, root, ctx.rootDir)
require.Equal(t, filepath.Join(root, unreleasedDir), ctx.UnreleasedDir)
require.Equal(t, filepath.Join(root, changelogMD), ctx.ChangelogMD)
require.Equal(t, filepath.Join(root, unreleasedDir, templateYAML), ctx.TemplateYAML)
assert.Equal(t, root, ctx.rootDir)
assert.Equal(t, filepath.Join(root, unreleasedDir), ctx.UnreleasedDir)
assert.Equal(t, filepath.Join(root, changelogMD), ctx.ChangelogMD)
assert.Equal(t, filepath.Join(root, unreleasedDir, templateYAML), ctx.TemplateYAML)
}

func TestWithUnreleasedDir(t *testing.T) {
root := "/tmp"
unreleased := ".test"
ctx := New(root, WithUnreleasedDir(unreleased))
require.Equal(t, root, ctx.rootDir)
require.Equal(t, filepath.Join(root, unreleased), ctx.UnreleasedDir)
require.Equal(t, filepath.Join(root, changelogMD), ctx.ChangelogMD)
require.Equal(t, filepath.Join(root, unreleased, templateYAML), ctx.TemplateYAML)
assert.Equal(t, root, ctx.rootDir)
assert.Equal(t, filepath.Join(root, unreleased), ctx.UnreleasedDir)
assert.Equal(t, filepath.Join(root, changelogMD), ctx.ChangelogMD)
assert.Equal(t, filepath.Join(root, unreleased, templateYAML), ctx.TemplateYAML)
}

func TestRepoRoot(t *testing.T) {
assert.DirExists(t, RepoRoot())
}
168 changes: 168 additions & 0 deletions chloggen/internal/chlog/entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// 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 (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

func TestEntry(t *testing.T) {
testCases := []struct {
name string
entry Entry
expectErr string
toString string
}{
{
name: "empty",
entry: Entry{},
expectErr: "'' is not a valid 'change_type'. Specify one of [breaking deprecation new_component enhancement bug_fix]",
},
{
name: "missing_component",
entry: Entry{
ChangeType: "enhancement",
Note: "enhance!",
Issues: []int{123},
SubText: "",
},
expectErr: "specify a 'component'",
},
{
name: "missing_note",
entry: Entry{
ChangeType: "bug_fix",
Component: "bar",
Issues: []int{123},
SubText: "",
},
expectErr: "specify a 'note'",
},
{
name: "missing_issue",
entry: Entry{
ChangeType: "bug_fix",
Component: "bar",
Note: "fix bar",
SubText: "",
},
expectErr: "specify one or more issues #'s",
},
{
name: "valid",
entry: Entry{
ChangeType: "breaking",
Component: "foo",
Note: "broke foo",
Issues: []int{123},
SubText: "",
},
toString: "- `foo`: broke foo (#123)",
},
{
name: "multiple_issues",
entry: Entry{
ChangeType: "breaking",
Component: "foo",
Note: "broke foo",
Issues: []int{123, 345},
SubText: "",
},
toString: "- `foo`: broke foo (#123, #345)",
},
{
name: "subtext",
entry: Entry{
ChangeType: "breaking",
Component: "foo",
Note: "broke foo",
Issues: []int{123},
SubText: "more details",
},
toString: "- `foo`: broke foo (#123)\n more details",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.entry.Validate()
if tc.expectErr != "" {
assert.Equal(t, tc.expectErr, err.Error())
return
}
assert.NoError(t, err)
assert.Equal(t, tc.toString, tc.entry.String())
})
}

}

func TestReadDeleteEntries(t *testing.T) {
tempDir := t.TempDir()
entriesDir := filepath.Join(tempDir, ".chloggen")
require.NoError(t, os.Mkdir(entriesDir, os.ModePerm))

entryA := Entry{
ChangeType: "breaking",
Component: "foo",
Note: "broke foo",
Issues: []int{123},
}

bytesA, err := yaml.Marshal(entryA)
require.NoError(t, err)

fileA, err := os.CreateTemp(entriesDir, "*.yaml")
require.NoError(t, err)
defer fileA.Close()

_, err = fileA.Write(bytesA)
require.NoError(t, err)

entryB := Entry{
ChangeType: "bug_fix",
Component: "bar",
Note: "fix bar",
Issues: []int{345, 678},
SubText: "more details",
}

bytesB, err := yaml.Marshal(entryB)
require.NoError(t, err)

fileB, err := os.CreateTemp(entriesDir, "*.yaml")
require.NoError(t, err)
defer fileB.Close()

_, err = fileB.Write(bytesB)
require.NoError(t, err)

chloggenCtx := New(tempDir)
entries, err := ReadEntries(chloggenCtx)
assert.NoError(t, err)

assert.ElementsMatch(t, []*Entry{&entryA, &entryB}, entries)

assert.NoError(t, DeleteEntries(chloggenCtx))
entries, err = ReadEntries(chloggenCtx)
assert.NoError(t, err)
assert.Empty(t, entries)
}
106 changes: 74 additions & 32 deletions chloggen/internal/chlog/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,87 @@
package chlog

import (
"os"
"path/filepath"
"testing"

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

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 🚩
func TestSummary(t *testing.T) {

foo
bar
### 🚀 New components 🚀
brk1 := Entry{
ChangeType: Breaking,
Component: "foo",
Note: "broke foo",
Issues: []int{123},
}
brk2 := Entry{
ChangeType: Breaking,
Component: "bar",
Note: "broke bar",
Issues: []int{345, 678},
SubText: "more details",
}
dep1 := Entry{
ChangeType: Deprecation,
Component: "foo",
Note: "deprecate foo",
Issues: []int{1234},
}
dep2 := Entry{
ChangeType: Deprecation,
Component: "bar",
Note: "deprecate bar",
Issues: []int{3456, 6789},
SubText: "more details",
}
enh1 := Entry{
ChangeType: Enhancement,
Component: "foo",
Note: "enhance foo",
Issues: []int{12},
}
enh2 := Entry{
ChangeType: Enhancement,
Component: "bar",
Note: "enhance bar",
Issues: []int{34, 67},
SubText: "more details",
}
bug1 := Entry{
ChangeType: BugFix,
Component: "foo",
Note: "bug foo",
Issues: []int{1},
}
bug2 := Entry{
ChangeType: BugFix,
Component: "bar",
Note: "bug bar",
Issues: []int{3, 6},
SubText: "more details",
}
new1 := Entry{
ChangeType: NewComponent,
Component: "foo",
Note: "new foo",
Issues: []int{2},
}
new2 := Entry{
ChangeType: NewComponent,
Component: "bar",
Note: "new bar",
Issues: []int{4, 7},
SubText: "more details",
}

foo
bar
new component
actual, err := GenerateSummary("1.0", []*Entry{&brk1, &brk2, &dep1, &dep2, &enh1, &enh2, &bug1, &bug2, &new1, &new2})
assert.NoError(t, err)

### 🧰 Bug fixes 🧰
expected, err := os.ReadFile(filepath.Join("testdata", "CHANGELOG.md"))
require.NoError(t, err)

foo
bar
foobar
`, result)
assert.Equal(t, string(expected), actual)
}
32 changes: 32 additions & 0 deletions chloggen/internal/chlog/testdata/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

## 1.0

### 🛑 Breaking changes 🛑

- `foo`: broke foo (#123)
- `bar`: broke bar (#345, #678)
more details

### 🚩 Deprecations 🚩

- `foo`: deprecate foo (#1234)
- `bar`: deprecate bar (#3456, #6789)
more details

### 🚀 New components 🚀

- `foo`: new foo (#2)
- `bar`: new bar (#4, #7)
more details

### 💡 Enhancements 💡

- `foo`: enhance foo (#12)
- `bar`: enhance bar (#34, #67)
more details

### 🧰 Bug fixes 🧰

- `foo`: bug foo (#1)
- `bar`: bug bar (#3, #6)
more details

0 comments on commit 093a41c

Please sign in to comment.