Skip to content

Commit

Permalink
adds support of multiple versions by package in the replace command
Browse files Browse the repository at this point in the history
  • Loading branch information
rvflash committed Nov 21, 2024
1 parent d4dd720 commit 5fd3677
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 266 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ linters-settings:
- standard
- prefix(github.com)
- prefix(golang.org)
- prefix(google.golang.org)
- prefix(go.uber.org/mock)

linters:
enable-all: true
Expand Down
31 changes: 28 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
module github.com/rvflash/goup

go 1.14
go 1.22

toolchain go1.23.1

require (
github.com/fatih/color v1.16.0
github.com/go-git/go-git/v5 v5.11.0
github.com/golang/mock v1.7.0-rc.1
github.com/jdxcode/netrc v1.0.0
github.com/matryer/is v1.4.1
github.com/mattn/go-isatty v0.0.20
github.com/rvflash/workr v1.0.0
golang.org/x/mod v0.14.0
go.uber.org/mock v0.4.0
golang.org/x/mod v0.18.0
)

require (
dario.cat/mergo v1.0.1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
215 changes: 14 additions & 201 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func filePath(path string) string {

func walkPath(root string) []string {
var res []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(root, func(path string, _ os.FileInfo, err error) error {
if err != nil {
return err
}
Expand Down
19 changes: 12 additions & 7 deletions internal/semver/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ import (
type Tags []Tag

// Not removes from the list of tags the given tag.
func (t Tags) Not(w fmt.Stringer) Tags {
key := func(t Tags) int {
func (t Tags) Not(versions ...fmt.Stringer) Tags {
key := func(t Tags, xv fmt.Stringer) int {
for k, v := range t {
if Compare(v, w) == 0 {
if Compare(v, xv) == 0 {
return k
}
}
return -1
}
i := key(t)
if i < 0 {
return t
t2 := make([]Tag, len(t))
copy(t2, t)
for _, xv := range versions {
i := key(t2, xv)
if i < 0 {
return t2
}
t2 = append(t2[:i], t2[i+1:]...)
}
return append(t[:i], t[i+1:]...)
return t2
}

// Len implements the sort interface.
Expand Down
3 changes: 2 additions & 1 deletion internal/vcs/git/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/matryer/is"
errup "github.com/rvflash/goup/internal/errors"
"github.com/rvflash/goup/internal/vcs"
"github.com/rvflash/goup/internal/vcs/git"
mockvcs "github.com/rvflash/goup/testdata/mock/vcs"

"go.uber.org/mock/gomock"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion internal/vcs/goget/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"path/filepath"
"testing"

"github.com/golang/mock/gomock"
"github.com/matryer/is"
"github.com/rvflash/goup/internal/errors"
"github.com/rvflash/goup/internal/semver"
"github.com/rvflash/goup/internal/vcs"
"github.com/rvflash/goup/internal/vcs/goget"
mockvcs "github.com/rvflash/goup/testdata/mock/vcs"

"go.uber.org/mock/gomock"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion pkg/goup/entry_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"strings"
"testing"

"github.com/golang/mock/gomock"
"github.com/matryer/is"
"github.com/rvflash/goup/internal/errors"
"github.com/rvflash/goup/internal/semver"
"github.com/rvflash/goup/pkg/mod"
mockMod "github.com/rvflash/goup/testdata/mock/mod"

"go.uber.org/mock/gomock"
)

const (
Expand Down
15 changes: 12 additions & 3 deletions pkg/goup/goup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package goup
import (
"context"
"errors"
"fmt"
"io/ioutil"
"sync/atomic"
"time"
Expand Down Expand Up @@ -150,9 +151,9 @@ func (e *goUp) checkDependency(ctx context.Context, dep mod.Module) *Entry {
if err != nil {
return newFailure(err, dep)
}
x, ok := dep.ExcludeVersion()
if ok {
vs = vs.Not(x)
x := dep.ExcludeVersions()
if len(x) > 0 {
vs = vs.Not(stringer(x)...)
}
v, ok := latest(vs, dep, e.Config.Major, e.Config.MajorMinor)
if !ok {
Expand All @@ -170,6 +171,14 @@ func (e *goUp) checkDependency(ctx context.Context, dep mod.Module) *Entry {
return newFailure(errs.ErrSystem, dep)
}

func stringer(list []semver.Tag) []fmt.Stringer {
res := make([]fmt.Stringer, len(list))
for k, v := range list {
res[k] = v
}
return res
}

func updateFile(file mod.Mod) error {
buf, err := file.Format()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/goup/goup_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/matryer/is"
errup "github.com/rvflash/goup/internal/errors"
"github.com/rvflash/goup/internal/semver"
"github.com/rvflash/goup/internal/vcs"
"github.com/rvflash/goup/pkg/mod"
mockMod "github.com/rvflash/goup/testdata/mock/mod"
mockVCS "github.com/rvflash/goup/testdata/mock/vcs"

"go.uber.org/mock/gomock"
)

func TestGoUp_CheckDependency(t *testing.T) {
Expand Down Expand Up @@ -197,7 +198,7 @@ func newModule(ctrl *gomock.Controller, indirect bool) *mockMod.MockModule {
m.EXPECT().Path().Return(repoName).AnyTimes()
m.EXPECT().Version().Return(semver.New(v0)).AnyTimes()
m.EXPECT().Indirect().Return(indirect).AnyTimes()
m.EXPECT().ExcludeVersion().Return(nil, false).AnyTimes()
m.EXPECT().ExcludeVersions().Return(nil).AnyTimes()
return m
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/mod/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (f *File) Format() ([]byte, error) {

// dependencies returns the list of modules in this go.mod file.
// Firstly we get the modules used to replace legacy ones.
// Then those required. We use the replace dependency instead of this required.
// Then those required. We use the `replace` dependency instead of this required.
func dependencies(f *modfile.File) []Module {
var m = make(map[string]Module)
for _, r := range f.Replace {
Expand Down Expand Up @@ -184,7 +184,7 @@ func dependencies(f *modfile.File) []Module {
// Ignores exclusion of any unused dependency.
continue
}
m[r.Mod.Path].(*module).excludeVersion = semver.New(r.Mod.Version)
m[r.Mod.Path].(*module).excludes = append(m[r.Mod.Path].(*module).excludes, semver.New(r.Mod.Version))
}
return modules(m)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/mod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ type Module interface {
Path() string
Replacement() bool
Version() semver.Tag
ExcludeVersion() (v semver.Tag, ok bool)
ExcludeVersions() []semver.Tag
}

type module struct {
indirect,
replacement bool
path string
excludeVersion,
version *semver.Version
path string
excludes []semver.Tag
version *semver.Version
}

// ExcludeVersion implements the module interface.
func (m *module) ExcludeVersion() (v semver.Tag, ok bool) {
return m.excludeVersion, m.excludeVersion != nil
// ExcludeVersions implements the module interface.
func (m *module) ExcludeVersions() []semver.Tag {
return m.excludes
}

// Indirect implements the module interface.
Expand Down
28 changes: 13 additions & 15 deletions pkg/mod/module_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,29 @@ func TestModule_Version(t *testing.T) {
mod = module{}
are = is.New(t)
)
are.Equal(mod.Indirect(), false) // mismatch indirect
are.Equal(mod.Path(), "") // mismatch path
are.True(!mod.Replacement()) // mismatch replacement
are.Equal(mod.Version(), nil) // mismatch version
x, ok := mod.ExcludeVersion()
are.True(!ok) // unexpected exclude version
are.Equal(x, nil) // mismatch exclude version
are.Equal(mod.Indirect(), false) // mismatch indirect
are.Equal(mod.Path(), "") // mismatch path
are.True(!mod.Replacement()) // mismatch replacement
are.Equal(mod.Version(), nil) // mismatch version
are.Equal(0, len(mod.ExcludeVersions())) // unexpected exclude version
})

t.Run("valued", func(t *testing.T) {
t.Parallel()
var (
v = semver.New(version)
mod = module{
excludeVersion: v,
indirect: indirect,
path: path,
replacement: true,
version: v,
excludes: []semver.Tag{v},
indirect: indirect,
path: path,
replacement: true,
version: v,
}
are = is.New(t)
)
x, ok := mod.ExcludeVersion()
are.True(ok) // expected exclude version
are.Equal(x, v) // mismatch exclude version
x := mod.ExcludeVersions()
are.Equal(1, len(x)) // expected exclude version
are.Equal(x[0], v) // mismatch exclude version
are.Equal(mod.Indirect(), indirect) // mismatch indirect
are.Equal(mod.Path(), path) // mismatch path
are.True(mod.Replacement()) // mismatch replacement
Expand Down
11 changes: 8 additions & 3 deletions testdata/mock/mod/file.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions testdata/mock/mod/module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5fd3677

Please sign in to comment.