github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/pkg/git/git_test.go (about)

     1  package git
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"gotest.tools/v3/assert"
    10  )
    11  
    12  func TestWithGitOptions(t *testing.T) {
    13  	cloneOpts := &CloneOptions{}
    14  	WithRepoURL("test_url")(cloneOpts)
    15  	assert.Equal(t, cloneOpts.RepoURL, "test_url")
    16  	WithBranch("test_branch")(cloneOpts)
    17  	assert.Equal(t, cloneOpts.Branch, "test_branch")
    18  	WithCommit("test_commit")(cloneOpts)
    19  	assert.Equal(t, cloneOpts.Commit, "test_commit")
    20  	WithTag("test_tag")(cloneOpts)
    21  	assert.Equal(t, cloneOpts.Tag, "test_tag")
    22  	WithLocalPath("test_local_path")(cloneOpts)
    23  	assert.Equal(t, cloneOpts.LocalPath, "test_local_path")
    24  	WithWriter(nil)(cloneOpts)
    25  	assert.Equal(t, cloneOpts.Writer, nil)
    26  }
    27  
    28  func TestNewCloneOptions(t *testing.T) {
    29  	cloneOpts := NewCloneOptions("https://github.com/kcl-lang/kcl", "", "v1.0.0", "", "", nil)
    30  	assert.Equal(t, cloneOpts.RepoURL, "https://github.com/kcl-lang/kcl")
    31  	assert.Equal(t, cloneOpts.Tag, "v1.0.0")
    32  	assert.Equal(t, cloneOpts.Commit, "")
    33  	assert.Equal(t, cloneOpts.Branch, "")
    34  	assert.Equal(t, cloneOpts.LocalPath, "")
    35  	assert.Equal(t, cloneOpts.Writer, nil)
    36  }
    37  
    38  func TestValidateGitOptions(t *testing.T) {
    39  	cloneOpts := &CloneOptions{}
    40  	WithBranch("test_branch")(cloneOpts)
    41  	err := cloneOpts.Validate()
    42  	assert.Equal(t, err, nil)
    43  	WithCommit("test_commit")(cloneOpts)
    44  	err = cloneOpts.Validate()
    45  	assert.Equal(t, err.Error(), "only one of branch, tag or commit is allowed")
    46  }
    47  
    48  func TestCloneWithOptions(t *testing.T) {
    49  	var buf bytes.Buffer
    50  
    51  	tmpdir, err := os.MkdirTemp("", "git")
    52  	tmpdir = filepath.Join(tmpdir, "git")
    53  	assert.Equal(t, err, nil)
    54  	defer func() {
    55  		rErr := os.RemoveAll(tmpdir)
    56  		assert.Equal(t, rErr, nil)
    57  	}()
    58  
    59  	repo, err := CloneWithOpts(
    60  		WithRepoURL("https://github.com/KusionStack/catalog.git"),
    61  		WithCommit("4e59d5852cd7"),
    62  		WithWriter(&buf),
    63  		WithLocalPath(tmpdir),
    64  	)
    65  	assert.Equal(t, err, nil)
    66  
    67  	head, err := repo.Head()
    68  	assert.Equal(t, err, nil)
    69  	assert.Equal(t, head.Hash().String(), "4e59d5852cd76542f9f0ec65e5773ca9f4e02462")
    70  	assert.Equal(t, err, nil)
    71  }