Add a test for creation of anonymous tempfiles

This commit is contained in:
Kovid Goyal 2023-01-26 11:51:02 +05:30
parent 4185e30d73
commit eb50fac8de
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 31 additions and 1 deletions

View File

@ -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 {

View 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")
}
}
}