Skip to content

Commit

Permalink
Add tests for print functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 17, 2023
1 parent c268bfd commit 773943c
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 18 deletions.
4 changes: 2 additions & 2 deletions internal/pkg/conflictless/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package conflictless
func check(cfg *Config) {
combined, err := scanDir(*cfg.Flags.Directory)
if err != nil {
printErrorAndExit(err.Error(), func() {})
PrintErrorAndExit(err.Error(), func() {})
}

printCheckSuccess(combined.IsEmpty())
PrintCheckSuccess(combined.IsEmpty())
}
4 changes: 2 additions & 2 deletions internal/pkg/conflictless/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func CLI() {
parseCLIFlags(&cfg)

if cfg.Flags.Command == "" {
printUsageAndExit(&cfg)
PrintUsageAndExit(&cfg)
}

switch cfg.Flags.Command {
Expand All @@ -46,7 +46,7 @@ func CLI() {
case commandHelp:
help()
default:
printErrorAndExit(fmt.Sprintf("invalid command: '%s'", cfg.Flags.Command), usage)
PrintErrorAndExit(fmt.Sprintf("invalid command: '%s'", cfg.Flags.Command), usage)
}
}

Expand Down
16 changes: 8 additions & 8 deletions internal/pkg/conflictless/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@ import (
func Generate(cfg *Config) {
err := cfg.SetBumpFromFlags()
if err != nil {
printErrorAndExit(err.Error(), usageGenerate)
PrintErrorAndExit(err.Error(), usageGenerate)
}

cfg.Changelog, err = ReadChangelog(cfg)
if err != nil {
printErrorAndExit(err.Error(), func() {})
PrintErrorAndExit(err.Error(), func() {})
}

combined, err := scanDir(*cfg.Flags.Directory)
if err != nil {
printErrorAndExit(err.Error(), func() {})
PrintErrorAndExit(err.Error(), func() {})
}

if combined.IsEmpty() {
printErrorAndExit("no changelog entries found", func() {})
PrintErrorAndExit("no changelog entries found", func() {})
}

newSection := DataToMarkdown(cfg, combined)
if newSection == "" {
printErrorAndExit("failed to generate a new version section", func() {})
PrintErrorAndExit("failed to generate a new version section", func() {})
}

err = cfg.Changelog.WriteSection(newSection)
if err != nil {
printErrorAndExit(err.Error(), func() {})
PrintErrorAndExit(err.Error(), func() {})
}

err = removeChangeFiles(*cfg.Flags.Directory)
if err != nil {
printErrorAndExit(err.Error(), func() {})
PrintErrorAndExit(err.Error(), func() {})
}

printGenerateSuccess(newSection)
PrintGenerateSuccess(newSection)
}

func DataToMarkdown(cfg *Config, data *schema.Data) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/conflictless/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func help() {
case "":
usage()
default:
printErrorAndExit(fmt.Sprintf("unknown help topic '%s'", topic), func() {})
PrintErrorAndExit(fmt.Sprintf("unknown help topic '%s'", topic), func() {})
os.Exit(exitCodeMisuseError)
}

Expand Down
14 changes: 9 additions & 5 deletions internal/pkg/conflictless/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"os"
)

func printUsageAndExit(cfg *Config) {
// PrintUsageAndExit prints the usage and exits.
func PrintUsageAndExit(cfg *Config) {
if cfg.Flags.Command == "" {
printErrorAndExit("", usage)
PrintErrorAndExit("", usage)
}

switch cfg.Flags.Command {
Expand All @@ -22,7 +23,8 @@ func printUsageAndExit(cfg *Config) {
os.Exit(exitCodeMisuseError)
}

func printErrorAndExit(msg string, usageFunc func()) {
// PrintErrorAndExit prints an error message and exits.
func PrintErrorAndExit(msg string, usageFunc func()) {
if msg != "" {
//nolint:forbidigo
fmt.Printf("Error: %s\n\n", msg)
Expand All @@ -32,12 +34,14 @@ func printErrorAndExit(msg string, usageFunc func()) {
os.Exit(exitCodeMisuseError)
}

func printGenerateSuccess(section string) {
// PrintGenerateSuccess prints a success message for generate command.
func PrintGenerateSuccess(section string) {
//nolint:forbidigo
fmt.Printf("Generated new version section successfully!\n\n```md\n%s```\n", section)
}

func printCheckSuccess(noContent bool) {
// PrintCheckSuccess prints a success message for check command.
func PrintCheckSuccess(noContent bool) {
var msg string

if noContent {
Expand Down
112 changes: 112 additions & 0 deletions internal/pkg/conflictless/print_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package conflictless_test

import (
"errors"
"os"
"os/exec"
"testing"

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

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

crasherEnv := os.Getenv("TEST_PRINT_ERROR_AND_EXIT_CRASHER")

if crasherEnv != "" {
conflictless.PrintErrorAndExit(crasherEnv, func() {})

return
}

//nolint:gosec // this is a test package so G204 doesn't really matter here.
cmd := exec.Command(os.Args[0], "-test.run=^TestPrintErrorAndExit$")

cmd.Env = append(os.Environ(), "TEST_PRINT_ERROR_AND_EXIT_CRASHER=foobarbaz")
err := cmd.Run()

assert.IsType(t, new(exec.ExitError), err)

exitErr := new(*exec.ExitError)
errors.As(err, exitErr)

assert.False(t, (*exitErr).Success(), "process ran with err %v, want non zero exit status", err)
}

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

crasherEnv := os.Getenv("TEST_PRINT_USAGE_AND_EXIT_CRASHER")
cfg := new(conflictless.Config)

if crasherEnv != "" {
if crasherEnv != "no-cmd" {
cfg.Flags.Command = crasherEnv
}

conflictless.PrintUsageAndExit(cfg)

return
}

for _, crasher := range []string{
"no-cmd",
"check",
"generate",
"usage",
} {
// Reinitialise crasher for parallel testing.
crasher := crasher

t.Run(crasher, func(t *testing.T) {
t.Parallel()

//nolint:gosec // this is a test package so G204 doesn't really matter here.
cmd := exec.Command(os.Args[0], "-test.run=^TestPrintUsageAndExit$")

cmd.Env = append(os.Environ(), "TEST_PRINT_USAGE_AND_EXIT_CRASHER="+crasher)
err := cmd.Run()

assert.IsType(t, new(exec.ExitError), err)

exitErr := new(*exec.ExitError)
errors.As(err, exitErr)

assert.False(t, (*exitErr).Success(), "process ran with err %v, want non zero exit status", err)
})
}
}

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

for _, testCase := range []struct {
description string
noContent bool
expected string
}{
{"no content", true, "No changes found!\n"},
{"content", false, "Change files are valid!\n"},
} {
// Reinitialise testCase for parallel testing.
testCase := testCase

t.Run("", func(t *testing.T) {
t.Parallel()

file := createTempFile(t, os.TempDir(), "test-stdout")
defer os.Remove(file.Name())

os.Stdout = file

conflictless.PrintCheckSuccess(testCase.noContent)

data, err := os.ReadFile(file.Name())

assert.NoError(t, err)
assert.Equal(t, testCase.expected, string(data))
})
}
}

0 comments on commit 773943c

Please sign in to comment.