github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/helpers/file.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 "regexp" 8 "strings" 9 10 . "github.com/onsi/gomega" 11 ) 12 13 // ConvertPathToRegularExpression converts a windows file path into a 14 // string which may be embedded in a ginkgo-compatible regular expression. 15 func ConvertPathToRegularExpression(path string) string { 16 return strings.Replace(path, "\\", "\\\\", -1) 17 } 18 19 func OSAgnosticPath(baseDir string, template string, args ...interface{}) string { 20 theRealPath, err := filepath.EvalSymlinks(baseDir) 21 Expect(err).ToNot(HaveOccurred()) 22 return regexp.QuoteMeta(filepath.Join(theRealPath, fmt.Sprintf(template, args...))) 23 } 24 25 func TempFileWithContent(contents string) string { 26 tempFile, err := ioutil.TempFile("", "*") 27 defer tempFile.Close() 28 Expect(err).NotTo(HaveOccurred()) 29 30 bytes := []byte(contents) 31 _, err = tempFile.Write(bytes) 32 Expect(err).NotTo(HaveOccurred()) 33 34 return tempFile.Name() 35 }