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

[chore][chloggen] Rename 'Context' to 'Config' and move to dedicated package #370

Merged
merged 1 commit into from
Jul 20, 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
11 changes: 6 additions & 5 deletions chloggen/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"gopkg.in/yaml.v3"

"go.opentelemetry.io/build-tools/chloggen/internal/chlog"
"go.opentelemetry.io/build-tools/chloggen/internal/config"
)

func getSampleEntries() []*chlog.Entry {
Expand Down Expand Up @@ -97,18 +98,18 @@ func entryWithSubtext() *chlog.Entry {
}
}

func setupTestDir(t *testing.T, entries []*chlog.Entry) chlog.Context {
ctx := chlog.New(t.TempDir())
func setupTestDir(t *testing.T, entries []*chlog.Entry) config.Config {
ctx := config.New(t.TempDir())

// Create a known dummy changelog which may be updated by the test
changelogBytes, err := os.ReadFile(filepath.Join("testdata", chlog.DefaultChangelogMD))
changelogBytes, err := os.ReadFile(filepath.Join("testdata", config.DefaultChangelogMD))
require.NoError(t, err)
require.NoError(t, os.WriteFile(ctx.ChangelogMD, changelogBytes, os.FileMode(0755)))

require.NoError(t, os.Mkdir(ctx.ChloggenDir, os.FileMode(0755)))

// Copy the entry template, for tests that ensure it is not deleted
templateInRootDir := chlog.New("testdata").TemplateYAML
templateInRootDir := config.New("testdata").TemplateYAML
templateBytes, err := os.ReadFile(filepath.Clean(templateInRootDir))
require.NoError(t, err)
require.NoError(t, os.WriteFile(ctx.TemplateYAML, templateBytes, os.FileMode(0755)))
Expand All @@ -120,7 +121,7 @@ func setupTestDir(t *testing.T, entries []*chlog.Entry) chlog.Context {
return ctx
}

func writeEntryYAML(ctx chlog.Context, filename string, entry *chlog.Entry) error {
func writeEntryYAML(ctx config.Config, filename string, entry *chlog.Entry) error {
entryBytes, err := yaml.Marshal(entry)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions chloggen/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func newCmd() *cobra.Command {
Use: "new",
Short: "Creates new change file",
RunE: func(cmd *cobra.Command, args []string) error {
path := filepath.Join(chlogCtx.ChloggenDir, cleanFileName(filename))
path := filepath.Join(globalCfg.ChloggenDir, cleanFileName(filename))
var pathWithExt string
switch ext := filepath.Ext(path); ext {
case ".yaml":
Expand All @@ -42,7 +42,7 @@ func newCmd() *cobra.Command {
pathWithExt = path + ".yaml"
}

templateBytes, err := os.ReadFile(filepath.Clean(chlogCtx.TemplateYAML))
templateBytes, err := os.ReadFile(filepath.Clean(globalCfg.TemplateYAML))
if err != nil {
return err
}
Expand Down
21 changes: 12 additions & 9 deletions chloggen/cmd/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Flags:
Global Flags:
--chloggen-directory string directory containing unreleased change log entries (default: .chloggen)`

func TestNewCommand(t *testing.T) {
func TestNewErr(t *testing.T) {
var out, err string

out, err = runCobra(t, "new", "--help")
Expand All @@ -48,32 +48,35 @@ func TestNewCommand(t *testing.T) {
out, err = runCobra(t, "new", "--filename", "my-change")
assert.Contains(t, out, newUsage)
assert.Contains(t, err, `no such file or directory`)
}

func TestNew(t *testing.T) {
globalCfg = setupTestDir(t, []*chlog.Entry{})

// Set up a test directory to which we will write new files
chlogCtx = setupTestDir(t, []*chlog.Entry{})
var out, err string

out, err = runCobra(t, "new", "--filename", "my-change")
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(chlogCtx.ChloggenDir, "my-change.yaml")))
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(globalCfg.ChloggenDir, "my-change.yaml")))
assert.Empty(t, err)

out, err = runCobra(t, "new", "--filename", "some-change.yaml")
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(chlogCtx.ChloggenDir, "some-change.yaml")))
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(globalCfg.ChloggenDir, "some-change.yaml")))
assert.Empty(t, err)

out, err = runCobra(t, "new", "--filename", "some-change.yml")
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(chlogCtx.ChloggenDir, "some-change.yaml")))
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(globalCfg.ChloggenDir, "some-change.yaml")))
assert.Empty(t, err)

out, err = runCobra(t, "new", "--filename", "replace/forward/slash")
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(chlogCtx.ChloggenDir, "replace_forward_slash.yaml")))
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(globalCfg.ChloggenDir, "replace_forward_slash.yaml")))
assert.Empty(t, err)

out, err = runCobra(t, "new", "--filename", "not.an.extension")
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(chlogCtx.ChloggenDir, "not.an.extension.yaml")))
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(globalCfg.ChloggenDir, "not.an.extension.yaml")))
assert.Empty(t, err)

out, err = runCobra(t, "new", "--filename", "my-change.txt")
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(chlogCtx.ChloggenDir, "my-change.txt.yaml")))
assert.Contains(t, out, fmt.Sprintf("Changelog entry template copied to: %s", filepath.Join(globalCfg.ChloggenDir, "my-change.txt.yaml")))
assert.Empty(t, err)
}

Expand Down
22 changes: 17 additions & 5 deletions chloggen/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

"go.opentelemetry.io/build-tools/chloggen/internal/chlog"
"go.opentelemetry.io/build-tools/chloggen/internal/config"
)

var (
chloggenDir string
chlogCtx chlog.Context
globalCfg config.Config
)

func rootCmd() *cobra.Command {
Expand All @@ -48,13 +51,22 @@

func initConfig() {
// Don't override if already set in tests
var uninitialized chlog.Context
if chlogCtx != uninitialized {
var uninitialized config.Config
if globalCfg != uninitialized {
return
}

if chloggenDir == "" {
chloggenDir = ".chloggen"
}
chlogCtx = chlog.New(chlog.RepoRoot(), chlog.WithChloggenDir(chloggenDir))
globalCfg = config.New(repoRoot(), config.WithChloggenDir(chloggenDir))
}

func repoRoot() string {
dir, err := os.Getwd()
if err != nil {
// This is not expected, but just in case
fmt.Println("FAIL: Could not determine current working directory")
}

Check warning on line 70 in chloggen/cmd/root.go

View check run for this annotation

Codecov / codecov/patch

chloggen/cmd/root.go#L68-L70

Added lines #L68 - L70 were not covered by tests
return dir
}
6 changes: 5 additions & 1 deletion chloggen/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Flags:

Use "chloggen [command] --help" for more information about a command.`

func TestRootCommand(t *testing.T) {
func TestRoot(t *testing.T) {
var out, err string

out, err = runCobra(t)
Expand All @@ -49,3 +49,7 @@ func TestRootCommand(t *testing.T) {
assert.Contains(t, out, rootUsage)
assert.Empty(t, err)
}

func TestRepoRoot(t *testing.T) {
assert.DirExists(t, repoRoot())
}
12 changes: 6 additions & 6 deletions chloggen/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func updateCmd() *cobra.Command {
Use: "update",
Short: "Updates CHANGELOG.MD to include all new changes",
RunE: func(cmd *cobra.Command, args []string) error {
entries, err := chlog.ReadEntries(chlogCtx)
entries, err := chlog.ReadEntries(globalCfg)
if err != nil {
return err
}
Expand All @@ -60,7 +60,7 @@ func updateCmd() *cobra.Command {
return nil
}

oldChlogBytes, err := os.ReadFile(filepath.Clean(chlogCtx.ChangelogMD))
oldChlogBytes, err := os.ReadFile(filepath.Clean(globalCfg.ChangelogMD))
if err != nil {
return err
}
Expand All @@ -77,18 +77,18 @@ func updateCmd() *cobra.Command {
chlogBuilder.WriteString(chlogUpdate)
chlogBuilder.WriteString(chlogHistory)

tmpMD := chlogCtx.ChangelogMD + ".tmp"
tmpMD := globalCfg.ChangelogMD + ".tmp"
if err = os.WriteFile(filepath.Clean(tmpMD), []byte(chlogBuilder.String()), 0600); err != nil {
return err
}

if err = os.Rename(tmpMD, chlogCtx.ChangelogMD); err != nil {
if err = os.Rename(tmpMD, globalCfg.ChangelogMD); err != nil {
return err
}

cmd.Printf("Finished updating %s\n", chlogCtx.ChangelogMD)
cmd.Printf("Finished updating %s\n", globalCfg.ChangelogMD)

return chlog.DeleteEntries(chlogCtx)
return chlog.DeleteEntries(globalCfg)
},
}
cmd.Flags().StringVarP(&version, "version", "v", "vTODO", "will be rendered directly into the update text")
Expand Down
36 changes: 30 additions & 6 deletions chloggen/cmd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,34 @@ import (
"go.opentelemetry.io/build-tools/chloggen/internal/chlog"
)

func TestUpdateE2E(t *testing.T) {
const updateUsage = `Usage:
chloggen update [flags]

Flags:
-d, --dry will generate the update text and print to stdout
-h, --help help for update
-v, --version string will be rendered directly into the update text (default "vTODO")

Global Flags:
--chloggen-directory string directory containing unreleased change log entries (default: .chloggen)`

func TestUpdateErr(t *testing.T) {
var out, err string

out, err = runCobra(t, "update", "--help")
assert.Contains(t, out, updateUsage)
assert.Empty(t, err)

out, err = runCobra(t, "update")
assert.Contains(t, out, updateUsage)
assert.Contains(t, err, "no entries to add to the changelog")
}

func TestUpdate(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Windows line breaks cause comparison failures w/ golden files.")
}

tests := []struct {
name string
entries []*chlog.Entry
Expand Down Expand Up @@ -87,7 +111,7 @@ func TestUpdateE2E(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
chlogCtx = setupTestDir(t, tc.entries)
globalCfg = setupTestDir(t, tc.entries)

args := []string{"update", "--version", tc.version}
if tc.dry {
Expand All @@ -101,10 +125,10 @@ func TestUpdateE2E(t *testing.T) {
if tc.dry {
assert.Contains(t, out, "Generated changelog updates:")
} else {
assert.Contains(t, out, fmt.Sprintf("Finished updating %s", chlogCtx.ChangelogMD))
assert.Contains(t, out, fmt.Sprintf("Finished updating %s", globalCfg.ChangelogMD))
}

actualBytes, ioErr := os.ReadFile(chlogCtx.ChangelogMD)
actualBytes, ioErr := os.ReadFile(globalCfg.ChangelogMD)
require.NoError(t, ioErr)

expectedChangelogMD := filepath.Join("testdata", tc.name+".md")
Expand All @@ -113,13 +137,13 @@ func TestUpdateE2E(t *testing.T) {

require.Equal(t, string(expectedBytes), string(actualBytes))

remainingYAMLs, ioErr := filepath.Glob(filepath.Join(chlogCtx.ChloggenDir, "*.yaml"))
remainingYAMLs, ioErr := filepath.Glob(filepath.Join(globalCfg.ChloggenDir, "*.yaml"))
require.NoError(t, ioErr)
if tc.dry {
require.Equal(t, 1+len(tc.entries), len(remainingYAMLs))
} else {
require.Equal(t, 1, len(remainingYAMLs))
require.Equal(t, chlogCtx.TemplateYAML, remainingYAMLs[0])
require.Equal(t, globalCfg.TemplateYAML, remainingYAMLs[0])
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions chloggen/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func validateCmd() *cobra.Command {
Use: "validate",
Short: "Validates the files in the changelog directory",
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := os.Stat(chlogCtx.ChloggenDir); err != nil {
if _, err := os.Stat(globalCfg.ChloggenDir); err != nil {
return err
}

entries, err := chlog.ReadEntries(chlogCtx)
entries, err := chlog.ReadEntries(globalCfg)
if err != nil {
return err
}
Expand All @@ -40,7 +40,7 @@ func validateCmd() *cobra.Command {
return err
}
}
cmd.Printf("PASS: all files in %s/ are valid\n", chlogCtx.ChloggenDir)
cmd.Printf("PASS: all files in %s/ are valid\n", globalCfg.ChloggenDir)
return nil
},
}
Expand Down
27 changes: 24 additions & 3 deletions chloggen/cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,28 @@ import (
"go.opentelemetry.io/build-tools/chloggen/internal/chlog"
)

func TestValidateE2E(t *testing.T) {
const validateUsage = `Usage:
chloggen validate [flags]

Flags:
-h, --help help for validate

Global Flags:
--chloggen-directory string directory containing unreleased change log entries (default: .chloggen)`

func TestValidateErr(t *testing.T) {
var out, err string

out, err = runCobra(t, "validate", "--help")
assert.Contains(t, out, validateUsage)
assert.Empty(t, err)

out, err = runCobra(t, "validate")
assert.Contains(t, out, validateUsage)
assert.Contains(t, err, "no such file or directory")
}

func TestValidate(t *testing.T) {
tests := []struct {
name string
entries []*chlog.Entry
Expand Down Expand Up @@ -119,15 +140,15 @@ func TestValidateE2E(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
chlogCtx = setupTestDir(t, tc.entries)
globalCfg = setupTestDir(t, tc.entries)

out, err := runCobra(t, "validate")

if tc.wantErr != "" {
assert.Regexp(t, tc.wantErr, err)
} else {
assert.Empty(t, err)
assert.Contains(t, out, fmt.Sprintf("PASS: all files in %s/ are valid", chlogCtx.ChloggenDir))
assert.Contains(t, out, fmt.Sprintf("PASS: all files in %s/ are valid", globalCfg.ChloggenDir))
}
})
}
Expand Down
Loading
Loading