Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/internal/migrations/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var Migrations = []Migration{
v3migrations.MigrateMount,
v3migrations.MigrateConfigListenerFields,
v3migrations.MigrateListenerCallbacks,
v3migrations.MigrateShutdownHook,
v3migrations.MigrateListenMethods,
v3migrations.MigrateContextMethods,
v3migrations.MigrateCORSConfig,
Expand Down
20 changes: 20 additions & 0 deletions cmd/internal/migrations/v3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,26 @@ func MigrateListenerCallbacks(cmd *cobra.Command, cwd string, _, _ *semver.Versi
return nil
}

// MigrateShutdownHook updates the deprecated OnShutdown hook to OnPostShutdown
// and adapts inline hook functions to accept the error parameter.
func MigrateShutdownHook(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
err := internal.ChangeFileContent(cwd, func(content string) string {
reName := regexp.MustCompile(`\.Hooks\(\)\.OnShutdown\(`)
content = reName.ReplaceAllString(content, ".Hooks().OnPostShutdown(")

reInline := regexp.MustCompile(`\.OnPostShutdown\(func\(\s*\)`)
content = reInline.ReplaceAllString(content, ".OnPostShutdown(func(err error)")
Comment thread
ReneWerner87 marked this conversation as resolved.
Comment thread
ReneWerner87 marked this conversation as resolved.

return content
})
if err != nil {
return fmt.Errorf("failed to migrate shutdown hooks: %w", err)
}

cmd.Println("Migrating shutdown hooks")
return nil
}

// MigrateFilesystemMiddleware replaces deprecated filesystem middleware with static middleware
func MigrateFilesystemMiddleware(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
err := internal.ChangeFileContent(cwd, func(content string) string {
Expand Down
23 changes: 23 additions & 0 deletions cmd/internal/migrations/v3/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,26 @@ var _ = basicauth.New(basicauth.Config{
assert.Contains(t, content, `Authorizer: func(u, p string, _ fiber.Ctx) bool`)
assert.Contains(t, buf.String(), "Migrating basicauth authorizer")
}

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

dir, err := os.MkdirTemp("", "mhooks")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()

file := writeTempFile(t, dir, `package main
import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()
app.Hooks().OnShutdown(func() error { return nil })
}`)

var buf bytes.Buffer
cmd := newCmd(&buf)
require.NoError(t, v3.MigrateShutdownHook(cmd, dir, nil, nil))

content := readFile(t, file)
assert.Contains(t, content, `.Hooks().OnPostShutdown(func(err error) error {`)
assert.Contains(t, buf.String(), "Migrating shutdown hooks")
}
Comment on lines +831 to +852
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider converting this test to a table-driven test to improve robustness and ensure various formatting styles are handled correctly. This would make it easier to add more scenarios, such as different whitespace arrangements or function references instead of inline functions.

Loading