Add a test for creation of anonymous tempfiles
This commit is contained in:
parent
4185e30d73
commit
eb50fac8de
@ -24,7 +24,11 @@ func CreateAnonymousTemp(dir string) (*os.File, error) {
|
|||||||
return os.NewFile(uintptr(fd), path), nil
|
return os.NewFile(uintptr(fd), path), nil
|
||||||
}
|
}
|
||||||
if err == unix.ENOENT {
|
if err == unix.ENOENT {
|
||||||
return nil, err
|
return nil, &os.PathError{
|
||||||
|
Op: "open",
|
||||||
|
Path: dir,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
f, err := os.CreateTemp(dir, "")
|
f, err := os.CreateTemp(dir, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
26
tools/utils/tpmfile_test.go
Normal file
26
tools/utils/tpmfile_test.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ = fmt.Print
|
||||||
|
|
||||||
|
func TestCreateAnonymousTempfile(t *testing.T) {
|
||||||
|
f, err := CreateAnonymousTemp("")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
fd := int64(f.Fd())
|
||||||
|
f.Close()
|
||||||
|
if runtime.GOOS == "linux" {
|
||||||
|
if f.Name() != "/proc/self/fd/"+strconv.FormatInt(fd, 10) {
|
||||||
|
t.Fatalf("Anonymous tempfile was not created atomically")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user