github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/gitlfsclean_test.go (about)

     1  package services
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/cobalt77/jfrog-client-go/utils"
    10  	"github.com/cobalt77/jfrog-client-go/utils/io/fileutils"
    11  	"github.com/cobalt77/jfrog-client-go/utils/log"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestExtractRepo(t *testing.T) {
    16  	pwd, err := os.Getwd()
    17  	assert.NoError(t, err)
    18  	testPath := filepath.Join(pwd, "testdata", "gitlfs")
    19  	repo, err := extractRepo(testPath, "lfsConfigExample", "https://localhost:8080/artifactory", lfsConfigUrlExtractor)
    20  	if err != nil {
    21  		t.Error("Got err: ", err)
    22  	}
    23  	if repo != "lfs-local" {
    24  		t.Error("Failed to extract repo from .lfsconfig file format. Expected: \"lfs-local\" Got: ", repo)
    25  
    26  	}
    27  	repo, err = extractRepo(testPath, "configExample", "http://localhost:8081/artifactory", configLfsUrlExtractor)
    28  	if err != nil {
    29  		t.Error("Got err: ", err)
    30  	}
    31  	if repo != "lfs-local" {
    32  		t.Error("Failed to extract repo from .git/config file format. Expected: \"lfs-local\" Got: ", repo)
    33  	}
    34  }
    35  
    36  func TestGetLfsFilesFromGit(t *testing.T) {
    37  	log.SetLogger(log.NewLogger(log.DEBUG, nil))
    38  	fileId := "4bf4c8c0fef3f5c8cf6f255d1c784377138588c0a9abe57e440bce3ccb350c2e"
    39  	gitPath := getCliDotGitPath(t)
    40  	refs := strings.Join([]string{"refs", "heads", "*"}, "/")
    41  	if utils.IsWindows() {
    42  		refs = strings.Join([]string{"refs", "heads", "*"}, "\\\\")
    43  	}
    44  	results, err := getLfsFilesFromGit(gitPath, "HEAD|"+refs)
    45  	if err != nil {
    46  		t.Error("Got err: ", err)
    47  	}
    48  	_, ok := results[fileId]
    49  	if !ok {
    50  		t.Error("couldn't find test.bin test file")
    51  	}
    52  }
    53  
    54  func getCliDotGitPath(t *testing.T) string {
    55  	workingDir, err := os.Getwd()
    56  	if err != nil {
    57  		t.Error("Failed to get current dir.")
    58  	}
    59  	dotGitPath := filepath.Join(workingDir, "..", "..")
    60  	dotGitExists, err := fileutils.IsDirExists(filepath.Join(dotGitPath, ".git"), false)
    61  	if err != nil {
    62  		t.Error(err)
    63  	}
    64  	if !dotGitExists {
    65  		t.Error("Can't find .git")
    66  	}
    67  	return dotGitPath
    68  }