@@ -22,6 +22,7 @@ import (
2222 "os"
2323 "os/exec"
2424 "path/filepath"
25+ "regexp"
2526 "strings"
2627 "testing"
2728 "time"
@@ -126,7 +127,7 @@ func TestMemcached(t *testing.T) {
126127 }()
127128 }
128129 }
129- modBytes , err := insertGoModReplace (t , sdkRepo , replace .repo , replace .ref )
130+ modBytes , err := insertGoModReplace (sdkRepo , replace .repo , replace .ref )
130131 if err != nil {
131132 t .Fatalf ("Failed to insert go.mod replace: %v" , err )
132133 }
@@ -311,11 +312,15 @@ func getGoModReplace(t *testing.T, localSDKPath string) goModReplace {
311312 }
312313}
313314
314- func insertGoModReplace (t * testing. T , repo , path , sha string ) ([]byte , error ) {
315+ func insertGoModReplace (repo , path , sha string ) ([]byte , error ) {
315316 modBytes , err := ioutil .ReadFile ("go.mod" )
316317 if err != nil {
317318 return nil , errors .Wrap (err , "failed to read go.mod" )
318319 }
320+ // Remove all replace lines in go.mod.
321+ replaceRe := regexp .MustCompile (fmt .Sprintf ("(replace )?%s =>.+" , repo ))
322+ modBytes = replaceRe .ReplaceAll (modBytes , nil )
323+ // Append the desired replace to the end of go.mod's bytes.
319324 sdkReplace := fmt .Sprintf ("replace %s => %s" , repo , path )
320325 if sha != "" {
321326 sdkReplace = fmt .Sprintf ("%s %s" , sdkReplace , sha )
0 commit comments