github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/integration/helpers/file.go (about) 1 package helpers 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 9 . "github.com/onsi/gomega" 10 ) 11 12 // ConvertPathToRegularExpression converts a windows file path into a 13 // string which may be embedded in a ginkgo-compatible regular expression. 14 func ConvertPathToRegularExpression(path string) string { 15 return strings.Replace(path, "\\", "\\\\", -1) 16 } 17 18 // TempFileWithContent writes a temp file with given content and return the 19 // file name. 20 func TempFileWithContent(contents string) string { 21 tempFile, err := ioutil.TempFile("", "*") 22 Expect(err).NotTo(HaveOccurred()) 23 defer tempFile.Close() 24 25 bytes := []byte(contents) 26 _, err = tempFile.Write(bytes) 27 Expect(err).NotTo(HaveOccurred()) 28 29 return tempFile.Name() 30 } 31 32 // TempDirAbsolutePath wraps `ioutil.TempDir`, ensuring symlinks are expanded 33 // before returning the path 34 func TempDirAbsolutePath(dir string, prefix string) string { 35 tempDir, err := ioutil.TempDir(dir, prefix) 36 Expect(err).NotTo(HaveOccurred()) 37 38 tempDir, err = filepath.EvalSymlinks(tempDir) 39 Expect(err).NotTo(HaveOccurred()) 40 41 return tempDir 42 } 43 44 // TempFileAbsolutePath wraps `ioutil.TempFile`, ensuring symlinks are expanded 45 // before returning the path 46 func TempFileAbsolutePath(dir string, pattern string) *os.File { 47 var ( 48 err error 49 absoluteDir string 50 ) 51 if dir == "" { 52 absoluteDir = os.TempDir() 53 absoluteDir, err = filepath.EvalSymlinks(absoluteDir) 54 Expect(err).NotTo(HaveOccurred()) 55 } else { 56 absoluteDir, err = filepath.EvalSymlinks(dir) 57 Expect(err).NotTo(HaveOccurred()) 58 } 59 tempFile, err := ioutil.TempFile(absoluteDir, pattern) 60 Expect(err).NotTo(HaveOccurred()) 61 62 return tempFile 63 }