From 633ca4e32a9c26dc4c37d03c4f1a136f5c7ff5a1 Mon Sep 17 00:00:00 2001 From: RW Date: Sat, 23 Aug 2025 17:05:05 +0200 Subject: [PATCH 1/7] chore: require lint and tests --- AGENTS.md | 3 ++ cmd/internal/migrations/lists.go | 4 +-- cmd/internal/migrations/lists_test.go | 38 +++++++++++++++++++++++ cmd/migrate.go | 17 +++++++---- cmd/migrate_test.go | 43 +++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 AGENTS.md create mode 100644 cmd/internal/migrations/lists_test.go diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..c4b28d8 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,3 @@ +# Repository Guidelines +- Always run `go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./...` +- Always run `go run gotest.tools/gotestsum@latest -f testname -- ./... -race -count=1 -shuffle=on` diff --git a/cmd/internal/migrations/lists.go b/cmd/internal/migrations/lists.go index c63ec26..c71f5b5 100644 --- a/cmd/internal/migrations/lists.go +++ b/cmd/internal/migrations/lists.go @@ -73,7 +73,7 @@ var Migrations = []Migration{ // DoMigration runs all migrations // It will run all migrations that match the current and target version -func DoMigration(cmd *cobra.Command, cwd string, curr, target *semver.Version, skipGoMod bool) error { +func DoMigration(cmd *cobra.Command, cwd string, curr, target *semver.Version, skipGoMod bool, verbose bool) error { for _, m := range Migrations { toC, err := semver.NewConstraint(m.To) if err != nil { @@ -90,7 +90,7 @@ func DoMigration(cmd *cobra.Command, cwd string, curr, target *semver.Version, s return err } } - } else { + } else if verbose { cmd.Printf("Skipping migration from %s to %s\n", m.From, m.To) } } diff --git a/cmd/internal/migrations/lists_test.go b/cmd/internal/migrations/lists_test.go new file mode 100644 index 0000000..ccd3ceb --- /dev/null +++ b/cmd/internal/migrations/lists_test.go @@ -0,0 +1,38 @@ +package migrations_test + +import ( + "bytes" + "testing" + + semver "github.com/Masterminds/semver/v3" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/gofiber/cli/cmd/internal/migrations" +) + +func Test_DoMigration_Verbose(t *testing.T) { + t.Parallel() + dir := t.TempDir() + curr := semver.MustParse("0.0.0") + target := semver.MustParse("0.1.0") + + t.Run("silent", func(t *testing.T) { + var buf bytes.Buffer + cmd := &cobra.Command{} + cmd.SetOut(&buf) + require.NoError(t, migrations.DoMigration(cmd, dir, curr, target, true, false)) + assert.Equal(t, "", buf.String()) + }) + + t.Run("verbose", func(t *testing.T) { + var buf bytes.Buffer + cmd := &cobra.Command{} + cmd.SetOut(&buf) + require.NoError(t, migrations.DoMigration(cmd, dir, curr, target, true, true)) + out := buf.String() + assert.Contains(t, out, "Skipping migration from >=1.0.0 to >=0.0.0-0") + assert.Contains(t, out, "Skipping migration from >=2.0.0 to <4.0.0-0") + }) +} diff --git a/cmd/migrate.go b/cmd/migrate.go index f5485a2..34b07f3 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -16,6 +16,7 @@ func newMigrateCmd() *cobra.Command { var targetVersionS string var force bool var skipGoMod bool + var verbose bool cmd := &cobra.Command{ Use: "migrate", @@ -27,12 +28,10 @@ func newMigrateCmd() *cobra.Command { latestFiberVersion = "" } - cmd.Flags().StringVarP(&targetVersionS, "to", "t", "", "Migrate to a specific version e.g:"+latestFiberVersion+" Format: X.Y.Z") - if err := cmd.MarkFlagRequired("to"); err != nil { - panic(err) - } + cmd.Flags().StringVarP(&targetVersionS, "to", "t", "", "Migrate to a specific version. Default: latest ("+latestFiberVersion+")") cmd.Flags().BoolVarP(&force, "force", "f", false, "Force migration even if already on version") cmd.Flags().BoolVarP(&skipGoMod, "skip_go_mod", "s", false, "Skip running go mod tidy, download and vendor") + cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output") cmd.RunE = func(cmd *cobra.Command, _ []string) error { return migrateRunE(cmd, MigrateOptions{ @@ -40,6 +39,7 @@ func newMigrateCmd() *cobra.Command { TargetVersionS: targetVersionS, Force: force, SkipGoMod: skipGoMod, + Verbose: verbose, }) } @@ -53,6 +53,7 @@ type MigrateOptions struct { TargetVersionS string Force bool SkipGoMod bool + Verbose bool } func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error { @@ -63,6 +64,12 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error { currentVersionS = strings.TrimPrefix(currentVersionS, "v") currentVersion := semver.MustParse(currentVersionS) + if opts.TargetVersionS == "" { + opts.TargetVersionS, err = LatestFiberVersion() + if err != nil { + return fmt.Errorf("failed to determine latest fiber version: %w", err) + } + } opts.TargetVersionS = strings.TrimPrefix(opts.TargetVersionS, "v") targetVersion, err := semver.NewVersion(opts.TargetVersionS) if err != nil { @@ -89,7 +96,7 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error { migrateFromS = migrateFrom.String() } - err = migrations.DoMigration(cmd, wd, migrateFrom, targetVersion, opts.SkipGoMod) + err = migrations.DoMigration(cmd, wd, migrateFrom, targetVersion, opts.SkipGoMod, opts.Verbose) if err != nil { return fmt.Errorf("migration failed %w", err) } diff --git a/cmd/migrate_test.go b/cmd/migrate_test.go index 808b711..d58f8f2 100644 --- a/cmd/migrate_test.go +++ b/cmd/migrate_test.go @@ -1,11 +1,13 @@ package cmd import ( + "net/http" "os" "os/exec" "path/filepath" "testing" + "github.com/jarcoal/httpmock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -111,6 +113,47 @@ func main() { assert.Equal(t, content, content2) } +func Test_Migrate_DefaultTarget(t *testing.T) { + dir, err := os.MkdirTemp("", "migrate_default") + require.NoError(t, err) + defer func() { require.NoError(t, os.RemoveAll(dir)) }() + + gomod := `module example.com/demo + +go 1.20 + +require github.com/gofiber/fiber/v2 v2.0.6 +` + require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(gomod), 0o600)) + + main := `package main +import "github.com/gofiber/fiber/v2" +func main() { + _ = fiber.New() +}` + require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte(main), 0o600)) + + cwd, err := os.Getwd() + require.NoError(t, err) + require.NoError(t, os.Chdir(dir)) + defer func() { require.NoError(t, os.Chdir(cwd)) }() + + setupCmd() + defer teardownCmd() + + httpmock.Activate() + defer httpmock.DeactivateAndReset() + httpmock.RegisterResponder(http.MethodGet, "https://api.github.com/repos/gofiber/fiber/releases/latest", httpmock.NewBytesResponder(200, []byte(`{"name":"v3.0.0"}`))) + + cmd := newMigrateCmd() + out, err := runCobraCmd(cmd) + require.NoError(t, err) + assert.Contains(t, out, "Migration from Fiber 2.0.6 to 3.0.0") + + content := readFileTB(t, filepath.Join(dir, "go.mod")) + assert.Contains(t, content, "github.com/gofiber/fiber/v3 v3.0.0") +} + func Test_RunGoMod(t *testing.T) { dir := t.TempDir() From 71d1e29474628de1f90fad7c7b16e14eb4902994 Mon Sep 17 00:00:00 2001 From: RW Date: Sat, 23 Aug 2025 17:25:30 +0200 Subject: [PATCH 2/7] chore: fix lint config and docs --- AGENTS.md | 1 + Makefile | 8 +++++--- cmd/internal/migrations/lists.go | 2 +- cmd/internal/migrations/lists_test.go | 4 ++++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index c4b28d8..1a3c090 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,3 +1,4 @@ # Repository Guidelines + - Always run `go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./...` - Always run `go run gotest.tools/gotestsum@latest -f testname -- ./... -race -count=1 -shuffle=on` diff --git a/Makefile b/Makefile index cdf459d..586893f 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,10 @@ +TOOLCHAIN ?= local + ## help: 💡 Display available commands .PHONY: help help: - @echo '⚡️ GoFiber/Cli Development:' - @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' + @echo '⚡️ GoFiber/Cli Development:' + @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' ## audit: 🚀 Conduct quality checks .PHONY: audit @@ -37,7 +39,7 @@ markdown: ## lint: 🚨 Run lint checks .PHONY: lint lint: - go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./... + GOTOOLCHAIN=$(TOOLCHAIN) go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./... ## test: 🚦 Execute all tests .PHONY: test diff --git a/cmd/internal/migrations/lists.go b/cmd/internal/migrations/lists.go index c71f5b5..3076c45 100644 --- a/cmd/internal/migrations/lists.go +++ b/cmd/internal/migrations/lists.go @@ -73,7 +73,7 @@ var Migrations = []Migration{ // DoMigration runs all migrations // It will run all migrations that match the current and target version -func DoMigration(cmd *cobra.Command, cwd string, curr, target *semver.Version, skipGoMod bool, verbose bool) error { +func DoMigration(cmd *cobra.Command, cwd string, curr, target *semver.Version, skipGoMod, verbose bool) error { for _, m := range Migrations { toC, err := semver.NewConstraint(m.To) if err != nil { diff --git a/cmd/internal/migrations/lists_test.go b/cmd/internal/migrations/lists_test.go index ccd3ceb..d0b00a1 100644 --- a/cmd/internal/migrations/lists_test.go +++ b/cmd/internal/migrations/lists_test.go @@ -19,6 +19,8 @@ func Test_DoMigration_Verbose(t *testing.T) { target := semver.MustParse("0.1.0") t.Run("silent", func(t *testing.T) { + t.Parallel() + var buf bytes.Buffer cmd := &cobra.Command{} cmd.SetOut(&buf) @@ -27,6 +29,8 @@ func Test_DoMigration_Verbose(t *testing.T) { }) t.Run("verbose", func(t *testing.T) { + t.Parallel() + var buf bytes.Buffer cmd := &cobra.Command{} cmd.SetOut(&buf) From 150d5698c79cc4397d64c80e491209913b41dc6e Mon Sep 17 00:00:00 2001 From: RW Date: Sat, 23 Aug 2025 18:00:12 +0200 Subject: [PATCH 3/7] fix: avoid network during migrate init --- cmd/migrate.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cmd/migrate.go b/cmd/migrate.go index 34b07f3..3a96e56 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -23,12 +23,7 @@ func newMigrateCmd() *cobra.Command { Short: "Migrate Fiber project version to a newer version", } - latestFiberVersion, err := LatestFiberVersion() - if err != nil { - latestFiberVersion = "" - } - - cmd.Flags().StringVarP(&targetVersionS, "to", "t", "", "Migrate to a specific version. Default: latest ("+latestFiberVersion+")") + cmd.Flags().StringVarP(&targetVersionS, "to", "t", "", "Migrate to a specific version. Default: latest") cmd.Flags().BoolVarP(&force, "force", "f", false, "Force migration even if already on version") cmd.Flags().BoolVarP(&skipGoMod, "skip_go_mod", "s", false, "Skip running go mod tidy, download and vendor") cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output") From 4e006cf77ee10ade758bb9aef0807c87404feb7e Mon Sep 17 00:00:00 2001 From: RW Date: Sat, 23 Aug 2025 18:00:26 +0200 Subject: [PATCH 4/7] fix: use tabs in Makefile commands --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 586893f..053ecfd 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ TOOLCHAIN ?= local ## help: 💡 Display available commands .PHONY: help help: - @echo '⚡️ GoFiber/Cli Development:' - @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' + @echo '⚡️ GoFiber/Cli Development:' + @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' ## audit: 🚀 Conduct quality checks .PHONY: audit @@ -39,7 +39,7 @@ markdown: ## lint: 🚨 Run lint checks .PHONY: lint lint: - GOTOOLCHAIN=$(TOOLCHAIN) go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./... + GOTOOLCHAIN=$(TOOLCHAIN) go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./... ## test: 🚦 Execute all tests .PHONY: test From 11ae4cf4dd06e678a76708e79d4eda3297853cb3 Mon Sep 17 00:00:00 2001 From: RW Date: Sat, 23 Aug 2025 18:27:38 +0200 Subject: [PATCH 5/7] docs: recommend Makefile commands --- AGENTS.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 1a3c090..1466a9f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,4 +1,5 @@ # Repository Guidelines -- Always run `go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./...` -- Always run `go run gotest.tools/gotestsum@latest -f testname -- ./... -race -count=1 -shuffle=on` +- Use the Makefile commands for common tasks (e.g., `make lint`, `make test`) +- Always run `TOOLCHAIN=go1.25.0 make lint` +- Always run `make test` From 55d99ede52bd7e7cd6662d537c7340a83d48438f Mon Sep 17 00:00:00 2001 From: RW Date: Sat, 23 Aug 2025 18:27:43 +0200 Subject: [PATCH 6/7] chore: default to go1.25.0 --- AGENTS.md | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 1466a9f..17ce774 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,5 +1,5 @@ # Repository Guidelines - Use the Makefile commands for common tasks (e.g., `make lint`, `make test`) -- Always run `TOOLCHAIN=go1.25.0 make lint` +- Always run `make lint` - Always run `make test` diff --git a/Makefile b/Makefile index 053ecfd..ad57fea 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -TOOLCHAIN ?= local +TOOLCHAIN ?= go1.25.0 ## help: 💡 Display available commands .PHONY: help From 4d010cd0d303256a5420dcc10df08fc1a9dda0d7 Mon Sep 17 00:00:00 2001 From: RW Date: Sat, 23 Aug 2025 18:47:22 +0200 Subject: [PATCH 7/7] fix: ignore test flags when running commands --- cmd/tester_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/tester_test.go b/cmd/tester_test.go index 98e8d6f..140c1e0 100644 --- a/cmd/tester_test.go +++ b/cmd/tester_test.go @@ -121,7 +121,11 @@ func runCobraCmd(cmd *cobra.Command, args ...string) (string, error) { parent.SilenceErrors = origSilence }() } else { - cmd.SetArgs(args) + if len(args) == 0 { + cmd.SetArgs([]string{}) + } else { + cmd.SetArgs(args) + } } err := cmd.Execute()