github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/builder/remotecontext/utils_test.go (about) 1 package remotecontext // import "github.com/docker/docker/builder/remotecontext" 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 15 if err != nil { 16 t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err) 17 } 18 19 return path, func() { 20 err = os.RemoveAll(path) 21 22 if err != nil { 23 t.Fatalf("Error when removing directory %s: %s", path, err) 24 } 25 } 26 } 27 28 // createTestTempSubdir creates a temporary directory for testing. 29 // It returns the created path but doesn't provide a cleanup function, 30 // so createTestTempSubdir should be used only for creating temporary subdirectories 31 // whose parent directories are properly cleaned up. 32 // When an error occurs, it terminates the test. 33 func createTestTempSubdir(t *testing.T, dir, prefix string) string { 34 path, err := os.MkdirTemp(dir, prefix) 35 36 if err != nil { 37 t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err) 38 } 39 40 return path 41 } 42 43 // createTestTempFile creates a temporary file within dir with specific contents and permissions. 44 // When an error occurs, it terminates the test 45 func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string { 46 filePath := filepath.Join(dir, filename) 47 err := os.WriteFile(filePath, []byte(contents), perm) 48 49 if err != nil { 50 t.Fatalf("Error when creating %s file: %s", filename, err) 51 } 52 53 return filePath 54 }