Skip to content

Commit 4cbacd0

Browse files
aryan9600hiddeco
authored andcommitted
gitrepo: add tests for reference name checkout strategy
Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent d32470e commit 4cbacd0

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

controllers/gitrepository_controller_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
600600

601601
branches := []string{"staging"}
602602
tags := []string{"non-semver-tag", "v0.1.0", "0.2.0", "v0.2.1", "v1.0.0-alpha", "v1.1.0", "v2.0.0"}
603+
refs := []string{"refs/pull/420/head"}
603604

604605
tests := []struct {
605606
name string
@@ -645,6 +646,24 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
645646
wantRevision: "staging@sha1:<commit>",
646647
wantReconciling: true,
647648
},
649+
{
650+
name: "Ref Name pointing to a branch",
651+
reference: &sourcev1.GitRepositoryRef{
652+
Name: "refs/heads/staging",
653+
},
654+
want: sreconcile.ResultSuccess,
655+
wantRevision: "refs/heads/staging@sha1:<commit>",
656+
wantReconciling: true,
657+
},
658+
{
659+
name: "Ref Name pointing to a PR",
660+
reference: &sourcev1.GitRepositoryRef{
661+
Name: "refs/pull/420/head",
662+
},
663+
want: sreconcile.ResultSuccess,
664+
wantRevision: "refs/pull/420/head@sha1:<commit>",
665+
wantReconciling: true,
666+
},
648667
{
649668
name: "SemVer",
650669
reference: &sourcev1.GitRepositoryRef{
@@ -801,6 +820,10 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
801820
g.Expect(remoteTagForHead(localRepo, headRef, tag)).To(Succeed())
802821
}
803822

823+
for _, ref := range refs {
824+
g.Expect(remoteRefForHead(localRepo, headRef, ref)).To(Succeed())
825+
}
826+
804827
r := &GitRepositoryReconciler{
805828
Client: fakeclient.NewClientBuilder().WithScheme(testEnv.GetScheme()).Build(),
806829
EventRecorder: record.NewFakeRecorder(32),
@@ -854,7 +877,7 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
854877
g.Expect(got).To(Equal(tt.want))
855878
if tt.wantRevision != "" && !tt.wantErr {
856879
revision := strings.ReplaceAll(tt.wantRevision, "<commit>", headRef.Hash().String())
857-
g.Expect(commit.String()).To(Equal(revision))
880+
g.Expect(commitReference(obj, &commit)).To(Equal(revision))
858881
g.Expect(conditions.IsTrue(obj, sourcev1.ArtifactOutdatedCondition)).To(Equal(tt.wantArtifactOutdated))
859882
g.Expect(conditions.IsTrue(obj, meta.ReconcilingCondition)).To(Equal(tt.wantReconciling))
860883
}
@@ -1888,6 +1911,20 @@ func remoteTagForHead(repo *gogit.Repository, head *plumbing.Reference, tag stri
18881911
})
18891912
}
18901913

1914+
func remoteRefForHead(repo *gogit.Repository, head *plumbing.Reference, reference string) error {
1915+
if err := repo.Storer.SetReference(plumbing.NewHashReference(plumbing.ReferenceName(reference), head.Hash())); err != nil {
1916+
return err
1917+
}
1918+
if err := repo.Push(&gogit.PushOptions{
1919+
RefSpecs: []config.RefSpec{
1920+
config.RefSpec("+" + reference + ":" + reference),
1921+
},
1922+
}); err != nil {
1923+
return err
1924+
}
1925+
return nil
1926+
}
1927+
18911928
func TestGitRepositoryReconciler_statusConditions(t *testing.T) {
18921929
tests := []struct {
18931930
name string

0 commit comments

Comments
 (0)