github.com/jfrog/jfrog-client-go@v1.40.2/utils/vcsdetails_test.go (about)

     1  package utils
     2  
     3  import (
     4  	biutils "github.com/jfrog/build-info-go/utils"
     5  	testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
     6  	"github.com/stretchr/testify/assert"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
    11  )
    12  
    13  func TestVcsDetails(t *testing.T) {
    14  	// Test the following .git types, on their corresponding paths in testdata.
    15  	testRuns := []string{"vcs", "packedvcs", "submodule", "worktree"}
    16  	for _, test := range testRuns {
    17  		t.Run(test, func(t *testing.T) {
    18  			var projectPath, tmpDir string
    19  			// Create temp folder.
    20  			tmpDir, err := fileutils.CreateTempDir()
    21  			assert.NoError(t, err, "Couldn't create temp dir")
    22  			defer func() {
    23  				assert.NoError(t, fileutils.RemoveTempDir(tmpDir), "Couldn't remove temp dir")
    24  			}()
    25  
    26  			switch test {
    27  			case "submodule":
    28  				projectPath = testsutils.InitVcsSubmoduleTestDir(t, filepath.Join("testdata", test), tmpDir)
    29  			case "worktree":
    30  				projectPath = testsutils.InitVcsWorktreeTestDir(t, filepath.Join("testdata", test), tmpDir)
    31  			default:
    32  				projectPath = initVcsTestDir(t, filepath.Join("testdata", test), tmpDir)
    33  			}
    34  			vcsDetails := NewVcsDetails()
    35  			revision, url, branch, err := vcsDetails.GetVcsDetails(projectPath)
    36  			assert.NoError(t, err)
    37  			assert.Equal(t, "https://github.com/jfrog/jfrog-cli.git", url)
    38  			assert.Equal(t, "6198a6294722fdc75a570aac505784d2ec0d1818", revision)
    39  			assert.Equal(t, "master", branch)
    40  		})
    41  	}
    42  }
    43  
    44  func initVcsTestDir(t *testing.T, srcPath, tmpDir string) (projectPath string) {
    45  	var err error
    46  	assert.NoError(t, biutils.CopyDir(srcPath, tmpDir, true, nil))
    47  	if found, err := fileutils.IsDirExists(filepath.Join(tmpDir, "gitdata"), false); found {
    48  		assert.NoError(t, err)
    49  		assert.NoError(t, fileutils.RenamePath(filepath.Join(tmpDir, "gitdata"), filepath.Join(tmpDir, ".git")))
    50  	}
    51  	if found, err := fileutils.IsDirExists(filepath.Join(tmpDir, "othergit", "gitdata"), false); found {
    52  		assert.NoError(t, err)
    53  		assert.NoError(t, fileutils.RenamePath(filepath.Join(tmpDir, "othergit", "gitdata"), filepath.Join(tmpDir, "othergit", ".git")))
    54  	}
    55  	projectPath, err = filepath.Abs(tmpDir)
    56  	assert.NoError(t, err)
    57  	return projectPath
    58  }