github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/builder/dockerfile/utils_test.go (about) 1 package dockerfile // import "github.com/Prakhar-Agarwal-byte/moby/builder/dockerfile" 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 ) 8 9 // createTestTempDir creates a temporary directory for testing. 10 // It returns the created path and a cleanup function which is meant to be used as deferred call. 11 // When an error occurs, it terminates the test. 12 func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) { 13 path, err := os.MkdirTemp(dir, prefix) 14 if err != nil { 15 t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err) 16 } 17 18 return path, func() { 19 err = os.RemoveAll(path) 20 21 if err != nil { 22 t.Fatalf("Error when removing directory %s: %s", path, err) 23 } 24 } 25 } 26 27 // createTestTempFile creates a temporary file within dir with specific contents and permissions. 28 // When an error occurs, it terminates the test 29 func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string { 30 filePath := filepath.Join(dir, filename) 31 err := os.WriteFile(filePath, []byte(contents), perm) 32 if err != nil { 33 t.Fatalf("Error when creating %s file: %s", filename, err) 34 } 35 36 return filePath 37 } 38 39 // createTestSymlink creates a symlink file within dir which points to oldname 40 func createTestSymlink(t *testing.T, dir, filename, oldname string) string { 41 filePath := filepath.Join(dir, filename) 42 if err := os.Symlink(oldname, filePath); err != nil { 43 t.Fatalf("Error when creating %s symlink to %s: %s", filename, oldname, err) 44 } 45 46 return filePath 47 }