@@ -547,30 +547,35 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
547547 name string
548548 skipForImplementation string
549549 reference * sourcev1.GitRepositoryRef
550+ beforeFunc func (obj * sourcev1.GitRepository , latestRev string )
550551 want sreconcile.Result
551552 wantErr bool
552553 wantRevision string
554+ wantArtifactOutdated bool
553555 }{
554556 {
555- name : "Nil reference (default branch)" ,
556- want : sreconcile .ResultSuccess ,
557- wantRevision : "master/<commit>" ,
557+ name : "Nil reference (default branch)" ,
558+ want : sreconcile .ResultSuccess ,
559+ wantRevision : "master/<commit>" ,
560+ wantArtifactOutdated : true ,
558561 },
559562 {
560563 name : "Branch" ,
561564 reference : & sourcev1.GitRepositoryRef {
562565 Branch : "staging" ,
563566 },
564- want : sreconcile .ResultSuccess ,
565- wantRevision : "staging/<commit>" ,
567+ want : sreconcile .ResultSuccess ,
568+ wantRevision : "staging/<commit>" ,
569+ wantArtifactOutdated : true ,
566570 },
567571 {
568572 name : "Tag" ,
569573 reference : & sourcev1.GitRepositoryRef {
570574 Tag : "v0.1.0" ,
571575 },
572- want : sreconcile .ResultSuccess ,
573- wantRevision : "v0.1.0/<commit>" ,
576+ want : sreconcile .ResultSuccess ,
577+ wantRevision : "v0.1.0/<commit>" ,
578+ wantArtifactOutdated : true ,
574579 },
575580 {
576581 name : "Branch commit" ,
@@ -579,8 +584,9 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
579584 Branch : "staging" ,
580585 Commit : "<commit>" ,
581586 },
582- want : sreconcile .ResultSuccess ,
583- wantRevision : "staging/<commit>" ,
587+ want : sreconcile .ResultSuccess ,
588+ wantRevision : "staging/<commit>" ,
589+ wantArtifactOutdated : true ,
584590 },
585591 {
586592 name : "Branch commit" ,
@@ -589,32 +595,56 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
589595 Branch : "staging" ,
590596 Commit : "<commit>" ,
591597 },
592- want : sreconcile .ResultSuccess ,
593- wantRevision : "HEAD/<commit>" ,
598+ want : sreconcile .ResultSuccess ,
599+ wantRevision : "HEAD/<commit>" ,
600+ wantArtifactOutdated : true ,
594601 },
595602 {
596603 name : "SemVer" ,
597604 reference : & sourcev1.GitRepositoryRef {
598605 SemVer : "*" ,
599606 },
600- want : sreconcile .ResultSuccess ,
601- wantRevision : "v2.0.0/<commit>" ,
607+ want : sreconcile .ResultSuccess ,
608+ wantRevision : "v2.0.0/<commit>" ,
609+ wantArtifactOutdated : true ,
602610 },
603611 {
604612 name : "SemVer range" ,
605613 reference : & sourcev1.GitRepositoryRef {
606614 SemVer : "<v0.2.1" ,
607615 },
608- want : sreconcile .ResultSuccess ,
609- wantRevision : "0.2.0/<commit>" ,
616+ want : sreconcile .ResultSuccess ,
617+ wantRevision : "0.2.0/<commit>" ,
618+ wantArtifactOutdated : true ,
610619 },
611620 {
612621 name : "SemVer prerelease" ,
613622 reference : & sourcev1.GitRepositoryRef {
614623 SemVer : ">=1.0.0-0 <1.1.0-0" ,
615624 },
616- wantRevision : "v1.0.0-alpha/<commit>" ,
617- want : sreconcile .ResultSuccess ,
625+ wantRevision : "v1.0.0-alpha/<commit>" ,
626+ want : sreconcile .ResultSuccess ,
627+ wantArtifactOutdated : true ,
628+ },
629+ {
630+ name : "Optimized clone" ,
631+ reference : & sourcev1.GitRepositoryRef {
632+ Branch : "staging" ,
633+ },
634+ beforeFunc : func (obj * sourcev1.GitRepository , latestRev string ) {
635+ // Add existing artifact on the object and storage.
636+ obj .Status = sourcev1.GitRepositoryStatus {
637+ Artifact : & sourcev1.Artifact {
638+ Revision : "staging/" + latestRev ,
639+ Path : randStringRunes (10 ),
640+ },
641+ }
642+ testStorage .Archive (obj .GetArtifact (), "testdata/git/repository" , nil )
643+ conditions .MarkTrue (obj , sourcev1 .ArtifactInStorageCondition , meta .SucceededReason , "foo" )
644+ },
645+ want : sreconcile .ResultSuccess ,
646+ wantRevision : "staging/<commit>" ,
647+ wantArtifactOutdated : false ,
618648 },
619649 }
620650
@@ -677,6 +707,10 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
677707 obj := obj .DeepCopy ()
678708 obj .Spec .GitImplementation = i
679709
710+ if tt .beforeFunc != nil {
711+ tt .beforeFunc (obj , headRef .Hash ().String ())
712+ }
713+
680714 var commit git.Commit
681715 var includes artifactSet
682716 got , err := r .reconcileSource (ctx , obj , & commit , & includes , tmpDir )
@@ -685,10 +719,10 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
685719 }
686720 g .Expect (err != nil ).To (Equal (tt .wantErr ))
687721 g .Expect (got ).To (Equal (tt .want ))
688- if tt .wantRevision != "" {
722+ if tt .wantRevision != "" && ! tt . wantErr {
689723 revision := strings .ReplaceAll (tt .wantRevision , "<commit>" , headRef .Hash ().String ())
690724 g .Expect (commit .String ()).To (Equal (revision ))
691- g .Expect (conditions .IsTrue (obj , sourcev1 .ArtifactOutdatedCondition )).To (BeTrue ( ))
725+ g .Expect (conditions .IsTrue (obj , sourcev1 .ArtifactOutdatedCondition )).To (Equal ( tt . wantArtifactOutdated ))
692726 }
693727 })
694728 }
0 commit comments