Skip to content

Commit

Permalink
fix: module replace (#12)
Browse files Browse the repository at this point in the history
* fix: module replace

Signed-off-by: hectorj2f <[email protected]>

* chore: add one more check

Signed-off-by: hectorj2f <[email protected]>

---------

Signed-off-by: hectorj2f <[email protected]>
  • Loading branch information
hectorj2f authored Jan 16, 2024
1 parent 99a167b commit 7c0cecf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func DoUpdate(pkgVersions map[string]*types.Package, modroot string, tidy bool,
if pkg.Replace {
log.Printf("Update package: %s\n", k)
log.Println("Running go mod edit replace ...")
if output, err := run.GoModEditReplaceModule(pkg.Name, pkg.Name, pkg.Version, modroot); err != nil {
if output, err := run.GoModEditReplaceModule(pkg.OldName, pkg.Name, pkg.Version, modroot); err != nil {
return nil, fmt.Errorf("failed to run 'go mod edit -replace': %v with output: %v", err, output)
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func DoUpdate(pkgVersions map[string]*types.Package, modroot string, tidy bool,
for _, pkg := range pkgVersions {
verStr := getVersion(newModFile, pkg.Name)
if semver.Compare(verStr, pkg.Version) < 0 {
return nil, fmt.Errorf("package %s is less than the desired version %s", pkg.Name, pkg.Version)
return nil, fmt.Errorf("package %s with %s is less than the desired version %s", pkg.Name, verStr, pkg.Version)
}
}

Expand All @@ -143,7 +143,8 @@ func getVersion(modFile *modfile.File, packageName string) string {

// Replace checks have to come first!
for _, replace := range modFile.Replace {
if replace.Old.Path == packageName {
// Check if there is a new
if replace.New.Path == packageName {
return replace.New.Version
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestUpdate(t *testing.T) {
name: "replace",
pkgVersions: map[string]*types.Package{
"k8s.io/client-go": {
OldName: "k8s.io/client-go",
Name: "k8s.io/client-go",
Version: "v0.28.0",
},
Expand Down Expand Up @@ -147,6 +148,9 @@ func TestReplaces(t *testing.T) {
if r.New.Path != "github.com/fakefuzz" {
t.Errorf("expected replace of github.com/google/gofuzz with github.com/fakefuzz, got %s", r.New.Path)
}
if r.Old.Path != "github.com/google/gofuzz" {
t.Errorf("expected replace of github.com/google/gofuzz, got %s", r.Old.Path)
}
if r.New.Version != "v1.2.3" {
t.Errorf("expected replace of github.com/google/gofuzz with v1.2.3, got %s", r.New.Version)
}
Expand Down

0 comments on commit 7c0cecf

Please sign in to comment.