Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run go mod tidy after go mod edit -replace #39

Merged
merged 4 commits into from
Feb 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
replaced := make(map[string]string)
for _, r := range b.Replacements {
log.Printf("[INFO] Replace %s => %s", r.Old.String(), r.New.String())
cmd := env.newCommand("go", "mod", "edit",
"-replace", fmt.Sprintf("%s=%s", r.Old.Param(), r.New.Param()))
err := env.runCommand(ctx, cmd, 10*time.Second)
err = env.execGoModReplace(ctx, r.Old.Param(), r.New.Param())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -135,12 +133,11 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) {
if err != nil {
return nil, err
}
replace := fmt.Sprintf("%s=%s", k6ModulePath, b.K6Repo)
replace := b.K6Repo
if b.K6Version != "" {
replace = fmt.Sprintf("%s@%s", replace, b.K6Version)
replace = fmt.Sprintf("%s@%s", b.K6Repo, b.K6Version)
}
cmd = env.newCommand("go", "mod", "edit", "-replace", replace)
err = env.runCommand(ctx, cmd, 10*time.Second)
err = env.execGoModReplace(ctx, k6ModulePath, replace)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -264,6 +261,18 @@ func (env environment) execGoModRequire(ctx context.Context, modulePath, moduleV
return env.runCommand(ctx, tidyCmd, env.timeoutGoGet)
}

func (env environment) execGoModReplace(ctx context.Context, modulePath, replaceRepo string) error {
replace := fmt.Sprintf("%s=%s", modulePath, replaceRepo)
cmd := env.newCommand("go", "mod", "edit", "-replace", replace)
err := env.runCommand(ctx, cmd, 10*time.Second)
if err != nil {
return err
}
// tidy the module to ensure go.mod will not have versions such as `latest`
tidyCmd := env.newCommand("go", "mod", "tidy")
return env.runCommand(ctx, tidyCmd, env.timeoutGoGet)
}

type goModTemplateContext struct {
K6Module string
Extensions []string
Expand Down