From dad9d14e4f3bcfdbe94f4725fe4ab2233537f307 Mon Sep 17 00:00:00 2001 From: ypjama <3822534+ypjama@users.noreply.github.com> Date: Sun, 6 Oct 2024 11:04:10 +0300 Subject: [PATCH] Add tests --- internal/pkg/conflictless/cli_test.go | 1 + .../pkg/conflictless/conflictless_test.go | 55 +++++++++++++++++++ internal/pkg/conflictless/generate_test.go | 3 +- internal/pkg/conflictless/preview_test.go | 50 +++++++++++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 internal/pkg/conflictless/preview_test.go diff --git a/internal/pkg/conflictless/cli_test.go b/internal/pkg/conflictless/cli_test.go index 6898a09..6e31408 100644 --- a/internal/pkg/conflictless/cli_test.go +++ b/internal/pkg/conflictless/cli_test.go @@ -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) { diff --git a/internal/pkg/conflictless/conflictless_test.go b/internal/pkg/conflictless/conflictless_test.go index 61ba63c..d751764 100644 --- a/internal/pkg/conflictless/conflictless_test.go +++ b/internal/pkg/conflictless/conflictless_test.go @@ -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() @@ -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 +} diff --git a/internal/pkg/conflictless/generate_test.go b/internal/pkg/conflictless/generate_test.go index c6b6b41..37803d8 100644 --- a/internal/pkg/conflictless/generate_test.go +++ b/internal/pkg/conflictless/generate_test.go @@ -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()) @@ -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() diff --git a/internal/pkg/conflictless/preview_test.go b/internal/pkg/conflictless/preview_test.go new file mode 100644 index 0000000..749982f --- /dev/null +++ b/internal/pkg/conflictless/preview_test.go @@ -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") +}