github.com/hanwen/go-fuse@v1.0.0/internal/testutil/tempdir.go (about)

     1  // Copyright 2016 the Go-FUSE Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package testutil
     6  
     7  import (
     8  	"io/ioutil"
     9  	"log"
    10  	"runtime"
    11  	"strings"
    12  )
    13  
    14  // TempDir creates a temporary directory that includes the name of the
    15  // testcase. Panics if there was an I/O problem creating the directory.
    16  func TempDir() string {
    17  	frames := make([]uintptr, 10) // at least 1 entry needed
    18  	n := runtime.Callers(1, frames)
    19  
    20  	lastName := ""
    21  	for _, pc := range frames[:n] {
    22  		f := runtime.FuncForPC(pc)
    23  		name := f.Name()
    24  		i := strings.LastIndex(name, ".")
    25  		if i >= 0 {
    26  			name = name[i+1:]
    27  		}
    28  		if strings.HasPrefix(name, "Test") {
    29  			lastName = name
    30  		}
    31  	}
    32  
    33  	dir, err := ioutil.TempDir("", lastName)
    34  	if err != nil {
    35  		log.Panicf("TempDir(%s): %v", lastName, err)
    36  	}
    37  	return dir
    38  }