From 4af0e48a8db2b2e87463bf04f13aef34a1d46037 Mon Sep 17 00:00:00 2001 From: ypjama <3822534+ypjama@users.noreply.github.com> Date: Sun, 6 Oct 2024 13:04:10 +0300 Subject: [PATCH] Add tests --- internal/pkg/conflictless/cli.go | 66 +++++++++----- internal/pkg/conflictless/cli_test.go | 108 +++++++++++++++++++++++ internal/pkg/conflictless/export_test.go | 6 ++ 3 files changed, 159 insertions(+), 21 deletions(-) create mode 100644 internal/pkg/conflictless/export_test.go diff --git a/internal/pkg/conflictless/cli.go b/internal/pkg/conflictless/cli.go index 7a2f5a8..d873d32 100644 --- a/internal/pkg/conflictless/cli.go +++ b/internal/pkg/conflictless/cli.go @@ -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, "") } diff --git a/internal/pkg/conflictless/cli_test.go b/internal/pkg/conflictless/cli_test.go index e61e788..4512a72 100644 --- a/internal/pkg/conflictless/cli_test.go +++ b/internal/pkg/conflictless/cli_test.go @@ -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() diff --git a/internal/pkg/conflictless/export_test.go b/internal/pkg/conflictless/export_test.go new file mode 100644 index 0000000..c1cc051 --- /dev/null +++ b/internal/pkg/conflictless/export_test.go @@ -0,0 +1,6 @@ +package conflictless + +// Export functions for tests + +//nolint:gochecknoglobals +var ParseCLIFlags = parseCLIFlags