Skip to content

Commit 8754b98

Browse files
author
Eric Stroczynski
authored
e2e: remove existing replace lines in go.mod for the SDK repo (#1664) (#1719)
* hack/,test/e2e/memcached_test.go: remove existing replace lines in go.mod for the SDK repo
1 parent 709fb1d commit 8754b98

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

hack/lib/test_lib.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ function add_go_mod_replace() {
7575
echo "module ${from_path}" > "${to_path}/go.mod"
7676
trap_add "rm ${to_path}/go.mod" EXIT
7777
fi
78+
# Check if a replace line already exists. If it does, remove. If not, append.
79+
if grep -q "${from_path} =>" go.mod; then
80+
sed -E -i 's|^.+'"${from_path} =>"'.+$||g' go.mod
81+
fi
7882
# Do not use "go mod edit" so formatting stays the same.
7983
local replace="replace ${from_path} => ${to_path}"
8084
if [[ -n "$version" ]]; then

test/e2e/memcached_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)