github.com/mondo192/jfrog-client-go@v1.0.0/utils/tests/utils.go (about) 1 package tests 2 3 import ( 4 "bufio" 5 "github.com/mondo192/jfrog-client-go/utils/io/fileutils" 6 "github.com/mondo192/jfrog-client-go/utils/log" 7 "github.com/stretchr/testify/assert" 8 "net" 9 "net/http" 10 "os" 11 "os/exec" 12 "path/filepath" 13 "strings" 14 "testing" 15 ) 16 17 type HttpServerHandlers map[string]func(w http.ResponseWriter, r *http.Request) 18 19 func StartHttpServer(handlers HttpServerHandlers) (int, error) { 20 listener, err := net.Listen("tcp", ":0") 21 if err != nil { 22 return 0, err 23 } 24 go func() { 25 httpMux := http.NewServeMux() 26 for k, v := range handlers { 27 httpMux.HandleFunc(k, v) 28 } 29 err = http.Serve(listener, httpMux) 30 if err != nil { 31 panic(err) 32 } 33 }() 34 return listener.Addr().(*net.TCPAddr).Port, nil 35 } 36 37 func GetTestPackages(searchPattern string) []string { 38 // Get all packages with test files. 39 cmd := exec.Command("go", "list", "-f", "{{.ImportPath}} {{.TestGoFiles}}", searchPattern) 40 packages, _ := cmd.Output() 41 42 scanner := bufio.NewScanner(strings.NewReader(string(packages))) 43 var unitTests []string 44 for scanner.Scan() { 45 fields := strings.Split(scanner.Text(), " ") 46 // Skip if package does not contain test files. 47 if len(fields) > 1 && len(fields[1]) > 2 { 48 unitTests = append(unitTests, fields[0]) 49 } 50 } 51 return unitTests 52 } 53 54 func ExcludeTestsPackage(packages []string, packageToExclude string) []string { 55 var res []string 56 for _, packageName := range packages { 57 if packageName != packageToExclude { 58 res = append(res, packageName) 59 } 60 } 61 return res 62 } 63 64 func RunTests(testsPackages []string, hideUnitTestsLog bool) error { 65 if len(testsPackages) == 0 { 66 return nil 67 } 68 testsPackages = append([]string{"test", "-v", "-p", "1"}, testsPackages...) 69 cmd := exec.Command("go", testsPackages...) 70 71 if hideUnitTestsLog { 72 tempDirPath := filepath.Join(os.TempDir(), "jfrog_tests_logs") 73 exitOnErr(fileutils.CreateDirIfNotExist(tempDirPath)) 74 75 f, err := os.Create(filepath.Join(tempDirPath, "unit_tests.log")) 76 exitOnErr(err) 77 78 cmd.Stdout, cmd.Stderr = f, f 79 if err := cmd.Run(); err != nil { 80 log.Error("Unit tests failed, full report available at the following path:", f.Name()) 81 exitOnErr(err) 82 } 83 log.Info("Full unit testing report available at the following path:", f.Name()) 84 return nil 85 } 86 87 cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr 88 if err := cmd.Run(); err != nil { 89 log.Error("Unit tests failed") 90 exitOnErr(err) 91 } 92 93 return nil 94 } 95 96 func exitOnErr(err error) { 97 if err != nil { 98 log.Error(err) 99 os.Exit(1) 100 } 101 } 102 103 func InitVcsSubmoduleTestDir(t *testing.T, srcPath, tmpDir string) (submodulePath string) { 104 var err error 105 assert.NoError(t, fileutils.CopyDir(srcPath, tmpDir, true, nil)) 106 if found, err := fileutils.IsDirExists(filepath.Join(tmpDir, "gitdata"), false); found { 107 assert.NoError(t, err) 108 assert.NoError(t, fileutils.RenamePath(filepath.Join(tmpDir, "gitdata"), filepath.Join(tmpDir, ".git"))) 109 } 110 submoduleDst := filepath.Join(tmpDir, "subdir", "submodule") 111 assert.NoError(t, fileutils.CopyFile(submoduleDst, filepath.Join(tmpDir, "gitSubmoduleData"))) 112 assert.NoError(t, fileutils.MoveFile(filepath.Join(submoduleDst, "gitSubmoduleData"), filepath.Join(submoduleDst, ".git"))) 113 submodulePath, err = filepath.Abs(submoduleDst) 114 assert.NoError(t, err) 115 return submodulePath 116 } 117 118 func InitVcsWorktreeTestDir(t *testing.T, srcPath, tmpDir string) (worktreePath string) { 119 var err error 120 assert.NoError(t, fileutils.CopyDir(srcPath, tmpDir, true, nil)) 121 if found, err := fileutils.IsDirExists(filepath.Join(tmpDir, "gitdata"), false); found { 122 assert.NoError(t, err) 123 assert.NoError(t, fileutils.RenamePath(filepath.Join(tmpDir, "gitdata"), filepath.Join(tmpDir, "bare.git"))) 124 } 125 worktreeDst := filepath.Join(tmpDir, "worktree_repo") 126 worktreePath, err = filepath.Abs(worktreeDst) 127 assert.NoError(t, fileutils.MoveFile(filepath.Join(worktreeDst, "gitWorktreeData"), filepath.Join(worktreeDst, ".git"))) 128 assert.NoError(t, err) 129 return worktreePath 130 } 131 132 func ChangeDirAndAssert(t *testing.T, dirPath string) { 133 assert.NoError(t, os.Chdir(dirPath), "Couldn't change dir to "+dirPath) 134 } 135 136 // ChangeDirWithCallback changes working directory to the given path and return function that change working directory back to the original path. 137 func ChangeDirWithCallback(t *testing.T, wd, dirPath string) func() { 138 ChangeDirAndAssert(t, dirPath) 139 return func() { 140 ChangeDirAndAssert(t, wd) 141 } 142 } 143 144 func RemoveAndAssert(t *testing.T, path string) { 145 assert.NoError(t, os.Remove(path), "Couldn't remove: "+path) 146 } 147 148 func RemoveAllAndAssert(t *testing.T, path string) { 149 assert.NoError(t, fileutils.RemoveTempDir(path), "Couldn't removeAll: "+path) 150 } 151 152 func SetEnvAndAssert(t *testing.T, key, value string) { 153 assert.NoError(t, os.Setenv(key, value), "Failed to set env: "+key) 154 } 155 156 func SetEnvWithCallbackAndAssert(t *testing.T, key, value string) func() { 157 oldValue, exist := os.LookupEnv(key) 158 SetEnvAndAssert(t, key, value) 159 160 if exist { 161 return func() { 162 SetEnvAndAssert(t, key, oldValue) 163 } 164 } 165 166 return func() { 167 UnSetEnvAndAssert(t, key) 168 } 169 } 170 171 func UnSetEnvAndAssert(t *testing.T, key string) { 172 assert.NoError(t, os.Unsetenv(key), "Failed to unset env: "+key) 173 } 174 175 func GetLocalArtifactoryTokenIfNeeded(url string) (adminToken string) { 176 if strings.Contains(url, "localhost:8081") { 177 adminToken = os.Getenv("JFROG_TESTS_LOCAL_ACCESS_TOKEN") 178 } 179 return 180 }