Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ypjama committed Oct 5, 2024
1 parent 2de4b7f commit ba2deda
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/pkg/conflictless/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package conflictless

import "fmt"
import (
"fmt"
"strings"
)

// FlagCollection is a collection of flags.
type FlagCollection struct {
Expand Down Expand Up @@ -99,6 +102,7 @@ func (cfg *Config) SetChangeFileFormatFromFlags() error {
}

formatFlag := *cfg.Flags.ChangeFileFormat
formatFlag = strings.ToLower(formatFlag)

switch formatFlag {
case "yaml":
Expand Down
51 changes: 51 additions & 0 deletions internal/pkg/conflictless/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,57 @@ func TestSetBumpFromFlags(t *testing.T) {
}
}

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

for _, testCase := range []struct {
description string
format string
expected string
}{
{"yml", "yml", "yml"},
{"yaml", "yaml", "yaml"},
{"json", "json", "json"},
{"upper_case_json", "JSON", "json"},
{"mixed_case_yaml", "yAmL", "yaml"},
} {
t.Run(testCase.description, func(t *testing.T) {
t.Parallel()

cfg := new(conflictless.Config)
cfg.Flags.ChangeFileFormat = &testCase.format

err := cfg.SetChangeFileFormatFromFlags()
assert.NoError(t, err)
assert.Equal(t, testCase.expected, cfg.ChangeFileFormat)
})
}
}

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

cfg := new(conflictless.Config)
cfg.ChangeFileFormat = "yml"
cfg.Flags.ChangeFileFormat = nil

err := cfg.SetChangeFileFormatFromFlags()
assert.NoError(t, err)
assert.Equal(t, "yml", cfg.ChangeFileFormat)
}

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

invalidFormat := "foo"

cfg := new(conflictless.Config)
cfg.Flags.ChangeFileFormat = &invalidFormat

err := cfg.SetChangeFileFormatFromFlags()
assert.Error(t, err)
}

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

Expand Down

0 comments on commit ba2deda

Please sign in to comment.