github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/testutil/temp_files.go (about)

     1  package testutil // import "github.com/docker/docker/testutil"
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  // TempDir returns a temporary directory for use in tests.
    10  // t.TempDir() can't be used as the temporary directory returned by
    11  // that function cannot be accessed by the fake-root user for rootless
    12  // Docker. It creates a nested hierarchy of directories where the
    13  // outermost has permission 0700.
    14  func TempDir(t *testing.T) string {
    15  	t.Helper()
    16  	dir := t.TempDir()
    17  
    18  	parent := filepath.Dir(dir)
    19  	if parent != "" {
    20  		if err := os.Chmod(parent, 0o777); err != nil {
    21  			t.Fatalf("Failed to chmod parent of temp directory %q: %v", parent, err)
    22  		}
    23  	}
    24  
    25  	return dir
    26  }