Skip to content

Commit

Permalink
fail when attempting to bump the main module dependency (#35)
Browse files Browse the repository at this point in the history
Signed-off-by: hectorj2f <[email protected]>
  • Loading branch information
hectorj2f authored May 16, 2024
1 parent 047c587 commit f403a69
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func ParseGoModfile(path string) (*modfile.File, []byte, error) {
}

func checkPackageValues(pkgVersions map[string]*types.Package, modFile *modfile.File) error {
if _, ok := pkgVersions[modFile.Module.Mod.Path]; ok {
return fmt.Errorf("bumping the main module is not allowed '%s'", modFile.Module.Mod.Path)
}
// Detect if the list of packages contain any replace statement for the package, if so we might drop that replace with a new one.
for _, replace := range modFile.Replace {
if replace != nil {
Expand Down
20 changes: 18 additions & 2 deletions pkg/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func TestGoModTidy(t *testing.T) {
pkgVersions map[string]*types.Package
fileName string
want map[string]string
wantErr bool
errMsg string
}{
{
name: "standard update",
Expand All @@ -171,6 +173,16 @@ func TestGoModTidy(t *testing.T) {
want: map[string]string{
"github.com/sirupsen/logrus": "v1.9.0",
},
}, {
name: "error when bumping main module",
pkgVersions: map[string]*types.Package{
"github.com/puerco/hello": {
Name: "github.com/puerco/hello",
Version: "v1.9.0",
},
},
wantErr: true,
errMsg: "bumping the main module is not allowed 'github.com/puerco/hello'",
},
}

Expand All @@ -183,8 +195,12 @@ func TestGoModTidy(t *testing.T) {

pkgVersions := maybeParseFile(t, tc.fileName, tc.pkgVersions)
modFile, err := DoUpdate(pkgVersions, &types.Config{Modroot: tmpdir, Tidy: false, GoVersion: ""})
if err != nil {
t.Fatal(err)
if (err != nil) != tc.wantErr {
t.Errorf("DoUpdate() error = %v, wantErr %v", err, tc.wantErr)
return
}
if tc.wantErr && err.Error() != tc.errMsg {
t.Errorf("expected err message %s, got %s", tc.errMsg, err.Error())
}
for pkg, want := range tc.want {
if got := getVersion(modFile, pkg); got != want {
Expand Down

0 comments on commit f403a69

Please sign in to comment.