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 19a31f3 commit 4af0e48
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 21 deletions.
66 changes: 45 additions & 21 deletions internal/pkg/conflictless/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,43 +137,67 @@ func parseCLIFlags(cfg *Config) {
}
}

func defineChangeLogFlags(cfg *Config, fs *flag.FlagSet) {
func defineChangeLogFlags(cfg *Config, flagset *flag.FlagSet) {
defaultChangelogFile := "CHANGELOG.md"

fs.StringVar(cfg.Flags.ChangelogFile, "changelog", defaultChangelogFile, "")
fs.StringVar(cfg.Flags.ChangelogFile, "c", defaultChangelogFile, "")
if cfg.Flags.ChangelogFile == nil {
cfg.Flags.ChangelogFile = new(string)
}

flagset.StringVar(cfg.Flags.ChangelogFile, "changelog", defaultChangelogFile, "")
flagset.StringVar(cfg.Flags.ChangelogFile, "c", defaultChangelogFile, "")
}

func defineBumpFlags(cfg *Config, fs *flag.FlagSet) {
func defineBumpFlags(cfg *Config, flagset *flag.FlagSet) {
defaultBumpStr := "minor"

fs.StringVar(cfg.Flags.Bump, "bump", defaultBumpStr, "")
fs.StringVar(cfg.Flags.Bump, "b", defaultBumpStr, "")
if cfg.Flags.Bump == nil {
cfg.Flags.Bump = new(string)
}

flagset.StringVar(cfg.Flags.Bump, "bump", defaultBumpStr, "")
flagset.StringVar(cfg.Flags.Bump, "b", defaultBumpStr, "")
}

func defineDirFlags(cfg *Config, fs *flag.FlagSet) {
func defineDirFlags(cfg *Config, flagset *flag.FlagSet) {
defaultDir := "changes"

fs.StringVar(cfg.Flags.Directory, "dir", defaultDir, "")
fs.StringVar(cfg.Flags.Directory, "d", defaultDir, "")
if cfg.Flags.Directory == nil {
cfg.Flags.Directory = new(string)
}

flagset.StringVar(cfg.Flags.Directory, "dir", defaultDir, "")
flagset.StringVar(cfg.Flags.Directory, "d", defaultDir, "")
}

func defineFormatFlags(cfg *Config, fs *flag.FlagSet) {
fs.StringVar(cfg.Flags.ChangeFileFormat, "format", defaultChangeFileFormat, "")
fs.StringVar(cfg.Flags.ChangeFileFormat, "f", defaultChangeFileFormat, "")
func defineFormatFlags(cfg *Config, flagset *flag.FlagSet) {
if cfg.Flags.ChangeFileFormat == nil {
cfg.Flags.ChangeFileFormat = new(string)
}

flagset.StringVar(cfg.Flags.ChangeFileFormat, "format", defaultChangeFileFormat, "")
flagset.StringVar(cfg.Flags.ChangeFileFormat, "f", defaultChangeFileFormat, "")
}

func defineCreateTypeFlags(cfg *Config, fs *flag.FlagSet) {
fs.StringVar(cfg.Flags.ChangeTypesCsv, "types", defaultChangeTypesCSV, "")
fs.StringVar(cfg.Flags.ChangeTypesCsv, "t", defaultChangeTypesCSV, "")
func defineCreateTypeFlags(cfg *Config, flagset *flag.FlagSet) {
if cfg.Flags.ChangeTypesCsv == nil {
cfg.Flags.ChangeTypesCsv = new(string)
}

flagset.StringVar(cfg.Flags.ChangeTypesCsv, "types", defaultChangeTypesCSV, "")
flagset.StringVar(cfg.Flags.ChangeTypesCsv, "t", defaultChangeTypesCSV, "")
}

func defineChangeFileNameFlags(cfg *Config, fs *flag.FlagSet) {
fs.StringVar(cfg.Flags.ChangeFileName, "name", "", "")
fs.StringVar(cfg.Flags.ChangeFileName, "n", "", "")
func defineChangeFileNameFlags(cfg *Config, flagset *flag.FlagSet) {
if cfg.Flags.ChangeFileName == nil {
cfg.Flags.ChangeFileName = new(string)
}

flagset.StringVar(cfg.Flags.ChangeFileName, "name", "", "")
flagset.StringVar(cfg.Flags.ChangeFileName, "n", "", "")
}

func defineSkipFlags(cfg *Config, fs *flag.FlagSet) {
fs.BoolVar(&cfg.Flags.SkipVersionLinks, "skip-version-links", false, "")
fs.BoolVar(&cfg.Flags.SkipVersionLinks, "s", false, "")
func defineSkipFlags(cfg *Config, flagset *flag.FlagSet) {
flagset.BoolVar(&cfg.Flags.SkipVersionLinks, "skip-version-links", false, "")
flagset.BoolVar(&cfg.Flags.SkipVersionLinks, "s", false, "")
}
108 changes: 108 additions & 0 deletions internal/pkg/conflictless/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,114 @@ import (
"github.com/ypjama/conflictless-keepachangelog/internal/pkg/conflictless"
)

type parseCliFlagsTestCase struct {
description string
argsAfterMain []string
expectedFlags conflictless.FlagCollection
}

//nolint:funlen
func TestParseCLIFlags(t *testing.T) {
t.Parallel()

originalArgs := os.Args

dir := "directory-for-cli-parse-test"
bump := "major"
changelog := "changelog-for-cli-parse-test.md"
format := "json"
types := "added,changed,removed"
name := "foo-bar-baz"

for _, testCase := range []parseCliFlagsTestCase{
{
"check",
[]string{"check", "--dir", dir},
conflictless.FlagCollection{
Bump: nil,
ChangeFileFormat: nil,
ChangeFileName: nil,
ChangelogFile: nil,
ChangeTypesCsv: nil,
Command: "check",
Directory: &dir,
SkipVersionLinks: false,
},
},
{
"create",
[]string{"create", "-d", dir, "-f", format, "-t", types, "-n", name},
conflictless.FlagCollection{
Bump: nil,
ChangeFileFormat: &format,
ChangeFileName: &name,
ChangelogFile: nil,
ChangeTypesCsv: &types,
Command: "create",
Directory: &dir,
SkipVersionLinks: false,
},
},
{
"generate",
[]string{"generate", "-c", changelog, "-s", "-d", dir, "-b", bump},
conflictless.FlagCollection{
Bump: &bump,
ChangeFileFormat: nil,
ChangeFileName: nil,
ChangelogFile: &changelog,
ChangeTypesCsv: nil,
Command: "generate",
Directory: &dir,
SkipVersionLinks: true,
},
},
{
"help",
[]string{"help"},
conflictless.FlagCollection{
Bump: nil,
ChangeFileFormat: nil,
ChangeFileName: nil,
ChangelogFile: nil,
ChangeTypesCsv: nil,
Command: "help",
Directory: nil,
SkipVersionLinks: false,
},
},
{
"preview",
[]string{"preview", "--changelog", changelog, "--skip-version-links", "--dir", dir, "--bump", bump},
conflictless.FlagCollection{
Bump: &bump,
ChangeFileFormat: nil,
ChangeFileName: nil,
ChangelogFile: &changelog,
ChangeTypesCsv: nil,
Command: "preview",
Directory: &dir,
SkipVersionLinks: true,
},
},
} {
t.Run(testCase.description, func(t *testing.T) {
t.Parallel()

args := []string{"conflictless"}
args = append(args, testCase.argsAfterMain...)
os.Args = args

cfg := new(conflictless.Config)
conflictless.ParseCLIFlags(cfg)

os.Args = originalArgs

assert.EqualValues(t, testCase.expectedFlags, cfg.Flags)
})
}
}

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

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

// Export functions for tests

//nolint:gochecknoglobals
var ParseCLIFlags = parseCLIFlags

0 comments on commit 4af0e48

Please sign in to comment.