...

Source file src/github.com/peterbourgon/ff/v3/fftest/tempfile.go

Documentation: github.com/peterbourgon/ff/v3/fftest

     1  package fftest
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  // TempFile uses ioutil.TempFile to create a temporary file with the given
    10  // content. Use the cleanup func to remove the file.
    11  func TempFile(t *testing.T, content string) (filename string, cleanup func()) {
    12  	t.Helper()
    13  
    14  	f, err := ioutil.TempFile("", "fftest_")
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  
    19  	if _, err := f.Write([]byte(content)); err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	if err := f.Close(); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	cleanup = func() {
    28  		if err := os.Remove(f.Name()); err != nil {
    29  			t.Errorf("os.Remove(%q): %v", f.Name(), err)
    30  		}
    31  	}
    32  
    33  	return f.Name(), cleanup
    34  }
    35  

View as plain text