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 @@ -60,6 +60,7 @@ var Migrations = []Migration{
v3migrations.MigrateTimeoutConfig,
v3migrations.MigrateBasicauthAuthorizer,
v3migrations.MigrateBasicauthConfig,
v3migrations.MigrateBasicauthStorePassword,
v3migrations.MigrateReqHeaderParser,
MigrateGoVersion("1.24"),
},
Expand Down
15 changes: 15 additions & 0 deletions cmd/internal/migrations/v3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,21 @@ func MigrateBasicauthConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version
return nil
}

// MigrateBasicauthStorePassword comments usages of the removed StorePassword option
// in basicauth middleware configuration.
func MigrateBasicauthStorePassword(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
re := regexp.MustCompile(`(\s*)StorePassword:\s*([^,\n]+)(,?)`)
err := internal.ChangeFileContent(cwd, func(content string) string {
return re.ReplaceAllString(content, `$1// TODO: StorePassword removed ($2)$3`)
})
if err != nil {
return fmt.Errorf("failed to migrate basicauth StorePassword: %w", err)
}

cmd.Println("Migrating basicauth StorePassword option")
return nil
}

// MigrateCacheConfig updates cache middleware configuration fields
func MigrateCacheConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
err := internal.ChangeFileContent(cwd, func(content string) string {
Expand Down
26 changes: 26 additions & 0 deletions cmd/internal/migrations/v3/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,32 @@ var _ = basicauth.New(basicauth.Config{
assert.Contains(t, buf.String(), "Migrating basicauth configs")
}

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

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

file := writeTempFile(t, dir, `package main
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/basicauth"
)
var _ = basicauth.New(basicauth.Config{
StorePassword: true,
})`)

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

content := readFile(t, file)
assert.NotContains(t, content, "StorePassword:")
assert.Contains(t, content, "// TODO: StorePassword removed (true)")
assert.Contains(t, buf.String(), "Migrating basicauth StorePassword option")
}
Comment on lines +1034 to +1058
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

The current test covers a single scenario. To improve test coverage and ensure the migration is robust, I recommend converting this to a table-driven test. This would make it easy to check various scenarios, such as when StorePassword is the last item in the config (no trailing comma) versus when it's followed by other fields (with a trailing comma).

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

	testCases := []struct {
		name  string
		input string
		want  string
	}{
		{
			name: "without trailing comma",
			input: `package main\nimport (\n    "github.com/gofiber/fiber/v2"\n    "github.com/gofiber/fiber/v2/middleware/basicauth"\n)\nvar _ = basicauth.New(basicauth.Config{\n    StorePassword: true,\n})`,
			want: `// TODO: StorePassword removed (true)`,
		},
		{
			name: "with trailing comma",
			input: `package main\nimport (\n    "github.com/gofiber/fiber/v2"\n    "github.com/gofiber/fiber/v2/middleware/basicauth"\n)\nvar _ = basicauth.New(basicauth.Config{\n    StorePassword: true,\n    Users: map[string]string{},\n})`,
			want: `// TODO: StorePassword removed (true),`,
		},
	}

	for _, tc := range testCases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

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

			file := writeTempFile(t, dir, tc.input)

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

			content := readFile(t, file)
			assert.NotContains(t, content, "StorePassword:")
			assert.Contains(t, content, tc.want)
			assert.Contains(t, buf.String(), "Migrating basicauth StorePassword option")
		})
	}
}


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

Expand Down
Loading