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 .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ issues:
linters:
- bodyclose
- err113
- revive
- source: 'fmt.Fprintf?'
linters:
- errcheck
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/migrations/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

var (
pkgRegex = regexp.MustCompile(`(?m)^(\s*require\s+)?(github\.com/gofiber/fiber/)(v\d+)(\s+)(v[\w.-]+)$`)
pkgRegex = regexp.MustCompile(`(?m)^(\s*(?:require\s+)?)(github\.com/gofiber/fiber/)(v\d+)(\s+)(v[\w.-]+)$`)
fiberImportRegex = regexp.MustCompile(`(^|")github\.com/gofiber/fiber/v\d+`)
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/migrations/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func Test_MigrateGoPkgs(t *testing.T) {

mainContent := `package main
import (
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
fiber "github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/adaptor"
)
func main() {
_, _ = fiber.New(), adaptor.New()
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/migrations/exec_stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func TestHelperProcess(t *testing.T) {
if out := os.Getenv("GO_HELPER_STDOUT"); out != "" {
_, _ = fmt.Fprint(os.Stdout, out)
}
os.Exit(0) //nolint:revive // helper process exits intentionally
os.Exit(0) // helper process exits intentionally
}
5 changes: 3 additions & 2 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
}
}

if !targetVersion.GreaterThan(currentVersion) && !opts.Force {
return fmt.Errorf("target version v%s is not greater than current version v%s", opts.TargetVersionS, currentVersionS)
if !opts.Force && !targetVersion.GreaterThan(currentVersion) {
cmd.Printf("Fiber already at %s\n", currentVersionS)
return nil
}

wd, err := os.Getwd()
Expand Down
93 changes: 87 additions & 6 deletions cmd/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ go 1.20
require github.com/gofiber/fiber/v2 v2.0.6
`

const hashABC = "abcdef1234567890abcdef1234567890abcdef12"

func Test_Migrate_V2_to_V3(t *testing.T) {
dir, err := os.MkdirTemp("", "migrate_v2_v3")
require.NoError(t, err)
Expand Down Expand Up @@ -251,9 +253,11 @@ require github.com/gofiber/fiber/v3 v3.0.0

t.Run("without force", func(t *testing.T) {
cmd := newMigrateCmd()
setupCmd()
defer teardownCmd()
out, err := runCobraCmd(cmd, "-t=3.0.0")
require.Error(t, err)
assert.Contains(t, out, "not greater")
require.NoError(t, err)
assert.Contains(t, out, "Fiber already at 3.0.0")
})

t.Run("force", func(t *testing.T) {
Expand Down Expand Up @@ -355,7 +359,7 @@ func Test_Migrate_WithHash(t *testing.T) {
require.NoError(t, os.Chdir(dir))
defer func() { require.NoError(t, os.Chdir(cwd)) }()

hash := "abcdef1234567890abcdef1234567890abcdef12"
hash := hashABC
short := hash[:7]
httpmock.Activate()
defer httpmock.DeactivateAndReset()
Expand Down Expand Up @@ -422,7 +426,7 @@ func Test_Migrate_WithHash_DowngradeWithForce(t *testing.T) {
require.NoError(t, os.Chdir(dir))
defer func() { require.NoError(t, os.Chdir(cwd)) }()

hash := "abcdef1234567890abcdef1234567890abcdef12"
hash := hashABC
short := hash[:7]
httpmock.Activate()
defer httpmock.DeactivateAndReset()
Expand All @@ -434,8 +438,10 @@ func Test_Migrate_WithHash_DowngradeWithForce(t *testing.T) {
setupCmd()
defer teardownCmd()
out, err := runCobraCmd(cmd, "-t=3.0.0", "--hash="+short)
require.Error(t, err)
assert.Contains(t, out, "not greater")
require.NoError(t, err)
assert.Contains(t, out, "Fiber already at")
gm := readFileTB(t, filepath.Join(dir, "go.mod"))
assert.Contains(t, gm, "fedcba654321")
})

t.Run("force", func(t *testing.T) {
Expand All @@ -450,3 +456,78 @@ func Test_Migrate_WithHash_DowngradeWithForce(t *testing.T) {
assert.NotContains(t, gm, "fedcba654321")
})
}

func Test_Migrate_Force_ResetVersion(t *testing.T) {
dir, err := os.MkdirTemp("", "migrate_hash_reset")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()

goMod := "module example\n\n" +
"go 1.20\n\n" +
"require github.com/gofiber/fiber/v3\tv3.0.1-0.20200102030405-abcdef123456\n"
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(goMod), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main"), 0o600))

cwd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(dir))
defer func() { require.NoError(t, os.Chdir(cwd)) }()

t.Run("without force", func(t *testing.T) {
cmd := newMigrateCmd()
setupCmd()
defer teardownCmd()
out, err := runCobraCmd(cmd, "-t=3.0.0")
require.NoError(t, err)
assert.Contains(t, out, "Fiber already at")
gm := readFileTB(t, filepath.Join(dir, "go.mod"))
assert.Contains(t, gm, "abcdef123456")
})

t.Run("force", func(t *testing.T) {
cmd := newMigrateCmd()
setupCmd()
defer teardownCmd()
_, err := runCobraCmd(cmd, "-t=3.0.0", "-f")
require.NoError(t, err)

gm := readFileTB(t, filepath.Join(dir, "go.mod"))
assert.Contains(t, gm, "github.com/gofiber/fiber/v3\tv3.0.0")
assert.NotContains(t, gm, "abcdef123456")
})
}

func Test_Migrate_WithHash_RequireBlock(t *testing.T) {
dir, err := os.MkdirTemp("", "migrate_hash_block")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()

goMod := "module example\n\n" +
"go 1.20\n\n" +
"require (\n" +
"\tgithub.com/gofiber/fiber/v3 v3.0.0-beta.5\n" +
")\n"
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(goMod), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main"), 0o600))

cwd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(dir))
defer func() { require.NoError(t, os.Chdir(cwd)) }()

hash := hashABC
short := hash[:7]
httpmock.Activate()
defer httpmock.DeactivateAndReset()
commitURL := "https://api.github.com/repos/gofiber/fiber/commits/" + short
httpmock.RegisterResponder(http.MethodGet, commitURL, httpmock.NewBytesResponder(200, []byte(`{"sha":"`+hash+`","commit":{"committer":{"date":"2020-01-02T03:04:05Z"}}}`)))

cmd := newMigrateCmd()
setupCmd()
defer teardownCmd()
_, err = runCobraCmd(cmd, "-t=3.0.0-beta.5", "--hash="+short, "-f")
require.NoError(t, err)

gm := readFileTB(t, filepath.Join(dir, "go.mod"))
assert.Contains(t, gm, "github.com/gofiber/fiber/v3 v3.0.0-beta.5.0.20200102030405-abcdef123456")
}
Loading