Skip to content

Commit 26ff457

Browse files
authored
Merge pull request #234 from gofiber/claude/migrate-static-middleware-01REo5PC9G8oEaRfwGrLPR8a
2 parents 50fd2fc + 51bdb84 commit 26ff457

File tree

2 files changed

+97
-8
lines changed

2 files changed

+97
-8
lines changed

cmd/internal/migrations/v3/filesystem_middleware.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import (
1111
"github.com/gofiber/cli/cmd/internal"
1212
)
1313

14+
var (
15+
reFilesystemNew = regexp.MustCompile(`filesystem\.New\s*\(`)
16+
reFilesystemRootHTTP = regexp.MustCompile(`Root:\s*http.Dir\(([^)]+)\)`)
17+
reFilesystemRoot = regexp.MustCompile(`Root:\s*([^,\n]+)`)
18+
reFilesystemIndex = regexp.MustCompile(`Index:\s*([^,\n]+)`)
19+
reFilesystemNotFoundFile = regexp.MustCompile(`(?m)^(\s*)(NotFoundFile:\s*[^,\n]+)(,?)`)
20+
)
21+
1422
func MigrateFilesystemMiddleware(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
1523
changed, err := internal.ChangeFileContent(cwd, func(content string) string {
1624
content = strings.ReplaceAll(content,
@@ -20,19 +28,23 @@ func MigrateFilesystemMiddleware(cmd *cobra.Command, cwd string, _, _ *semver.Ve
2028
"github.com/gofiber/fiber/v3/middleware/filesystem",
2129
"github.com/gofiber/fiber/v3/middleware/static")
2230

23-
reNew := regexp.MustCompile(`filesystem\.New\s*\(`)
24-
content = reNew.ReplaceAllString(content, `static.New("", `)
31+
content = reFilesystemNew.ReplaceAllString(content, `static.New("", `)
2532

2633
content = strings.ReplaceAll(content, "filesystem.Config{", "static.Config{")
2734

28-
reRootHTTP := regexp.MustCompile(`Root:\s*http.Dir\(([^)]+)\)`)
29-
content = reRootHTTP.ReplaceAllString(content, `FS: os.DirFS($1)`)
35+
content = reFilesystemRootHTTP.ReplaceAllString(content, `FS: os.DirFS($1)`)
36+
37+
content = reFilesystemRoot.ReplaceAllString(content, `FS: os.DirFS($1)`)
3038

31-
reRoot := regexp.MustCompile(`Root:\s*([^,\n]+)`)
32-
content = reRoot.ReplaceAllString(content, `FS: os.DirFS($1)`)
39+
content = reFilesystemIndex.ReplaceAllString(content, `IndexNames: []string{$1}`)
3340

34-
reIndex := regexp.MustCompile(`Index:\s*([^,\n]+)`)
35-
content = reIndex.ReplaceAllString(content, `IndexNames: []string{$1}`)
41+
// Handle NotFoundFile migration - comment it out and add TODO for NotFoundHandler
42+
// Only migrate if not already migrated (check for TODO comment)
43+
if !strings.Contains(content, "TODO: Migrate to NotFoundHandler") {
44+
content = reFilesystemNotFoundFile.ReplaceAllString(content,
45+
`$1// TODO: Migrate to NotFoundHandler (fiber.Handler) - NotFoundFile is deprecated
46+
$1// $2$3`)
47+
}
3648

3749
return content
3850
})

cmd/internal/migrations/v3/filesystem_middleware_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v3_test
33
import (
44
"bytes"
55
"os"
6+
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
@@ -40,3 +41,79 @@ func main() {
4041
assert.Contains(t, content, `IndexNames: []string{"index.html"}`)
4142
assert.Contains(t, buf.String(), "Migrating filesystem middleware")
4243
}
44+
45+
func Test_MigrateFilesystemMiddleware_WithNotFoundFile(t *testing.T) {
46+
t.Parallel()
47+
48+
dir, err := os.MkdirTemp("", "mfs-notfound")
49+
require.NoError(t, err)
50+
defer func() { require.NoError(t, os.RemoveAll(dir)) }()
51+
52+
file := writeTempFile(t, dir, `package main
53+
import (
54+
"github.com/gofiber/fiber/v2/middleware/filesystem"
55+
"net/http"
56+
)
57+
func main() {
58+
app.All("/*", filesystem.New(filesystem.Config{
59+
Root: http.Dir("./dist"),
60+
NotFoundFile: "index.html",
61+
Index: "index.html",
62+
}))
63+
}`)
64+
65+
var buf bytes.Buffer
66+
cmd := newCmd(&buf)
67+
require.NoError(t, v3.MigrateFilesystemMiddleware(cmd, dir, nil, nil))
68+
69+
content := readFile(t, file)
70+
// Check that NotFoundFile is commented out
71+
assert.Contains(t, content, "// NotFoundFile: \"index.html\"")
72+
// Check that TODO comment is added
73+
assert.Contains(t, content, "// TODO: Migrate to NotFoundHandler (fiber.Handler) - NotFoundFile is deprecated")
74+
// Check that static migration happened
75+
assert.Contains(t, content, `static.New("", static.Config{`)
76+
assert.Contains(t, content, `FS: os.DirFS("./dist")`)
77+
assert.Contains(t, content, `IndexNames: []string{"index.html"}`)
78+
}
79+
80+
func Test_MigrateFilesystemMiddleware_NotFoundFileIdempotent(t *testing.T) {
81+
t.Parallel()
82+
83+
dir, err := os.MkdirTemp("", "mfs-idempotent")
84+
require.NoError(t, err)
85+
defer func() { require.NoError(t, os.RemoveAll(dir)) }()
86+
87+
file := writeTempFile(t, dir, `package main
88+
import (
89+
"github.com/gofiber/fiber/v2/middleware/filesystem"
90+
"net/http"
91+
)
92+
func main() {
93+
app.All("/*", filesystem.New(filesystem.Config{
94+
Root: http.Dir("./dist"),
95+
NotFoundFile: "index.html",
96+
Index: "index.html",
97+
}))
98+
}`)
99+
100+
var buf bytes.Buffer
101+
cmd := newCmd(&buf)
102+
103+
// First migration
104+
require.NoError(t, v3.MigrateFilesystemMiddleware(cmd, dir, nil, nil))
105+
firstContent := readFile(t, file)
106+
107+
// Second migration - should be idempotent
108+
buf.Reset()
109+
require.NoError(t, v3.MigrateFilesystemMiddleware(cmd, dir, nil, nil))
110+
secondContent := readFile(t, file)
111+
112+
// Content should be exactly the same after second migration
113+
assert.Equal(t, firstContent, secondContent, "Migration should be idempotent")
114+
115+
// Verify the TODO comment is only present once
116+
assert.Equal(t, 1, strings.Count(secondContent, "TODO: Migrate to NotFoundHandler"))
117+
// Verify the NotFoundFile comment is only present once
118+
assert.Equal(t, 1, strings.Count(secondContent, "// NotFoundFile:"))
119+
}

0 commit comments

Comments
 (0)