Skip to content

Commit

Permalink
fix: added checkout_handler test
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ committed Aug 14, 2024
1 parent 12b9590 commit 79c79dd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
7 changes: 4 additions & 3 deletions internal/vcs/checkout_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,15 @@ func (ch *CheckoutHandler) CheckoutBaseBranch(logger *zerolog.Logger, folderPath
logger.Error().Err(err).Msg("Failed to create tmp directory for base branch")
return err
}
ch.baseFolderPath = baseBranchFolderPath

repo, err := Clone(logger, folderPath, baseBranchFolderPath, baseBranchName)

if err != nil {
logger.Error().Err(err).Msg("Failed to clone base branch")
return err
}
ch.repository = repo

ch.cleanupFunc = func() {
cleanupFunc := func() {
if baseBranchFolderPath == "" {
return
}
Expand All @@ -87,5 +85,8 @@ func (ch *CheckoutHandler) CheckoutBaseBranch(logger *zerolog.Logger, folderPath
}
}

ch.baseFolderPath = baseBranchFolderPath
ch.repository = repo
ch.cleanupFunc = cleanupFunc
return nil
}
80 changes: 80 additions & 0 deletions internal/vcs/checkout_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* © 2024 Snyk Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package vcs

import (
"github.com/snyk/snyk-ls/internal/testutil"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCheckoutHandler_ShouldCheckout(t *testing.T) {
c := testutil.UnitTest(t)
repoPath := t.TempDir()
_, _ = initGitRepo(t, repoPath, false)
ch := NewCheckoutHandler()

err := ch.CheckoutBaseBranch(c.Logger(), repoPath)

assert.NotNil(t, ch.CleanupFunc())
assert.NotNil(t, ch.Repo())
assert.NotEmpty(t, ch.BaseFolderPath())

ch.CleanupFunc()()
assert.NoError(t, err)
}

func TestCheckoutHandler_InvalidGitRepo(t *testing.T) {
c := testutil.UnitTest(t)
repoPath := t.TempDir()
ch := NewCheckoutHandler()

err := ch.CheckoutBaseBranch(c.Logger(), repoPath)
assert.Error(t, err)
assert.Nil(t, ch.CleanupFunc())
assert.Nil(t, ch.Repo())
assert.Empty(t, ch.BaseFolderPath())
}

func TestCheckoutHandler_AlreadyCreated(t *testing.T) {
c := testutil.UnitTest(t)
repoPath := t.TempDir()
_, _ = initGitRepo(t, repoPath, false)

ch := NewCheckoutHandler()

err := ch.CheckoutBaseBranch(c.Logger(), repoPath)
assert.Error(t, err)
assert.NotNil(t, ch.CleanupFunc())
assert.NotNil(t, ch.Repo())
assert.NotEmpty(t, ch.BaseFolderPath())

firstRunPath := ch.BaseFolderPath()
firstRunRepo := ch.Repo()
firstRunCleanupFunc := ch.CleanupFunc()

err = ch.CheckoutBaseBranch(c.Logger(), repoPath)
assert.Error(t, err)
assert.NotNil(t, ch.CleanupFunc())
assert.NotEmpty(t, ch.Repo())
assert.NotEmpty(t, ch.BaseFolderPath())

assert.Equal(t, firstRunPath, firstRunRepo)
assert.Equal(t, firstRunPath, firstRunPath)
assert.Equal(t, firstRunCleanupFunc, firstRunCleanupFunc)
ch.CleanupFunc()()
}

0 comments on commit 79c79dd

Please sign in to comment.