Skip to content

Commit 2f9a35d

Browse files
authored
fix: respect go get order (#30)
Signed-off-by: hectorj2f <[email protected]>
1 parent faace68 commit 2f9a35d

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

cmd/gobump/root.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,22 @@ var rootCmd = &cobra.Command{
3535
}
3636
packages := strings.Split(rootFlags.packages, " ")
3737
pkgVersions := map[string]*types.Package{}
38-
for _, pkg := range packages {
38+
for i, pkg := range packages {
3939
parts := strings.Split(pkg, "@")
4040
if len(parts) != 2 {
4141
return fmt.Errorf("Error: Invalid package format. Each package should be in the format <package@version>. Usage: gobump --packages=\"<package1@version> <package2@version> ...\"")
4242
}
4343
pkgVersions[parts[0]] = &types.Package{
4444
Name: parts[0],
4545
Version: parts[1],
46+
Index: i,
4647
}
4748
}
4849

4950
var replaces []string
5051
if len(rootFlags.replaces) != 0 {
5152
replaces = strings.Split(rootFlags.replaces, " ")
52-
for _, replace := range replaces {
53+
for i, replace := range replaces {
5354
parts := strings.Split(replace, "=")
5455
if len(parts) != 2 {
5556
return fmt.Errorf("Error: Invalid replace format. Each replace should be in the format <oldpackage=newpackage@version>. Usage: gobump -replaces=\"<oldpackage=newpackage@version> ...\"")
@@ -65,6 +66,7 @@ var rootCmd = &cobra.Command{
6566
Name: rep[0],
6667
Version: rep[1],
6768
Replace: true,
69+
Index: i,
6870
}
6971
}
7072
}

pkg/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Package struct {
66
Version string
77
Replace bool
88
Require bool
9+
Index int
910
}
1011

1112
type Config struct {

pkg/update/update.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path"
10+
"sort"
1011
"strings"
1112

1213
"github.com/chainguard-dev/gobump/pkg/run"
@@ -99,8 +100,11 @@ func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfil
99100
return nil, err
100101
}
101102

103+
depsBumpOrdered := orderPkgVersionsMap(pkgVersions)
104+
102105
// Replace the packages first.
103-
for k, pkg := range pkgVersions {
106+
for _, k := range depsBumpOrdered {
107+
pkg := pkgVersions[k]
104108
if pkg.Replace {
105109
log.Printf("Update package: %s\n", k)
106110
log.Println("Running go mod edit replace ...")
@@ -109,8 +113,9 @@ func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfil
109113
}
110114
}
111115
}
112-
// Bump the require or new get packages.
113-
for k, pkg := range pkgVersions {
116+
// Bump the require or new get packages in the specified order.
117+
for _, k := range depsBumpOrdered {
118+
pkg := pkgVersions[k]
114119
// Skip the replace that have been updated above
115120
if !pkg.Replace {
116121
log.Printf("Update package: %s\n", k)
@@ -166,6 +171,17 @@ func DoUpdate(pkgVersions map[string]*types.Package, cfg *types.Config) (*modfil
166171
return newModFile, nil
167172
}
168173

174+
func orderPkgVersionsMap(pkgVersions map[string]*types.Package) []string {
175+
depsBumpOrdered := make([]string, 0, len(pkgVersions))
176+
for repo := range pkgVersions {
177+
depsBumpOrdered = append(depsBumpOrdered, repo)
178+
}
179+
sort.SliceStable(depsBumpOrdered, func(i, j int) bool {
180+
return pkgVersions[depsBumpOrdered[i]].Index < pkgVersions[depsBumpOrdered[j]].Index
181+
})
182+
return depsBumpOrdered
183+
}
184+
169185
func getVersion(modFile *modfile.File, packageName string) string {
170186
// Handle package update, including 'replace' clause
171187

pkg/update/update_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package update
22

33
import (
44
"os/exec"
5+
"reflect"
56
"testing"
67

78
"github.com/chainguard-dev/gobump/pkg/types"
@@ -58,6 +59,54 @@ func TestUpdate(t *testing.T) {
5859
}
5960
}
6061

62+
func TestUpdateInOrder(t *testing.T) {
63+
testCases := []struct {
64+
name string
65+
pkgVersions map[string]*types.Package
66+
want []string
67+
}{
68+
{
69+
name: "standard update",
70+
pkgVersions: map[string]*types.Package{
71+
"github.com/google/uuid": {
72+
Name: "github.com/google/uuid",
73+
Version: "v1.4.0",
74+
Index: 0,
75+
},
76+
"k8s.io/api": {
77+
OldName: "k8s.io/api",
78+
Name: "k8s.io/api",
79+
Version: "v0.28.0",
80+
Index: 2,
81+
},
82+
"k8s.io/client-go": {
83+
OldName: "k8s.io/client-go",
84+
Name: "k8s.io/client-go",
85+
Version: "v0.28.0",
86+
Index: 1,
87+
},
88+
},
89+
want: []string{
90+
"github.com/google/uuid",
91+
"k8s.io/api",
92+
"k8s.io/client-go",
93+
},
94+
},
95+
}
96+
97+
for _, tc := range testCases {
98+
t.Run(tc.name, func(t *testing.T) {
99+
tmpdir := t.TempDir()
100+
copyFile(t, "testdata/aws-efs-csi-driver/go.mod", tmpdir)
101+
102+
got := orderPkgVersionsMap(tc.pkgVersions)
103+
if len(got) != len(tc.want) || reflect.DeepEqual(got, tc.want) {
104+
t.Errorf("expected %s, got %s", tc.want, got)
105+
}
106+
})
107+
}
108+
}
109+
61110
func TestGoModTidy(t *testing.T) {
62111
testCases := []struct {
63112
name string

0 commit comments

Comments
 (0)