Skip to content

Commit 866ceae

Browse files
authored
Merge pull request #239 from gofiber/codex/2025-11-30-18-06-28
2 parents 922957d + 77a9e2b commit 866ceae

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

cmd/internal/migrations/v3/context_methods.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import (
1919
func MigrateContextMethods(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
2020
changed, err := internal.ChangeFileContent(cwd, func(content string) string {
2121
orig := content
22+
const migrationMarker = "// fiber:context-methods migrated"
23+
if strings.Contains(orig, migrationMarker) {
24+
return content
25+
}
2226

2327
// old Context() returned fasthttp.RequestCtx
2428
if !strings.Contains(orig, ".SetContext(") {
@@ -78,6 +82,14 @@ func MigrateContextMethods(cmd *cobra.Command, cwd string, _, _ *semver.Version)
7882
return match
7983
})
8084

85+
if content != orig && !strings.Contains(content, migrationMarker) {
86+
if strings.HasSuffix(content, "\n") {
87+
content += migrationMarker + "\n"
88+
} else {
89+
content += "\n" + migrationMarker + "\n"
90+
}
91+
}
92+
8193
return content
8294
})
8395
if err != nil {

cmd/internal/migrations/v3/context_methods_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,37 @@ func handler(c fiber.Ctx) error {
131131
assert.Equal(t, 2, strings.Count(second, "SetContext("))
132132
}
133133

134+
func Test_MigrateContextMethods_UserContextOnlyMultipleRuns(t *testing.T) {
135+
t.Parallel()
136+
137+
dir, err := os.MkdirTemp("", "mcmtestuserctx")
138+
require.NoError(t, err)
139+
defer func() { require.NoError(t, os.RemoveAll(dir)) }()
140+
141+
file := writeTempFile(t, dir, `package main
142+
import "github.com/gofiber/fiber/v2"
143+
func handler(c fiber.Ctx) error {
144+
ctx := c.UserContext()
145+
_ = ctx
146+
return nil
147+
}`)
148+
149+
var buf bytes.Buffer
150+
cmd := newCmd(&buf)
151+
require.NoError(t, v3.MigrateContextMethods(cmd, dir, nil, nil))
152+
153+
first := readFile(t, file)
154+
require.Contains(t, first, "ctx := c.Context()")
155+
require.NotContains(t, first, ".RequestCtx()")
156+
require.Contains(t, first, "// fiber:context-methods migrated")
157+
158+
require.NoError(t, v3.MigrateContextMethods(cmd, dir, nil, nil))
159+
second := readFile(t, file)
160+
assert.Equal(t, first, second)
161+
assert.Equal(t, 1, strings.Count(second, "ctx := c.Context()"))
162+
assert.Equal(t, 0, strings.Count(second, ".RequestCtx()"))
163+
}
164+
134165
func Test_MigrateContextMethods_SkipNonFiber(t *testing.T) {
135166
t.Parallel()
136167

0 commit comments

Comments
 (0)