-
Notifications
You must be signed in to change notification settings - Fork 21
Fix Add migration and import handling #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package migrations_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| semver "github.com/Masterminds/semver/v3" | ||
| "github.com/gofiber/cli/cmd/internal/migrations" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_MigrateGoPkgs(t *testing.T) { | ||
| dir, err := os.MkdirTemp("", "mgpkgs") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| mainContent := `package main | ||
| import ( | ||
| fiber "github.com/gofiber/fiber/v2" | ||
| "github.com/gofiber/fiber/v2/middleware/adaptor" | ||
| ) | ||
| func main() { | ||
| _, _ = fiber.New(), adaptor.New() | ||
| }` | ||
| file := filepath.Join(dir, "main.go") | ||
| require.NoError(t, os.WriteFile(file, []byte(mainContent), 0o600)) | ||
|
|
||
| modContent := `module example | ||
|
|
||
| go 1.22 | ||
|
|
||
| require github.com/gofiber/fiber/v2 v2.0.0` | ||
| require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(modContent), 0o600)) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| target := semver.MustParse("3.0.0") | ||
| require.NoError(t, migrations.MigrateGoPkgs(cmd, dir, nil, target)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "github.com/gofiber/fiber/v3") | ||
| assert.NotContains(t, content, "github.com/gofiber/fiber/v2") | ||
|
|
||
| mod := readFile(t, filepath.Join(dir, "go.mod")) | ||
| assert.Contains(t, mod, "github.com/gofiber/fiber/v3 v3.0.0") | ||
| assert.Contains(t, buf.String(), "Migrating Go packages") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -359,13 +359,60 @@ func MigrateMount(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { | |
| // MigrateAddMethod adapts the Add method signature | ||
| func MigrateAddMethod(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { | ||
| err := internal.ChangeFileContent(cwd, func(content string) string { | ||
| return replaceCall(content, ".Add", func(call string, args []string) string { | ||
| if len(args) < 2 { | ||
| return call | ||
| re := regexp.MustCompile(`\.Add\(`) | ||
| matches := re.FindAllStringIndex(content, -1) | ||
| if len(matches) == 0 { | ||
| return content | ||
| } | ||
|
|
||
| var b strings.Builder | ||
| last := 0 | ||
| for _, m := range matches { | ||
| if m[0] < last { | ||
| continue | ||
| } | ||
| args[0] = fmt.Sprintf("[]string{%s}", args[0]) | ||
| return fmt.Sprintf(".Add(%s)", strings.Join(args, ", ")) | ||
| }) | ||
|
|
||
| startCall := m[0] | ||
| if startCall > 0 { | ||
| if _, err := b.WriteString(content[last:startCall]); err != nil { | ||
| return content | ||
| } | ||
| } | ||
|
|
||
| end, inner := extractCall(content, m[1]) | ||
| identStart := startCall - 1 | ||
| for identStart >= 0 { | ||
| ch := content[identStart] | ||
| if !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_') { | ||
| break | ||
| } | ||
| identStart-- | ||
| } | ||
| ident := content[identStart+1 : startCall] | ||
|
|
||
| switch ident { | ||
| case "Header", "httpServerActiveRequests": | ||
| if _, err := b.WriteString(content[startCall:end]); err != nil { | ||
| return content | ||
| } | ||
| default: | ||
| args := splitArgs(inner) | ||
| if len(args) >= 2 { | ||
| args[0] = fmt.Sprintf("[]string{%s}", args[0]) | ||
| } | ||
| if _, err := b.WriteString(".Add(" + strings.Join(args, ", ") + ")"); err != nil { | ||
| return content | ||
| } | ||
| } | ||
|
|
||
| last = end | ||
| } | ||
|
|
||
| if _, err := b.WriteString(content[last:]); err != nil { | ||
| return content | ||
| } | ||
|
|
||
| return b.String() | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to migrate Add method calls: %w", err) | ||
|
|
@@ -414,11 +461,13 @@ func MigrateCORSConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) err | |
|
|
||
| // MigrateCSRFConfig updates csrf middleware configuration fields | ||
| func MigrateCSRFConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { | ||
| replacer := strings.NewReplacer("Expiration:", "IdleTimeout:") | ||
| reConfig := regexp.MustCompile(`csrf\.Config{[^}]*}`) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regular expression For example, consider this code: var _ = csrf.New(csrf.Config{
Expiration: 10 * time.Minute,
Next: func(c *fiber.Ctx) bool {
if c.Path() == "/login" {
return true
}
return false
},
})The regex will only match up to the A more robust approach would be to parse the struct literal by matching braces, similar to how |
||
| reSession := regexp.MustCompile(`\s*SessionKey:\s*[^,]+,?\n`) | ||
| reKeyLookup := regexp.MustCompile(`(\s*)KeyLookup:\s*([^,\n]+)(,?)(\n?)`) | ||
| err := internal.ChangeFileContent(cwd, func(content string) string { | ||
| content = replacer.Replace(content) | ||
| content = reConfig.ReplaceAllStringFunc(content, func(s string) string { | ||
| return strings.ReplaceAll(s, "Expiration:", "IdleTimeout:") | ||
| }) | ||
| content = reSession.ReplaceAllString(content, "") | ||
|
|
||
| content = reKeyLookup.ReplaceAllStringFunc(content, func(s string) string { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Harden go.mod regex: allow tabs/any whitespace and +incompatible/build metadata.
( *?)won’t catch tabs, andv[\w.-]+misses+(e.g.,v3.0.0+incompatible). Widen the character class and whitespace to ensure robust replacement across real-world go.mod lines.Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents