Skip to content

Commit

Permalink
fix: handle deps that also appear in replaces blocks in the go.mod
Browse files Browse the repository at this point in the history
Signed-off-by: hectorj2f <[email protected]>
  • Loading branch information
hectorj2f committed Jan 17, 2024
1 parent f98e8e9 commit fe3e57d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pkg/update/testdata/bye/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/puerco/hello

go 1.19

require github.com/sirupsen/logrus v1.8.0

replace github.com/sirupsen/logrus => github.com/sirupsen/logrus v1.3.0

require (
github.com/magefile/mage v1.10.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
13 changes: 13 additions & 0 deletions pkg/update/testdata/bye/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17 changes: 17 additions & 0 deletions pkg/update/testdata/bye/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2022 Puerco J. Cerdo
SPDX-License-Identifier: Apache-2.0
*/

package main

import (
"fmt"

"github.com/sirupsen/logrus"
)

func main() {
logrus.Info("preparing to say hi...")
fmt.Println("Hello World!")
}
7 changes: 6 additions & 1 deletion pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func checkPackageValues(pkgVersions map[string]*types.Package, modFile *modfile.
if _, ok := pkgVersions[replace.New.Path]; ok {
// pkg is already been replaced
pkgVersions[replace.New.Path].Replace = true
// This happens when we found a replace in the go mod for a dependency that we defined in deps.
// We need to drop that replace, so we need to set the name of the old path to use the existing one in the go.mod.
if pkgVersions[replace.New.Path].OldName == "" {
pkgVersions[replace.New.Path].OldName = replace.Old.Path
}
if semver.IsValid(pkgVersions[replace.New.Path].Version) {
if semver.Compare(replace.New.Version, pkgVersions[replace.New.Path].Version) > 0 {
return fmt.Errorf("package %s with version '%s' is already at version %s", replace.New.Path, replace.New.Version, pkgVersions[replace.New.Path].Version)
Expand Down Expand Up @@ -103,7 +108,7 @@ func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfil
log.Printf("Update package: %s\n", k)
log.Println("Running go mod edit replace ...")
if output, err := run.GoModEditReplaceModule(pkg.OldName, pkg.Name, pkg.Version, cfg.Modroot); err != nil {
return nil, fmt.Errorf("failed to run 'go mod edit -replace': %v with output: %v", err, output)
return nil, fmt.Errorf("failed to run 'go mod edit -replace': %v for package %s/%s@%s with output: %v", err, pkg.OldName, pkg.Name, pkg.Version, output)
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,46 @@ func TestGoModTidy(t *testing.T) {
}
}

func TestReplaceAndRequire(t *testing.T) {
testCases := []struct {
name string
pkgVersions map[string]*types.Package
want map[string]string
}{
{
name: "standard update",
pkgVersions: map[string]*types.Package{
"github.com/sirupsen/logrus": {
Name: "github.com/sirupsen/logrus",
Version: "v1.9.0",
},
},
want: map[string]string{
"github.com/sirupsen/logrus": "v1.9.0",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tmpdir := t.TempDir()
copyFile(t, "testdata/bye/go.mod", tmpdir)
copyFile(t, "testdata/bye/go.sum", tmpdir)
copyFile(t, "testdata/bye/main.go", tmpdir)

modFile, err := DoUpdate(tc.pkgVersions, &types.Config{Modroot: tmpdir, Tidy: false, GoVersion: ""})
if err != nil {
t.Fatal(err)
}
for pkg, want := range tc.want {
if got := getVersion(modFile, pkg); got != want {
t.Errorf("expected %s, got %s", want, got)
}
}
})
}
}

func TestUpdateError(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit fe3e57d

Please sign in to comment.