#8 new flag UpMajor.

This commit is contained in:
Gorbunov Kirill Andreevich
2024-03-17 15:41:25 +03:00
parent 3a60ffbf91
commit 974c685136
5 changed files with 84 additions and 14 deletions

View File

@@ -250,7 +250,8 @@ type UpdateOptions struct {
Modules []module.Version
Pre bool
Cached bool
Major bool
Major bool // Major true compare only major
UpMajor bool // UpMajor module up with major
}
// Updates finds updates for a set of specified modules.
@@ -276,13 +277,13 @@ func Updates(opt UpdateOptions) {
ch <- Update{Module: m, Err: err}
return nil
}
prefix, ok := ModMajor(mod.Path)
major := semver.Major(m.Version)
var v string
switch ok {
switch opt.UpMajor {
case true:
v = mod.MaxVersion(prefix, opt.Pre)
default:
v = mod.MaxVersion("", opt.Pre)
case false:
v = mod.MaxVersion(major, opt.Pre)
}
if IsNewerVersion(m.Version, v, opt.Major) {
ch <- Update{Module: m, Version: v}

View File

@@ -0,0 +1,55 @@
package modules
import "testing"
func TestModMajor(t *testing.T) {
type args struct {
modpath string
}
var tests = []struct {
name string
args args
want string
want1 bool
}{
{"Test #1",
args{
"github.com/jackc/chunkreader/v2",
},
"v2",
true,
},
{"Test #2",
args{
"github.com/jackc/chunkreader",
},
"",
true,
},
{"Test #3",
args{
"gopkg.in/yaml.v2",
},
"v2",
true,
},
{"Test #4",
args{
"github.com/jackc/chunkreader/v1",
},
"",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := ModMajor(tt.args.modpath)
if got != tt.want {
t.Errorf("ModMajor() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("ModMajor() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

View File

@@ -210,6 +210,11 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
var cmd *exec.Cmd
var out []byte
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-droprequire=%s", mod.Module.Path))
if out, err = cmd.CombinedOutput(); err != nil {
logger.Fatalf(ctx, "failed to run go mod edit: %s err: %v", out, err)
}
cmd = exec.CommandContext(ctx, epath, "mod", "edit", fmt.Sprintf("-require=%s@%s", path, mod.Version))
if out, err = cmd.CombinedOutput(); err != nil {
logger.Fatalf(ctx, "failed to run go mod edit: %s err: %v", out, err)