Skip to content

Commit 0108c16

Browse files
authored
fix: use git hash for upgrade test checkouts (#109)
* fix: remove fetching of HEAD for upgrade test Removing the fetch of HEAD, as it is also creating a local branch called HEAD which will cause git to complain about ambiguous HEAD references (HEAD as a "current branch pointer" and a local branch name). In testing was found it was not necessary for functionality. * fix: use git hash for upgrade test In upgrade test, use the hash of the original branch to switch back after checkout of master/main. This fixes an issue where the original PR branch is a detached HEAD reference, and does not have a suitable "branch name" to checkout at end of test, but the hash will always work.
1 parent 0a22f25 commit 0108c16

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

testhelper/tests.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ func (options *TestOptions) RunTestUpgrade() (*terraform.PlanStruct, error) {
188188
gitRoot, _ := GitRootPath(options.TerraformDir)
189189
gitRepo, _ := git.PlainOpen(gitRoot)
190190

191-
ref, _ := gitRepo.Head()
192-
prBranch := ref.Name()
193-
logger.Log(options.Testing, "Current Branch (PR): "+prBranch)
191+
// maintain a reference of current checkout, which might be a detatched PR merge, will be used to switch back later
192+
prRef, _ := gitRepo.Head()
193+
logger.Log(options.Testing, "Current Branch [Name - Hash]:", prRef.Name(), "-", prRef.Hash())
194194

195195
// fetch to ensure all branches are present
196196
remote, err := gitRepo.Remote("origin")
@@ -199,7 +199,7 @@ func (options *TestOptions) RunTestUpgrade() (*terraform.PlanStruct, error) {
199199
}
200200

201201
opts := &git.FetchOptions{
202-
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
202+
RefSpecs: []config.RefSpec{"refs/*:refs/*"},
203203
}
204204

205205
if err := remote.Fetch(opts); err != nil {
@@ -240,14 +240,19 @@ func (options *TestOptions) RunTestUpgrade() (*terraform.PlanStruct, error) {
240240

241241
// Only proceed to upgrade Test of master branch apply passed
242242
if resultErr == nil {
243-
logger.Log(options.Testing, "Attempting Git Checkout PR Branch: ", prBranch)
243+
logger.Log(options.Testing, "Attempting Git Checkout PR Branch:", prRef.Name(), "-", prRef.Hash())
244+
// checkout the HASH of original (PR) branch.
245+
// NOTE: in automation the original checkout branch is detached and points to the pseudo merge of the PR.
246+
// These detached merge branches report their name as "HEAD" which is not a suitable checkout value.
247+
// The solution here is to do this final checkout on the HASH of the original branch which will work
248+
// with both detached and normal branches.
244249
resultErr = w.Checkout(&git.CheckoutOptions{
245-
Branch: prBranch,
246-
Force: true})
250+
Hash: prRef.Hash(),
251+
Force: true})
247252
assert.Nilf(options.Testing, resultErr, "Could Not Checkout PR Branch")
248253
if resultErr == nil {
249254
cur, _ = gitRepo.Head()
250-
logger.Log(options.Testing, "Current Branch (PR): "+cur.Name())
255+
logger.Log(options.Testing, "Current Branch (PR):", cur.Name(), "-", cur.Hash())
251256

252257
// Plan needs a temp file to store plan in
253258
tmpPlanFile, _ := os.CreateTemp(options.TerraformDir, "terratest-plan-file-")

0 commit comments

Comments
 (0)