Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
zoncoen committed May 30, 2024
1 parent b49b11d commit b2315b1
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 11 deletions.
54 changes: 43 additions & 11 deletions cmd/scenarigo/cmd/plugin/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
tip bool
goMinVer string
versionTooHighErrorRegexp *regexp.Regexp
retryError = errors.New("retry")
)

func init() {
Expand Down Expand Up @@ -75,6 +76,8 @@ type overrideModule struct {
replace *modfile.Replace
replacedBy string
replaceLocalPath string
names []string
force bool
}

func (o *overrideModule) requireReplace() (*modfile.Require, string, *modfile.Replace, string) {
Expand All @@ -90,6 +93,15 @@ func (o *overrideModule) requireReplace() (*modfile.Require, string, *modfile.Re
return o.require, o.requiredBy, o.replace, o.replacedBy
}

func (o *overrideModule) appendName(name string) {
for _, n := range o.names {
if n == name {
return
}
}
o.names = append(o.names, name)
}

func buildRun(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
Expand Down Expand Up @@ -144,7 +156,9 @@ func buildRun(cmd *cobra.Command, args []string) error {
overrides[m] = &overrideModule{
require: o.require,
requiredBy: o.requiredBy,
force: true,
}
overrides[m].appendName(o.requiredBy)
}

requires, err := requiredModulesByScenarigo()
Expand All @@ -155,7 +169,9 @@ func buildRun(cmd *cobra.Command, args []string) error {
overrides[r.Mod.Path] = &overrideModule{
require: r,
requiredBy: "scenarigo",
force: true,
}
overrides[r.Mod.Path].appendName("scenarigo")
}

overrideKeys := make([]string, 0, len(overrides))
Expand All @@ -164,18 +180,22 @@ func buildRun(cmd *cobra.Command, args []string) error {
}
sort.Strings(overrideKeys)

var retry bool
for _, pb := range pbs {
if err := pb.build(cmd, goCmd, overrideKeys, overrides); err != nil {
if err == retryError {
retry = true
continue
}
return fmt.Errorf("failed to build plugin %s: %w", pb.name, err)
}
}

// TODO
break
if !retry {
break
}
}

for _, pb := range pbs {
// TODO
if err := pb.printUpdatedResult(cmd, goCmd, pb.name, pb.gomodPath, overrides); err != nil {
return err
}
Expand Down Expand Up @@ -206,8 +226,10 @@ func selectUnifiedVersions(pbs []*pluginBuilder) (map[string]*overrideModule, er
require: r,
requiredBy: pb.name,
}
overrides[r.Mod.Path].appendName(pb.name)
continue
}
overrides[r.Mod.Path].appendName(pb.name)
if compareVers(o.require.Mod.Version, r.Mod.Version) < 0 {
overrides[r.Mod.Path].require = r
overrides[r.Mod.Path].requiredBy = pb.name
Expand All @@ -226,8 +248,10 @@ func selectUnifiedVersions(pbs []*pluginBuilder) (map[string]*overrideModule, er
replacedBy: pb.name,
replaceLocalPath: localPath,
}
overrides[r.Old.Path].appendName(pb.name)
continue
}
overrides[r.Old.Path].appendName(pb.name)
if o.replace != nil {
if o.replace.New.Path != r.New.Path || o.replace.New.Version != r.New.Version {
if (localPath == "" && o.replaceLocalPath == "") || localPath != o.replaceLocalPath {
Expand Down Expand Up @@ -581,7 +605,12 @@ func (pb *pluginBuilder) updateReplaceDirectives(cmd *cobra.Command, goCmd strin
}
} else {
if v, ok := requires[require.Mod.Path]; ok {
if require.Mod.Version != v {
switch compareVers(require.Mod.Version, v) {
case -1: // require.Mod.Version < v
// change the miximum version
fmt.Println("override", require.Mod.Path, require.Mod.Version, v)
return retryError
case 1: // require.Mod.Version > v
if err := gomod.AddReplace(require.Mod.Path, v, require.Mod.Path, require.Mod.Version); err != nil {
return fmt.Errorf("%s: %w", pb.gomodPath, err)
}
Expand Down Expand Up @@ -719,16 +748,19 @@ func printUpdatedReplaces(cmd *cobra.Command, name string, overrides map[string]
}

func (pb *pluginBuilder) editGoMod(cmd *cobra.Command, goCmd string, edit func(*modfile.File) error) error {
gomod, err := parseGoMod(cmd, goCmd, pb.gomodPath)
if err != nil {
return fmt.Errorf("failed to parse %s: %w", pb.gomodPath, err)
if pb.gomod == nil {
gomod, err := parseGoMod(cmd, goCmd, pb.gomodPath)
if err != nil {
return fmt.Errorf("failed to parse %s: %w", pb.gomodPath, err)
}
pb.gomod = gomod
}

if err := edit(gomod); err != nil {
if err := edit(pb.gomod); err != nil {
return fmt.Errorf("failed to edit %s: %w", pb.gomodPath, err)
}
gomod.Cleanup()
edited, err := gomod.Format()
pb.gomod.Cleanup()
edited, err := pb.gomod.Format()
if err != nil {
return fmt.Errorf("failed to edit %s: %w", pb.gomodPath, err)
}
Expand Down
95 changes: 95 additions & 0 deletions cmd/scenarigo/cmd/plugin/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,16 @@ import (
"google.golang.org/grpc": {
require: &modfile.Require{
Mod: module.Version{
Path: "google.golang.org/grpc",
Version: "v1.46.0",
},
},
replace: &modfile.Replace{
Old: module.Version{
Path: "google.golang.org/grpc",
Version: "v1.46.0",
},
New: module.Version{
Path: "google.golang.org/grpc",
Version: "v1.40.0",
},
Expand Down Expand Up @@ -1922,6 +1932,91 @@ go 100.0.0
})
}
})

t.Run("retry", func(t *testing.T) {
tests := map[string]struct {
gomod string
src string
overrides map[string]*overrideModule
expect string
}{
"override require by the smaller version": {
gomod: fmt.Sprintf(`module plugin_module
go %s
require google.golang.org/grpc v1.37.1
require (
github.com/golang/protobuf v1.4.2 // indirect
golang.org/x/net v0.0.0-20190311183353-d8887717615a // indirect
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a // indirect
golang.org/x/text v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.25.0 // indirect
)
`, goVersion),
src: `package main
import (
_ "google.golang.org/grpc"
)
`,
overrides: map[string]*overrideModule{
"google.golang.org/grpc": {
require: &modfile.Require{
Mod: module.Version{
Path: "google.golang.org/grpc",
Version: "v1.36.0",
},
},
requiredBy: "test",
},
},
expect: fmt.Sprintf("go.mod requires go >= 100.0.0 (running go %s)", goVer),
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
tmpDir := t.TempDir()
gomod := filepath.Join(tmpDir, "go.mod")
create(t, gomod, test.gomod)
if test.src != "" {
create(t, filepath.Join(tmpDir, "main.go"), test.src)
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
goCmd, err := findGoCmd(ctx)
if err != nil {
t.Fatalf("failed to find go command: %s", err)
}

overrideKeys := make([]string, 0, len(test.overrides))
for k := range test.overrides {
overrideKeys = append(overrideKeys, k)
}
sort.Strings(overrideKeys)

cmd := &cobra.Command{}
var stdout bytes.Buffer
cmd.SetOutput(&stdout)

pb := &pluginBuilder{
name: "test.so",
gomodPath: gomod,
}
err = pb.updateGoMod(cmd, goCmd, overrideKeys, test.overrides)
if err == nil {
t.Fatal("no error")
}
if !strings.Contains(err.Error(), test.expect) {
t.Fatalf("expected %q but got %q", test.expect, err)
}
})
}
})
}

func setupGitServer(t *testing.T, goCmd string) {
Expand Down

0 comments on commit b2315b1

Please sign in to comment.