github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/builder/remotecontext/utils_test.go (about)

     1  package remotecontext // import "github.com/docker/docker/builder/remotecontext"
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  // createTestTempDir creates a temporary directory for testing.
    11  // It returns the created path and a cleanup function which is meant to be used as deferred call.
    12  // When an error occurs, it terminates the test.
    13  func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) {
    14  	path, err := ioutil.TempDir(dir, prefix)
    15  
    16  	if err != nil {
    17  		t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err)
    18  	}
    19  
    20  	return path, func() {
    21  		err = os.RemoveAll(path)
    22  
    23  		if err != nil {
    24  			t.Fatalf("Error when removing directory %s: %s", path, err)
    25  		}
    26  	}
    27  }
    28  
    29  // createTestTempSubdir creates a temporary directory for testing.
    30  // It returns the created path but doesn't provide a cleanup function,
    31  // so createTestTempSubdir should be used only for creating temporary subdirectories
    32  // whose parent directories are properly cleaned up.
    33  // When an error occurs, it terminates the test.
    34  func createTestTempSubdir(t *testing.T, dir, prefix string) string {
    35  	path, err := ioutil.TempDir(dir, prefix)
    36  
    37  	if err != nil {
    38  		t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err)
    39  	}
    40  
    41  	return path
    42  }
    43  
    44  // createTestTempFile creates a temporary file within dir with specific contents and permissions.
    45  // When an error occurs, it terminates the test
    46  func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string {
    47  	filePath := filepath.Join(dir, filename)
    48  	err := ioutil.WriteFile(filePath, []byte(contents), perm)
    49  
    50  	if err != nil {
    51  		t.Fatalf("Error when creating %s file: %s", filename, err)
    52  	}
    53  
    54  	return filePath
    55  }