@@ -22,6 +22,7 @@ import (
22
22
"os"
23
23
"os/exec"
24
24
"path/filepath"
25
+ "regexp"
25
26
"strings"
26
27
"testing"
27
28
"time"
@@ -126,7 +127,7 @@ func TestMemcached(t *testing.T) {
126
127
}()
127
128
}
128
129
}
129
- modBytes , err := insertGoModReplace (t , sdkRepo , replace .repo , replace .ref )
130
+ modBytes , err := insertGoModReplace (sdkRepo , replace .repo , replace .ref )
130
131
if err != nil {
131
132
t .Fatalf ("Failed to insert go.mod replace: %v" , err )
132
133
}
@@ -311,11 +312,15 @@ func getGoModReplace(t *testing.T, localSDKPath string) goModReplace {
311
312
}
312
313
}
313
314
314
- func insertGoModReplace (t * testing. T , repo , path , sha string ) ([]byte , error ) {
315
+ func insertGoModReplace (repo , path , sha string ) ([]byte , error ) {
315
316
modBytes , err := ioutil .ReadFile ("go.mod" )
316
317
if err != nil {
317
318
return nil , errors .Wrap (err , "failed to read go.mod" )
318
319
}
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.
319
324
sdkReplace := fmt .Sprintf ("replace %s => %s" , repo , path )
320
325
if sha != "" {
321
326
sdkReplace = fmt .Sprintf ("%s %s" , sdkReplace , sha )
0 commit comments