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

     1  package utils
     2  
     3  import (
     4  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
     5  	"github.com/stretchr/testify/assert"
     6  	"os"
     7  	"path/filepath"
     8  	"strconv"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  type gitExecutor struct {
    15  	gitManager GitManager
    16  }
    17  
    18  func NewGitExecutor(dotGitPath string) *gitExecutor {
    19  	return &gitExecutor{gitManager: *NewGitManager(dotGitPath)}
    20  }
    21  
    22  func (m *gitExecutor) execGit(args ...string) (string, string, error) {
    23  	return m.gitManager.ExecGit(args...)
    24  }
    25  func (m *gitExecutor) GetUrl() (string, string, error) {
    26  	return m.execGit("config", "--get", "remote.origin.url")
    27  }
    28  
    29  func (m *gitExecutor) GetRevision() (string, string, error) {
    30  	return m.execGit("show", "-s", "--format=%H", "HEAD")
    31  }
    32  
    33  func (m *gitExecutor) GetBranch() (string, string, error) {
    34  	return m.execGit("branch", "--show-current")
    35  }
    36  
    37  func (m *gitExecutor) GetMessage(revision string) (string, string, error) {
    38  	return m.execGit("show", "-s", "--format=%B", revision)
    39  }
    40  
    41  func TestReadConfig(t *testing.T) {
    42  	testReadConfig(t)
    43  }
    44  
    45  // Open a git repo using 'go-git' package fails when:
    46  //  1. OS is Windows.
    47  //  2. using go-git v4.7.0.
    48  //  3. .git/config file contains path with backslashes.
    49  func TestReadConfigWithBackslashes(t *testing.T) {
    50  	dotGitPath := getDotGitPath(t)
    51  	gitExec := NewGitExecutor(dotGitPath)
    52  	timestamp := strconv.FormatInt(time.Now().Unix(), 10)
    53  	_, _, err := gitExec.execGit("config", "--local", "--add", "http.https://github.com.sslCAInfo"+timestamp, dotGitPath)
    54  	assert.NoError(t, err)
    55  	defer func() {
    56  		_, _, err = gitExec.execGit("config", "--local", "--unset", "http.https://github.com.sslCAInfo"+timestamp)
    57  		assert.NoError(t, err)
    58  	}()
    59  	testReadConfig(t)
    60  }
    61  
    62  func testReadConfig(t *testing.T) {
    63  	dotGitPath := getDotGitPath(t)
    64  	gitManager := NewGitManager(dotGitPath)
    65  	err := gitManager.ReadConfig()
    66  	assert.NoError(t, err)
    67  
    68  	gitExecutor := NewGitExecutor(dotGitPath)
    69  	url, _, err := gitExecutor.GetUrl()
    70  	assert.NoError(t, err)
    71  	if !strings.HasSuffix(url, ".git") {
    72  		url += ".git"
    73  	}
    74  	assert.Equal(t, url, gitManager.GetUrl(), "Wrong url")
    75  	revision, _, err := gitExecutor.GetRevision()
    76  	assert.NoError(t, err)
    77  	assert.Equal(t, revision, gitManager.GetRevision(), "Wrong revision")
    78  	branch, _, err := gitExecutor.GetBranch()
    79  	assert.NoError(t, err)
    80  	assert.Equal(t, branch, gitManager.GetBranch(), "Wrong branch")
    81  	message, _, err := gitExecutor.GetMessage(revision)
    82  	assert.NoError(t, err)
    83  	assert.Equal(t, message, gitManager.GetMessage(), "Wrong message")
    84  }
    85  
    86  func getDotGitPath(t *testing.T) string {
    87  	dotGitPath, err := os.Getwd()
    88  	assert.NoError(t, err, "Failed to get current dir")
    89  	dotGitPath = filepath.Dir(dotGitPath)
    90  	dotGitExists, err := fileutils.IsDirExists(filepath.Join(dotGitPath, ".git"), false)
    91  	assert.NoError(t, err)
    92  	assert.True(t, dotGitExists, "Can't find .git")
    93  	return dotGitPath
    94  }