Skip to content

Commit

Permalink
Add test for generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 17, 2023
1 parent 7c6c364 commit f0820d8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/conflictless/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func CLI() {
case commandCheck:
check(&cfg)
case commandGen:
generate(&cfg)
Generate(&cfg)
case commandHelp:
help()
default:
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/conflictless/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/ypjama/conflictless-keepachangelog/pkg/schema"
)

func generate(cfg *Config) {
// Generate generates a new version section in the changelog.
func Generate(cfg *Config) {
err := cfg.setBumpFromFlags()
if err != nil {
printErrorAndExit(err.Error(), usageGenerate)
Expand Down
84 changes: 84 additions & 0 deletions internal/pkg/conflictless/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package conflictless_test

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/ypjama/conflictless-keepachangelog/internal/pkg/conflictless"
)

const (
mkdirFileMode = 0o755
writeFileMode = 0o644
)

func writeDataToFile(t *testing.T, data []byte, file *os.File) {
t.Helper()

err := os.WriteFile(file.Name(), data, writeFileMode)
if err != nil {
t.Fatal(err)
}
}

func createFile(t *testing.T, dir, name string) *os.File {
t.Helper()

file, err := os.Create(dir + "/" + name)
if err != nil {
t.Fatal(err)
}

return file
}

func createTempFile(t *testing.T, dir, pattern string) *os.File {
t.Helper()

file, err := os.CreateTemp(dir, pattern)
if err != nil {
t.Fatal(err)
}

return file
}

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

changesDir, err := os.MkdirTemp(os.TempDir(), "changes")
assert.NoError(t, err)

defer os.RemoveAll(changesDir)

os.TempDir()

changesFile := createFile(t, changesDir, "1-foo.json")
defer os.Remove(changesFile.Name())

changelogFile := createTempFile(t, os.TempDir(), "CHANGELOG.md")
defer os.Remove(changelogFile.Name())

gitConfigFile := createTempFile(t, os.TempDir(), ".gitconfig")
defer os.Remove(gitConfigFile.Name())

writeDataToFile(t, []byte(`{"fixed":["foo"]}`), changesFile)
writeDataToFile(t, []byte(changelogContent), changelogFile)
writeDataToFile(t, []byte(gitConfig), gitConfigFile)

flagValueBumpPatch := "patch"

cfg := new(conflictless.Config)
cfg.Flags.Directory = &changesDir
cfg.Flags.Bump = &flagValueBumpPatch
cfg.ChangelogFile = changelogFile.Name()
cfg.RepositoryConfigFile = gitConfigFile.Name()

conflictless.Generate(cfg)

actual, err := os.ReadFile(changelogFile.Name())
assert.NoError(t, err)
assert.Contains(t, string(actual), "## [Unreleased]")
assert.Contains(t, string(actual), "## [1.0.2]")
}
10 changes: 6 additions & 4 deletions internal/pkg/conflictless/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (
"github.com/stretchr/testify/assert"
)

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

gitConfig := `[core]
const (
gitConfig = `[core]
repositoryformatversion = 0
filemode = true
bare = false
Expand All @@ -26,6 +24,10 @@ func TestDetectRepositoryURL(t *testing.T) {
[user]
name = foobar
email = foobar@localhost`
)

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

file, err := os.CreateTemp(os.TempDir(), "gitconfig")
assert.NoError(t, err)
Expand Down

0 comments on commit f0820d8

Please sign in to comment.