kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/test/e2e/utils.go (about) 1 package e2e 2 3 import ( 4 "io" 5 "os" 6 "path/filepath" 7 "sort" 8 "strings" 9 10 "github.com/otiai10/copy" 11 "github.com/thoas/go-funk" 12 "kcl-lang.io/kpm/pkg/reporter" 13 ) 14 15 // LoadFirstFileWithExt read the first file with extention 'ext' in 'dir' and return the content. 16 func LoadFirstFileWithExt(dir string, ext string) string { 17 files, err := os.ReadDir(dir) 18 if err != nil { 19 reporter.ExitWithReport("kpm_e2e: failed to load file, the dir not exists.") 20 } 21 22 for _, file := range files { 23 if filepath.Ext(file.Name()) == ext { 24 data, err := os.ReadFile(filepath.Join(dir, file.Name())) 25 if err != nil { 26 reporter.ExitWithReport("kpm_e2e: the file exists, but failed to read file.") 27 } 28 return string(data) 29 } 30 } 31 32 reporter.ExitWithReport("kpm_e2e: failed to load file, the file not exists.") 33 return "" 34 } 35 36 // Copy will copy file from 'srcPath' to 'dstPath'. 37 func Copy(srcPath, dstPath string) { 38 src, err := os.Open(srcPath) 39 if err != nil { 40 reporter.ExitWithReport("kpm_e2e: failed to copy file from src.") 41 } 42 defer src.Close() 43 44 dst, err := os.Create(dstPath) 45 if err != nil { 46 reporter.ExitWithReport("kpm_e2e: failed to copy file to dst.") 47 } 48 defer dst.Close() 49 50 _, err = io.Copy(dst, src) 51 if err != nil { 52 reporter.ExitWithReport("kpm_e2e: failed to copy file.") 53 } 54 } 55 56 // CopyDir will copy dir from 'srcDir' to 'dstDir'. 57 func CopyDir(srcDir, dstDir string) { 58 err := copy.Copy(srcDir, dstDir) 59 if err != nil { 60 reporter.ExitWithReport("kpm_e2e: failed to copy dir.") 61 } 62 } 63 64 var KEYS = []string{"<workspace>", "<ignore>", "<un_ordered>", "<user_home>"} 65 66 // IsIgnore will reture whether the expected result in 'expectedStr' should be ignored. 67 func IsIgnore(expectedStr string) bool { 68 return strings.Contains(expectedStr, "<ignore>") 69 } 70 71 // ReplaceAllKeyByValue will replace all 'key's by 'value' in 'originStr'. 72 func ReplaceAllKeyByValue(originStr, key, value string) string { 73 if !funk.Contains(KEYS, key) { 74 reporter.ExitWithReport("kpm_e2e: unknown key.", key) 75 } else { 76 return strings.ReplaceAll(originStr, key, value) 77 } 78 79 return originStr 80 } 81 82 // SplitCommand will spilt command string into []string, 83 // but the string in quotes will not be cut. 84 // If 'command' is 'aaa bbb "ccc ddd"', SplitCommand will return ["aaa", "bbb", "ccc ddd"]. 85 func SplitCommand(command string) []string { 86 var args []string 87 var currentArg string 88 inQuotes := false 89 for _, char := range command { 90 if char == '"' { 91 inQuotes = !inQuotes 92 continue 93 } 94 if char == ' ' && !inQuotes { 95 args = append(args, currentArg) 96 currentArg = "" 97 continue 98 } 99 currentArg += string(char) 100 } 101 if currentArg != "" { 102 args = append(args, currentArg) 103 } 104 return args 105 } 106 107 // RemoveLineOrder will remove the line order in 'str1'. 108 func RemoveLineOrder(str1 string) string { 109 // Split the strings into slices of lines 110 lines1 := strings.Split(str1, "\n") 111 112 // Sort the slices of lines 113 sort.Strings(lines1) 114 115 // Compare the sorted slices of lines 116 return strings.Join(lines1, "\n") 117 }