Merge pull request #755 from aryan9600/optimized-checkout-tag

This commit is contained in:
Hidde Beydals 2022-06-03 10:56:11 +02:00 committed by GitHub
commit c10c03180d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 1 deletions

View File

@ -48,7 +48,10 @@ func CheckoutStrategyForOptions(ctx context.Context, opt git.CheckoutOptions) gi
case opt.SemVer != "":
return &CheckoutSemVer{SemVer: opt.SemVer}
case opt.Tag != "":
return &CheckoutTag{Tag: opt.Tag}
return &CheckoutTag{
Tag: opt.Tag,
LastRevision: opt.LastRevision,
}
default:
branch := opt.Branch
if branch == "" {

View File

@ -498,3 +498,67 @@ func TestInitializeRepoWithRemote(t *testing.T) {
_, _, err = initializeRepoWithRemote(ctx, tmp, testRepoURL2, authOpts2)
g.Expect(err).To(HaveOccurred())
}
func TestCheckoutStrategyForOptions(t *testing.T) {
tests := []struct {
name string
opts git.CheckoutOptions
expectedStrat git.CheckoutStrategy
}{
{
name: "commit works",
opts: git.CheckoutOptions{
Commit: "commit",
},
expectedStrat: &CheckoutCommit{
Commit: "commit",
},
},
{
name: "semver works",
opts: git.CheckoutOptions{
SemVer: ">= 1.0.0",
},
expectedStrat: &CheckoutSemVer{
SemVer: ">= 1.0.0",
},
},
{
name: "tag with latest revision works",
opts: git.CheckoutOptions{
Tag: "v0.1.0",
LastRevision: "ar34oi2njrngjrng",
},
expectedStrat: &CheckoutTag{
Tag: "v0.1.0",
LastRevision: "ar34oi2njrngjrng",
},
},
{
name: "branch with latest revision works",
opts: git.CheckoutOptions{
Branch: "main",
LastRevision: "rrgij20mkmrg",
},
expectedStrat: &CheckoutBranch{
Branch: "main",
LastRevision: "rrgij20mkmrg",
},
},
{
name: "empty branch falls back to default",
opts: git.CheckoutOptions{},
expectedStrat: &CheckoutBranch{
Branch: git.DefaultBranch,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
strat := CheckoutStrategyForOptions(context.TODO(), tt.opts)
g.Expect(strat).To(Equal(tt.expectedStrat))
})
}
}