Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 6, 2024
1 parent 11fb6bc commit dad9d14
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/pkg/conflictless/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestCLIHelp(t *testing.T) {
{"help check", []string{"help", "check"}, false},
{"help create", []string{"help", "create"}, false},
{"help generate", []string{"help", "generate"}, false},
{"help preview", []string{"help", "preview"}, false},
{"help unknown", []string{"help", "unknown"}, true},
} {
t.Run(testCase.description, func(t *testing.T) {
Expand Down
55 changes: 55 additions & 0 deletions internal/pkg/conflictless/conflictless_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package conflictless_test

import (
"bytes"
"io"
"os"
"testing"
)

type readWriteCapture struct {
original *os.File
read *os.File
write *os.File
outChannel chan string
}

const (
mkdirFileMode = 0o755
writeFileMode = 0o644
)

//nolint:gochecknoglobals
var stdoutCapture *readWriteCapture

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

Expand Down Expand Up @@ -40,3 +52,46 @@ func createTempFile(t *testing.T, dir, pattern string) *os.File {

return file
}

func startStdoutCapture(t *testing.T) {
t.Helper()

stdoutCapture = new(readWriteCapture)
stdoutCapture.original = os.Stdout

read, write, _ := os.Pipe()
stdoutCapture.read = read
stdoutCapture.write = write

os.Stdout = stdoutCapture.write

stdoutCapture.outChannel = make(chan string)

go func() {
var buf bytes.Buffer

_, err := io.Copy(&buf, read)
if err != nil {
t.Error(err.Error())
}

stdoutCapture.outChannel <- buf.String()
}()
}

func stopStdoutCapture(t *testing.T) string {
t.Helper()

if stdoutCapture == nil {
t.Errorf("could not stop stdout capture because stdout capture wasn't initialized")
}

stdoutCapture.write.Close()
os.Stdout = stdoutCapture.original

output := <-stdoutCapture.outChannel

stdoutCapture = nil

return output
}
3 changes: 1 addition & 2 deletions internal/pkg/conflictless/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ func TestGenerate(t *testing.T) {

defer os.RemoveAll(changesDir)

os.TempDir()

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

Expand All @@ -36,6 +34,7 @@ func TestGenerate(t *testing.T) {
cfg := new(conflictless.Config)
cfg.Flags.Directory = &changesDir
cfg.Flags.Bump = &flagValueBumpPatch
cfg.Flags.SkipVersionLinks = true
cfg.ChangelogFile = changelogFile.Name()
cfg.RepositoryConfigFile = gitConfigFile.Name()

Expand Down
50 changes: 50 additions & 0 deletions internal/pkg/conflictless/preview_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package conflictless_test

import (
"os"
"testing"

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

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

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

defer os.RemoveAll(changesDir)

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

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

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

writeDataToFile(t, []byte(`{"added":["New major feature"]}`), changesFile)
writeDataToFile(t, []byte(changelogContent), changelogFile)
writeDataToFile(t, []byte(gitConfig), gitConfigFile)

flagValueBumpPatch := "major"

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

startStdoutCapture(t)

conflictless.Preview(cfg)

output := stopStdoutCapture(t)

assert.Contains(t, output, "```md\n")
assert.Contains(t, output, "## [2.0.0]")
assert.Contains(t, output, "### Added\n\n- New major feature\n")
}

0 comments on commit dad9d14

Please sign in to comment.