-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(boot): Try to update local boot clone to config ref from versions
This will check if the boot clone isn't using the ref the desired tag points to, after cloning the boot config if needed. If the clone is using a different ref, it will try to stash any local changes, update the clone to use the appropriate ref, and unstash those local changes. If there are errors in the unstash due to conflicts, it'll report the `git stash pop` error message and tell the user what they need to do to fix it. fixes #5929 Signed-off-by: Andrew Bayer <[email protected]>
- Loading branch information
1 parent
d8c3e03
commit 006bdea
Showing
3 changed files
with
302 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
// +build integration | ||
|
||
package boot | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/acarl005/stripansi" | ||
"github.com/jenkins-x/jx/pkg/cmd/opts" | ||
"github.com/jenkins-x/jx/pkg/gits" | ||
"github.com/jenkins-x/jx/pkg/log" | ||
"github.com/jenkins-x/jx/pkg/util" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
FirstTag = "v1.0.0" | ||
SecondTag = "v2.0.0" | ||
|
||
testFileName = "some-file" | ||
) | ||
|
||
func TestUpdateBootCloneIfOutOfDate_Conflicts(t *testing.T) { | ||
gitter := gits.NewGitCLI() | ||
|
||
repoDir, err := initializeTempGitRepo(gitter) | ||
assert.NoError(t, err) | ||
|
||
defer func() { | ||
err := os.RemoveAll(repoDir) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
testDir, err := ioutil.TempDir("", "update-local-boot-clone-test-clone-") | ||
assert.NoError(t, err) | ||
defer func() { | ||
err := os.RemoveAll(testDir) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
err = gitter.Clone(repoDir, testDir) | ||
assert.NoError(t, err) | ||
|
||
err = gitter.FetchTags(testDir) | ||
assert.NoError(t, err) | ||
|
||
err = gitter.Reset(testDir, FirstTag, true) | ||
assert.NoError(t, err) | ||
|
||
conflictingContent := []byte("something else") | ||
err = ioutil.WriteFile(filepath.Join(testDir, testFileName), conflictingContent, util.DefaultWritePermissions) | ||
assert.NoError(t, err) | ||
|
||
o := &BootOptions{ | ||
CommonOptions: &opts.CommonOptions{}, | ||
Dir: testDir, | ||
} | ||
r, fakeStdout, _ := os.Pipe() | ||
log.SetOutput(fakeStdout) | ||
o.CommonOptions.Out = fakeStdout | ||
|
||
err = o.updateBootCloneIfOutOfDate(SecondTag) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "Could not update local boot clone due to conflicts between local changes and v2.0.0.") | ||
|
||
fakeStdout.Close() | ||
outBytes, _ := ioutil.ReadAll(r) | ||
r.Close() | ||
output := stripansi.Strip(string(outBytes)) | ||
assert.Contains(t, output, "It is based on v1.0.0, but the version stream is using v2.0.0.") | ||
} | ||
|
||
func TestUpdateBootCloneIfOutOfDate_NoConflicts(t *testing.T) { | ||
gitter := gits.NewGitCLI() | ||
|
||
repoDir, err := initializeTempGitRepo(gitter) | ||
assert.NoError(t, err) | ||
|
||
defer func() { | ||
err := os.RemoveAll(repoDir) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
testDir, err := ioutil.TempDir("", "update-local-boot-clone-test-clone-") | ||
assert.NoError(t, err) | ||
defer func() { | ||
err := os.RemoveAll(testDir) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
err = gitter.Clone(repoDir, testDir) | ||
assert.NoError(t, err) | ||
|
||
err = gitter.FetchTags(testDir) | ||
assert.NoError(t, err) | ||
|
||
err = gitter.Reset(testDir, FirstTag, true) | ||
assert.NoError(t, err) | ||
|
||
conflictingContent := []byte("something else") | ||
err = ioutil.WriteFile(filepath.Join(testDir, "some-other-file"), conflictingContent, util.DefaultWritePermissions) | ||
assert.NoError(t, err) | ||
|
||
o := &BootOptions{ | ||
CommonOptions: &opts.CommonOptions{}, | ||
Dir: testDir, | ||
} | ||
r, fakeStdout, _ := os.Pipe() | ||
log.SetOutput(fakeStdout) | ||
o.CommonOptions.Out = fakeStdout | ||
|
||
err = o.updateBootCloneIfOutOfDate(SecondTag) | ||
assert.NoError(t, err) | ||
|
||
fakeStdout.Close() | ||
outBytes, _ := ioutil.ReadAll(r) | ||
r.Close() | ||
output := stripansi.Strip(string(outBytes)) | ||
assert.Contains(t, output, "It is based on v1.0.0, but the version stream is using v2.0.0.") | ||
} | ||
|
||
func TestUpdateBootCloneIfOutOfDate_UpToDate(t *testing.T) { | ||
gitter := gits.NewGitCLI() | ||
|
||
repoDir, err := initializeTempGitRepo(gitter) | ||
assert.NoError(t, err) | ||
|
||
defer func() { | ||
err := os.RemoveAll(repoDir) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
testDir, err := ioutil.TempDir("", "update-local-boot-clone-test-clone-") | ||
assert.NoError(t, err) | ||
defer func() { | ||
err := os.RemoveAll(testDir) | ||
assert.NoError(t, err) | ||
}() | ||
|
||
err = gitter.Clone(repoDir, testDir) | ||
assert.NoError(t, err) | ||
|
||
err = gitter.FetchTags(testDir) | ||
assert.NoError(t, err) | ||
|
||
err = gitter.Reset(testDir, SecondTag, true) | ||
assert.NoError(t, err) | ||
|
||
conflictingContent := []byte("something else") | ||
err = ioutil.WriteFile(filepath.Join(testDir, "some-other-file"), conflictingContent, util.DefaultWritePermissions) | ||
assert.NoError(t, err) | ||
|
||
o := &BootOptions{ | ||
CommonOptions: &opts.CommonOptions{}, | ||
Dir: testDir, | ||
} | ||
r, fakeStdout, _ := os.Pipe() | ||
log.SetOutput(fakeStdout) | ||
o.CommonOptions.Out = fakeStdout | ||
|
||
err = o.updateBootCloneIfOutOfDate(SecondTag) | ||
assert.NoError(t, err) | ||
|
||
fakeStdout.Close() | ||
outBytes, _ := ioutil.ReadAll(r) | ||
r.Close() | ||
output := stripansi.Strip(string(outBytes)) | ||
assert.Equal(t, "", output) | ||
} | ||
|
||
func initializeTempGitRepo(gitter gits.Gitter) (string, error) { | ||
dir, err := ioutil.TempDir("", "update-local-boot-clone-test-repo-") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = gitter.Init(dir) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = gitter.AddCommit(dir, "Initial Commit") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
testFile := filepath.Join(dir, testFileName) | ||
testFileContents := []byte("foo") | ||
err = ioutil.WriteFile(testFile, testFileContents, util.DefaultWritePermissions) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = gitter.Add(dir, ".") | ||
if err != nil { | ||
return "", err | ||
} | ||
err = gitter.AddCommit(dir, "Adding foo") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = gitter.CreateTag(dir, FirstTag, "First Tag") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
testFileContents = []byte("bar") | ||
err = ioutil.WriteFile(testFile, testFileContents, util.DefaultWritePermissions) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = gitter.AddCommit(dir, "Adding bar") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = gitter.CreateTag(dir, SecondTag, "Second Tag") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return dir, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters