github.com/jfrog/jfrog-cli-core/v2@v2.51.0/utils/tests/test_utils.go (about) 1 package tests 2 3 import ( 4 "os" 5 "path/filepath" 6 "regexp" 7 "strconv" 8 "strings" 9 "testing" 10 "time" 11 12 biutils "github.com/jfrog/build-info-go/utils" 13 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 14 "github.com/jfrog/jfrog-client-go/utils/log" 15 "github.com/jfrog/jfrog-client-go/utils/tests" 16 xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 // Prepare the .git environment for the test. Takes an existing folder and making it .git dir. 21 // sourceDirPath - Relative path to the source dir to change to .git 22 // targetDirPath - Relative path to the target created .git dir, usually 'testdata' under the parent dir. 23 func PrepareDotGitDir(t *testing.T, sourceDirPath, targetDirPath string) (string, string) { 24 // Get path to create .git folder in 25 baseDir, _ := os.Getwd() 26 baseDir = filepath.Join(baseDir, targetDirPath) 27 // Create .git path and make sure it is clean 28 dotGitPath := filepath.Join(baseDir, ".git") 29 RemovePath(dotGitPath, t) 30 // Get the path of the .git candidate path 31 dotGitPathTest := filepath.Join(baseDir, sourceDirPath) 32 // Rename the .git candidate 33 RenamePath(dotGitPathTest, dotGitPath, t) 34 return baseDir, dotGitPath 35 } 36 37 // Removing the provided path from the filesystem 38 func RemovePath(testPath string, t *testing.T) { 39 err := fileutils.RemovePath(testPath) 40 if err != nil { 41 t.Error(err) 42 t.FailNow() 43 } 44 } 45 46 // Renaming from old path to new path. 47 func RenamePath(oldPath, newPath string, t *testing.T) { 48 err := fileutils.RenamePath(oldPath, newPath) 49 if err != nil { 50 t.Error(err) 51 t.FailNow() 52 } 53 } 54 55 func CreateTempDirWithCallbackAndAssert(t *testing.T) (string, func()) { 56 tempDirPath, err := fileutils.CreateTempDir() 57 assert.NoError(t, err, "Couldn't create temp dir") 58 return tempDirPath, func() { 59 assert.NoError(t, fileutils.RemoveTempDir(tempDirPath), "Couldn't remove temp dir") 60 } 61 } 62 63 // Clean items with timestamp older than 24 hours. Used to delete old repositories, builds, release bundles and Docker images. 64 // baseItemNames - The items to delete without timestamp, i.e. [cli-rt1, cli-rt2, ...] 65 // getActualItems - Function that returns all actual items in the remote server, i.e. [cli-rt1-1592990748, cli-rt2-1592990748, ...] 66 // deleteItem - Function that deletes the item by name 67 func CleanUpOldItems(baseItemNames []string, getActualItems func() ([]string, error), deleteItem func(string)) { 68 actualItems, err := getActualItems() 69 if err != nil { 70 log.Warn("Couldn't retrieve items", err) 71 return 72 } 73 now := time.Now() 74 for _, baseItemName := range baseItemNames { 75 itemPattern := regexp.MustCompile(`^` + baseItemName + `[\w-]*-(\d*)$`) 76 for _, item := range actualItems { 77 regexGroups := itemPattern.FindStringSubmatch(item) 78 if regexGroups == nil { 79 // Item does not match 80 continue 81 } 82 83 itemTimestamp, err := strconv.ParseInt(regexGroups[len(regexGroups)-1], 10, 64) 84 if err != nil { 85 log.Warn("Error while parsing timestamp of", item, err) 86 continue 87 } 88 89 itemTime := time.Unix(itemTimestamp, 0) 90 if now.Sub(itemTime).Hours() > 24 { 91 deleteItem(item) 92 } 93 } 94 } 95 } 96 97 func CreateTestWorkspace(t *testing.T, sourceDir string) (string, func()) { 98 tempDirPath, createTempDirCallback := CreateTempDirWithCallbackAndAssert(t) 99 assert.NoError(t, biutils.CopyDir(sourceDir, tempDirPath, true, nil)) 100 wd, err := os.Getwd() 101 assert.NoError(t, err, "Failed to get current dir") 102 chdirCallback := tests.ChangeDirWithCallback(t, wd, tempDirPath) 103 return tempDirPath, func() { 104 chdirCallback() 105 createTempDirCallback() 106 } 107 } 108 109 func GetAndAssertNode(t *testing.T, modules []*xrayUtils.GraphNode, moduleId string) *xrayUtils.GraphNode { 110 module := GetModule(modules, moduleId) 111 assert.NotNil(t, module, "Module '"+moduleId+"' doesn't exist") 112 return module 113 } 114 115 // GetModule gets a specific module from the provided modules list 116 func GetModule(modules []*xrayUtils.GraphNode, moduleId string) *xrayUtils.GraphNode { 117 for _, module := range modules { 118 splitIdentifier := strings.Split(module.Id, "//") 119 id := splitIdentifier[0] 120 if len(splitIdentifier) > 1 { 121 id = splitIdentifier[1] 122 } 123 if id == moduleId { 124 return module 125 } 126 } 127 return nil 128 }