Skip to content

Commit 0dad71c

Browse files
committed
only rewrite Gopkg.toml once always
1 parent 8c1ade5 commit 0dad71c

File tree

1 file changed

+33
-63
lines changed

1 file changed

+33
-63
lines changed

test/e2e/memcached_test.go

+33-63
Original file line numberDiff line numberDiff line change
@@ -64,59 +64,55 @@ func TestMemcached(t *testing.T) {
6464
}
6565

6666
t.Log("Creating new operator project")
67-
var solveFailed bool
6867
cmdOut, err := exec.Command("operator-sdk",
6968
"new",
7069
"memcached-operator").CombinedOutput()
7170
if err != nil {
7271
// HACK: dep cannot resolve non-master branches as the base branch for PR's,
7372
// so running `dep ensure` will fail when first running
74-
// `operator-sdk new ...`. For now we can ignore the first solve failure,
75-
// and run `dep ensure` again.
73+
// `operator-sdk new ...`. For now we can ignore the first solve failure.
7674
// A permanent solution can be implemented once the following is merged:
7775
// https://github.com/golang/dep/pull/1658
7876
solveFailRe := regexp.MustCompile(`(?m)^[ \t]*Solving failure:.+github\.com/operator-framework/operator-sdk.+:$`)
79-
if solveFailRe.Match(cmdOut) {
80-
prSlug, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SLUG")
81-
if ok && prSlug != "" {
82-
prSha, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SHA")
83-
if ok && prSha != "" {
84-
os.Chdir("memcached-operator")
85-
gopkg, err := ioutil.ReadFile("Gopkg.toml")
86-
if err != nil {
87-
t.Fatal(err)
88-
}
89-
// Match against the '#osdk_branch_annotation' used for version substitution
90-
// and comment out the current branch.
91-
branchRe := regexp.MustCompile("([ ]+)(.+#osdk_branch_annotation)")
92-
gopkg = branchRe.ReplaceAll(gopkg, []byte("$1# $2"))
93-
// Plug in the fork to test against so `dep ensure` can resolve dependencies
94-
// correctly.
95-
gopkgString := string(gopkg)
96-
gopkgLoc := strings.LastIndex(gopkgString, "\n name = \"github.com/operator-framework/operator-sdk\"\n")
97-
gopkgString = gopkgString[:gopkgLoc] + "\n source = \"https://github.com/" + prSlug + "\"\n revision = \"" + prSha + "\"\n" + gopkgString[gopkgLoc+1:]
98-
err = ioutil.WriteFile("Gopkg.toml", []byte(gopkgString), filemode)
99-
if err != nil {
100-
t.Fatalf("failed to write updated Gopkg.toml: %v", err)
101-
}
102-
103-
solveFailed = true
104-
t.Logf("Gopkg.toml: %v", gopkgString)
105-
} else {
106-
t.Fatal("could not find sha of PR")
107-
}
77+
if !solveFailRe.Match(cmdOut) {
78+
t.Fatalf("error: %v\nCommand Output: %s\n", err, string(cmdOut))
79+
}
80+
}
81+
ctx.AddFinalizerFn(func() error { return os.RemoveAll(absProjectPath) })
82+
83+
os.Chdir("memcached-operator")
84+
prSlug, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SLUG")
85+
if ok && prSlug != "" {
86+
prSha, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SHA")
87+
if ok && prSha != "" {
88+
gopkg, err := ioutil.ReadFile("Gopkg.toml")
89+
if err != nil {
90+
t.Fatal(err)
10891
}
109-
cmdOut, err = exec.Command("dep", "ensure").CombinedOutput()
92+
// Match against the '#osdk_branch_annotation' used for version substitution
93+
// and comment out the current branch.
94+
branchRe := regexp.MustCompile("([ ]+)(.+#osdk_branch_annotation)")
95+
gopkg = branchRe.ReplaceAll(gopkg, []byte("$1# $2"))
96+
// Plug in the fork to test against so `dep ensure` can resolve dependencies
97+
// correctly.
98+
gopkgString := string(gopkg)
99+
gopkgLoc := strings.LastIndex(gopkgString, "\n name = \"github.com/operator-framework/operator-sdk\"\n")
100+
gopkgString = gopkgString[:gopkgLoc] + "\n source = \"https://github.com/" + prSlug + "\"\n revision = \"" + prSha + "\"\n" + gopkgString[gopkgLoc+1:]
101+
err = ioutil.WriteFile("Gopkg.toml", []byte(gopkgString), filemode)
110102
if err != nil {
111-
t.Fatalf("error: %v\nCommand Output: %s\n", err, string(cmdOut))
103+
t.Fatalf("failed to write updated Gopkg.toml: %v", err)
112104
}
105+
106+
t.Logf("Gopkg.toml: %v", gopkgString)
113107
} else {
114-
t.Fatalf("error: %v\nCommand Output: %s\n", err, string(cmdOut))
108+
t.Fatal("could not find sha of PR")
115109
}
116110
}
117-
ctx.AddFinalizerFn(func() error { return os.RemoveAll(absProjectPath) })
111+
cmdOut, err = exec.Command("dep", "ensure").CombinedOutput()
112+
if err != nil {
113+
t.Fatalf("error: %v\nCommand Output: %s\n", err, string(cmdOut))
114+
}
118115

119-
os.Chdir("memcached-operator")
120116
cmdOut, err = exec.Command("operator-sdk",
121117
"add",
122118
"api",
@@ -189,32 +185,6 @@ func TestMemcached(t *testing.T) {
189185
}
190186

191187
t.Log("Pulling new dependencies with dep ensure")
192-
prSlug, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SLUG")
193-
if !solveFailed && ok && prSlug != "" {
194-
prSha, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_SHA")
195-
if ok && prSha != "" {
196-
gopkg, err := ioutil.ReadFile("Gopkg.toml")
197-
if err != nil {
198-
t.Fatal(err)
199-
}
200-
// Match against the '#osdk_branch_annotation' used for version substitution
201-
// and comment out the current branch.
202-
branchRe := regexp.MustCompile("([ ]+)(.+#osdk_branch_annotation)")
203-
gopkg = branchRe.ReplaceAll(gopkg, []byte("$1# $2"))
204-
// Plug in the fork to test against so `dep ensure` can resolve dependencies
205-
// correctly.
206-
gopkgString := string(gopkg)
207-
gopkgLoc := strings.LastIndex(gopkgString, "\n name = \"github.com/operator-framework/operator-sdk\"\n")
208-
gopkgString = gopkgString[:gopkgLoc] + "\n source = \"https://github.com/" + prSlug + "\"\n revision = \"" + prSha + "\"\n" + gopkgString[gopkgLoc+1:]
209-
err = ioutil.WriteFile("Gopkg.toml", []byte(gopkgString), filemode)
210-
if err != nil {
211-
t.Fatalf("failed to write updated Gopkg.toml: %v", err)
212-
}
213-
t.Logf("Gopkg.toml: %v", gopkgString)
214-
} else {
215-
t.Fatal("could not find sha of PR")
216-
}
217-
}
218188
cmdOut, err = exec.Command("dep", "ensure").CombinedOutput()
219189
if err != nil {
220190
t.Fatalf("dep ensure failed: %v\nCommand Output:\n%v", err, string(cmdOut))

0 commit comments

Comments
 (0)