github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/dockerfile/utils_test.go (about)

     1  package dockerfile // import "github.com/demonoid81/moby/builder/dockerfile"
     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  // createTestTempFile creates a temporary file within dir with specific contents and permissions.
    30  // When an error occurs, it terminates the test
    31  func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string {
    32  	filePath := filepath.Join(dir, filename)
    33  	err := ioutil.WriteFile(filePath, []byte(contents), perm)
    34  
    35  	if err != nil {
    36  		t.Fatalf("Error when creating %s file: %s", filename, err)
    37  	}
    38  
    39  	return filePath
    40  }
    41  
    42  // createTestSymlink creates a symlink file within dir which points to oldname
    43  func createTestSymlink(t *testing.T, dir, filename, oldname string) string {
    44  	filePath := filepath.Join(dir, filename)
    45  	if err := os.Symlink(oldname, filePath); err != nil {
    46  		t.Fatalf("Error when creating %s symlink to %s: %s", filename, oldname, err)
    47  	}
    48  
    49  	return filePath
    50  }