mirror of
https://github.com/golang/sys.git
synced 2026-01-29 15:12:09 +03:00
unix: remove usage of ioutil.TempFile in tests
Mostly change that to os.Create(filepath.Join(t.TempDir(), "filename")) so that the cleanup is done automatically (and, because this is a new directory, we do not need to have a "create unique temp file name" logic). While at it, fix some log/error messages -- if an error is coming from e.g. os package, it is already wrapped, so there's no need to add more context. Change-Id: I62f13679f256b94095be5ca1e77a3fa302f01b97 Reviewed-on: https://go-review.googlesource.com/c/sys/+/526298 Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
committed by
Gopher Robot
parent
cb4ecd9fe9
commit
fc717d344a
@@ -9,6 +9,7 @@ package unix_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
@@ -16,11 +17,10 @@ import (
|
||||
)
|
||||
|
||||
func TestDup3(t *testing.T) {
|
||||
tempFile, err := os.CreateTemp("", "TestDup")
|
||||
tempFile, err := os.Create(filepath.Join(t.TempDir(), "TestDup"))
|
||||
if err != nil {
|
||||
t.Fatalf("CreateTemp failed: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(tempFile.Name())
|
||||
defer tempFile.Close()
|
||||
oldFd := int(tempFile.Fd())
|
||||
|
||||
|
||||
@@ -31,39 +31,25 @@ func stringsFromByteSlice(buf []byte) []string {
|
||||
return result
|
||||
}
|
||||
|
||||
func createTestFile(t *testing.T, dir string) (f *os.File, cleanup func() error) {
|
||||
file, err := ioutil.TempFile(dir, t.Name())
|
||||
func createTestFile(t *testing.T) string {
|
||||
filename := filepath.Join(t.TempDir(), t.Name())
|
||||
err := os.WriteFile(filename, testData, 0600)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = file.Write(testData)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return file, func() error {
|
||||
return os.Remove(file.Name())
|
||||
}
|
||||
return filename
|
||||
}
|
||||
|
||||
func TestClonefile(t *testing.T) {
|
||||
file, cleanup := createTestFile(t, "")
|
||||
defer cleanup()
|
||||
fileName := createTestFile(t)
|
||||
|
||||
clonedName := file.Name() + "-cloned"
|
||||
err := unix.Clonefile(file.Name(), clonedName, 0)
|
||||
clonedName := fileName + "-cloned"
|
||||
err := unix.Clonefile(fileName, clonedName, 0)
|
||||
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
||||
t.Skip("clonefile is not available or supported, skipping test")
|
||||
} else if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(clonedName)
|
||||
|
||||
clonedData, err := ioutil.ReadFile(clonedName)
|
||||
if err != nil {
|
||||
@@ -76,17 +62,15 @@ func TestClonefile(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClonefileatWithCwd(t *testing.T) {
|
||||
file, cleanup := createTestFile(t, "")
|
||||
defer cleanup()
|
||||
fileName := createTestFile(t)
|
||||
|
||||
clonedName := file.Name() + "-cloned"
|
||||
err := unix.Clonefileat(unix.AT_FDCWD, file.Name(), unix.AT_FDCWD, clonedName, 0)
|
||||
clonedName := fileName + "-cloned"
|
||||
err := unix.Clonefileat(unix.AT_FDCWD, fileName, unix.AT_FDCWD, clonedName, 0)
|
||||
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
||||
t.Skip("clonefileat is not available or supported, skipping test")
|
||||
} else if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(clonedName)
|
||||
|
||||
clonedData, err := ioutil.ReadFile(clonedName)
|
||||
if err != nil {
|
||||
@@ -99,25 +83,22 @@ func TestClonefileatWithCwd(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestClonefileatWithRelativePaths(t *testing.T) {
|
||||
srcDir := t.TempDir()
|
||||
dstDir := t.TempDir()
|
||||
|
||||
srcFileName := createTestFile(t)
|
||||
srcDir := filepath.Dir(srcFileName)
|
||||
srcFd, err := unix.Open(srcDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer unix.Close(srcFd)
|
||||
|
||||
dstDir := t.TempDir()
|
||||
dstFd, err := unix.Open(dstDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer unix.Close(dstFd)
|
||||
|
||||
srcFile, cleanup := createTestFile(t, srcDir)
|
||||
defer cleanup()
|
||||
|
||||
dstFile, err := ioutil.TempFile(dstDir, "TestClonefileat")
|
||||
dstFile, err := os.Create(filepath.Join(dstDir, "TestClonefileat"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -126,7 +107,7 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
src := filepath.Base(srcFile.Name())
|
||||
src := filepath.Base(srcFileName)
|
||||
dst := filepath.Base(dstFile.Name())
|
||||
err = unix.Clonefileat(srcFd, src, dstFd, dst, 0)
|
||||
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
||||
@@ -146,16 +127,16 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFclonefileat(t *testing.T) {
|
||||
file, cleanup := createTestFile(t, "")
|
||||
defer cleanup()
|
||||
fileName := createTestFile(t)
|
||||
dir := filepath.Dir(fileName)
|
||||
|
||||
fd, err := unix.Open(file.Name(), unix.O_RDONLY, 0)
|
||||
fd, err := unix.Open(fileName, unix.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer unix.Close(fd)
|
||||
|
||||
dstFile, err := ioutil.TempFile("", "TestFclonefileat")
|
||||
dstFile, err := os.Create(filepath.Join(dir, "dst"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -179,11 +160,10 @@ func TestFclonefileat(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFcntlFstore(t *testing.T) {
|
||||
f, err := ioutil.TempFile("", t.Name())
|
||||
f, err := os.CreateTemp(t.TempDir(), t.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
defer f.Close()
|
||||
|
||||
fstore := &unix.Fstore_t{
|
||||
|
||||
@@ -688,11 +688,10 @@ func TestFaccessat(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSyncFileRange(t *testing.T) {
|
||||
file, err := ioutil.TempFile("", "TestSyncFileRange")
|
||||
file, err := os.Create(filepath.Join(t.TempDir(), t.Name()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
|
||||
err = unix.SyncFileRange(int(file.Fd()), 0, 0, 0)
|
||||
@@ -1024,12 +1023,12 @@ func TestOpenat2(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIoctlFileDedupeRange(t *testing.T) {
|
||||
f1, err := ioutil.TempFile("", t.Name())
|
||||
dir := t.TempDir()
|
||||
f1, err := os.Create(filepath.Join(dir, "f1"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f1.Close()
|
||||
defer os.Remove(f1.Name())
|
||||
|
||||
// Test deduplication with two blocks of zeros
|
||||
data := make([]byte, 4096)
|
||||
@@ -1041,12 +1040,11 @@ func TestIoctlFileDedupeRange(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
f2, err := ioutil.TempFile("", t.Name())
|
||||
f2, err := os.Create(filepath.Join(dir, "f2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer f2.Close()
|
||||
defer os.Remove(f2.Name())
|
||||
|
||||
for i := 0; i < 2; i += 1 {
|
||||
// Make the 2nd block different
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -83,12 +84,12 @@ func TestSysconf(t *testing.T) {
|
||||
// Event Ports
|
||||
|
||||
func TestBasicEventPort(t *testing.T) {
|
||||
tmpfile, err := os.CreateTemp("", "eventport")
|
||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), "eventport"))
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create a tempfile: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tmpfile.Close()
|
||||
path := tmpfile.Name()
|
||||
defer os.Remove(path)
|
||||
|
||||
stat, err := os.Stat(path)
|
||||
if err != nil {
|
||||
|
||||
@@ -126,11 +126,10 @@ func TestSignalNum(t *testing.T) {
|
||||
|
||||
func TestFcntlInt(t *testing.T) {
|
||||
t.Parallel()
|
||||
file, err := ioutil.TempFile("", "TestFcntlInt")
|
||||
file, err := os.Create(filepath.Join(t.TempDir(), t.Name()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
f := file.Fd()
|
||||
flags, err := unix.FcntlInt(f, unix.F_GETFD, 0)
|
||||
@@ -293,9 +292,9 @@ func passFDChild() {
|
||||
// We make it in tempDir, which our parent will clean up.
|
||||
flag.Parse()
|
||||
tempDir := flag.Arg(0)
|
||||
f, err := ioutil.TempFile(tempDir, "")
|
||||
f, err := os.Create(filepath.Join(tempDir, "file"))
|
||||
if err != nil {
|
||||
fmt.Printf("TempFile: %v", err)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -452,11 +451,10 @@ func TestSetsockoptString(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDup(t *testing.T) {
|
||||
file, err := ioutil.TempFile("", "TestDup")
|
||||
file, err := os.Create(filepath.Join(t.TempDir(), t.Name()))
|
||||
if err != nil {
|
||||
t.Fatalf("Tempfile failed: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
f := int(file.Fd())
|
||||
|
||||
|
||||
@@ -124,11 +124,10 @@ func TestSignalNum(t *testing.T) {
|
||||
|
||||
func TestFcntlInt(t *testing.T) {
|
||||
t.Parallel()
|
||||
file, err := ioutil.TempFile("", "TestFnctlInt")
|
||||
file, err := os.Create(filepath.Join(t.TempDir(), "TestFnctlInt"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
f := file.Fd()
|
||||
flags, err := unix.FcntlInt(f, unix.F_GETFD, 0)
|
||||
@@ -291,9 +290,9 @@ func passFDChild() {
|
||||
// We make it in tempDir, which our parent will clean up.
|
||||
flag.Parse()
|
||||
tempDir := flag.Arg(0)
|
||||
f, err := ioutil.TempFile(tempDir, "")
|
||||
f, err := os.Create(filepath.Join(tempDir, "file"))
|
||||
if err != nil {
|
||||
fmt.Printf("TempFile: %v", err)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -424,11 +423,10 @@ func TestSetsockoptString(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDup(t *testing.T) {
|
||||
file, err := ioutil.TempFile("", "TestDup")
|
||||
file, err := os.Create(filepath.Join(t.TempDir(), "TestDup"))
|
||||
if err != nil {
|
||||
t.Fatalf("Tempfile failed: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
f := int(file.Fd())
|
||||
|
||||
@@ -615,9 +613,9 @@ func TestMountUnmount(t *testing.T) {
|
||||
func TestChroot(t *testing.T) {
|
||||
// create temp dir and tempfile 1
|
||||
tempDir := t.TempDir()
|
||||
f, err := ioutil.TempFile(tempDir, "chroot_test_file")
|
||||
f, err := os.Create(filepath.Join(tempDir, "chroot_test_file"))
|
||||
if err != nil {
|
||||
t.Fatalf("TempFile: %s", err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
// chroot temp dir
|
||||
err = unix.Chroot(tempDir)
|
||||
@@ -667,9 +665,9 @@ func TestFlock(t *testing.T) {
|
||||
}
|
||||
} else {
|
||||
// create temp dir and tempfile 1
|
||||
f, err := ioutil.TempFile(t.TempDir(), "flock_test_file")
|
||||
f, err := os.Create(filepath.Join(t.TempDir(), "flock_test_file"))
|
||||
if err != nil {
|
||||
t.Fatalf("TempFile: %s", err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
fd := int(f.Fd())
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
package unix_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -127,11 +127,10 @@ func TestXattr(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFdXattr(t *testing.T) {
|
||||
file, err := ioutil.TempFile("", "TestFdXattr")
|
||||
file, err := os.Create(filepath.Join(t.TempDir(), "TestFdXattr"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
defer file.Close()
|
||||
|
||||
fd := int(file.Fd())
|
||||
|
||||
Reference in New Issue
Block a user