mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-01 22:57:29 -04: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
+3
-3
@@ -9,6 +9,7 @@ package unix_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -16,11 +17,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestDup3(t *testing.T) {
|
func TestDup3(t *testing.T) {
|
||||||
tempFile, err := os.CreateTemp("", "TestDup")
|
tempFile, err := os.Create(filepath.Join(t.TempDir(), "TestDup"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("CreateTemp failed: %v", err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(tempFile.Name())
|
|
||||||
defer tempFile.Close()
|
defer tempFile.Close()
|
||||||
oldFd := int(tempFile.Fd())
|
oldFd := int(tempFile.Fd())
|
||||||
|
|
||||||
|
|||||||
+20
-40
@@ -31,39 +31,25 @@ func stringsFromByteSlice(buf []byte) []string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestFile(t *testing.T, dir string) (f *os.File, cleanup func() error) {
|
func createTestFile(t *testing.T) string {
|
||||||
file, err := ioutil.TempFile(dir, t.Name())
|
filename := filepath.Join(t.TempDir(), t.Name())
|
||||||
|
err := os.WriteFile(filename, testData, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
return filename
|
||||||
_, 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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClonefile(t *testing.T) {
|
func TestClonefile(t *testing.T) {
|
||||||
file, cleanup := createTestFile(t, "")
|
fileName := createTestFile(t)
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
clonedName := file.Name() + "-cloned"
|
clonedName := fileName + "-cloned"
|
||||||
err := unix.Clonefile(file.Name(), clonedName, 0)
|
err := unix.Clonefile(fileName, clonedName, 0)
|
||||||
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
||||||
t.Skip("clonefile is not available or supported, skipping test")
|
t.Skip("clonefile is not available or supported, skipping test")
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(clonedName)
|
|
||||||
|
|
||||||
clonedData, err := ioutil.ReadFile(clonedName)
|
clonedData, err := ioutil.ReadFile(clonedName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -76,17 +62,15 @@ func TestClonefile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestClonefileatWithCwd(t *testing.T) {
|
func TestClonefileatWithCwd(t *testing.T) {
|
||||||
file, cleanup := createTestFile(t, "")
|
fileName := createTestFile(t)
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
clonedName := file.Name() + "-cloned"
|
clonedName := fileName + "-cloned"
|
||||||
err := unix.Clonefileat(unix.AT_FDCWD, file.Name(), unix.AT_FDCWD, clonedName, 0)
|
err := unix.Clonefileat(unix.AT_FDCWD, fileName, unix.AT_FDCWD, clonedName, 0)
|
||||||
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
||||||
t.Skip("clonefileat is not available or supported, skipping test")
|
t.Skip("clonefileat is not available or supported, skipping test")
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(clonedName)
|
|
||||||
|
|
||||||
clonedData, err := ioutil.ReadFile(clonedName)
|
clonedData, err := ioutil.ReadFile(clonedName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -99,25 +83,22 @@ func TestClonefileatWithCwd(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestClonefileatWithRelativePaths(t *testing.T) {
|
func TestClonefileatWithRelativePaths(t *testing.T) {
|
||||||
srcDir := t.TempDir()
|
srcFileName := createTestFile(t)
|
||||||
dstDir := t.TempDir()
|
srcDir := filepath.Dir(srcFileName)
|
||||||
|
|
||||||
srcFd, err := unix.Open(srcDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
|
srcFd, err := unix.Open(srcDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer unix.Close(srcFd)
|
defer unix.Close(srcFd)
|
||||||
|
|
||||||
|
dstDir := t.TempDir()
|
||||||
dstFd, err := unix.Open(dstDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
|
dstFd, err := unix.Open(dstDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer unix.Close(dstFd)
|
defer unix.Close(dstFd)
|
||||||
|
|
||||||
srcFile, cleanup := createTestFile(t, srcDir)
|
dstFile, err := os.Create(filepath.Join(dstDir, "TestClonefileat"))
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
dstFile, err := ioutil.TempFile(dstDir, "TestClonefileat")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -126,7 +107,7 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
src := filepath.Base(srcFile.Name())
|
src := filepath.Base(srcFileName)
|
||||||
dst := filepath.Base(dstFile.Name())
|
dst := filepath.Base(dstFile.Name())
|
||||||
err = unix.Clonefileat(srcFd, src, dstFd, dst, 0)
|
err = unix.Clonefileat(srcFd, src, dstFd, dst, 0)
|
||||||
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
if err == unix.ENOSYS || err == unix.ENOTSUP {
|
||||||
@@ -146,16 +127,16 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFclonefileat(t *testing.T) {
|
func TestFclonefileat(t *testing.T) {
|
||||||
file, cleanup := createTestFile(t, "")
|
fileName := createTestFile(t)
|
||||||
defer cleanup()
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer unix.Close(fd)
|
defer unix.Close(fd)
|
||||||
|
|
||||||
dstFile, err := ioutil.TempFile("", "TestFclonefileat")
|
dstFile, err := os.Create(filepath.Join(dir, "dst"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -179,11 +160,10 @@ func TestFclonefileat(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFcntlFstore(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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(f.Name())
|
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
fstore := &unix.Fstore_t{
|
fstore := &unix.Fstore_t{
|
||||||
|
|||||||
@@ -688,11 +688,10 @@ func TestFaccessat(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSyncFileRange(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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(file.Name())
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = unix.SyncFileRange(int(file.Fd()), 0, 0, 0)
|
err = unix.SyncFileRange(int(file.Fd()), 0, 0, 0)
|
||||||
@@ -1024,12 +1023,12 @@ func TestOpenat2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestIoctlFileDedupeRange(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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer f1.Close()
|
defer f1.Close()
|
||||||
defer os.Remove(f1.Name())
|
|
||||||
|
|
||||||
// Test deduplication with two blocks of zeros
|
// Test deduplication with two blocks of zeros
|
||||||
data := make([]byte, 4096)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer f2.Close()
|
defer f2.Close()
|
||||||
defer os.Remove(f2.Name())
|
|
||||||
|
|
||||||
for i := 0; i < 2; i += 1 {
|
for i := 0; i < 2; i += 1 {
|
||||||
// Make the 2nd block different
|
// Make the 2nd block different
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -83,12 +84,12 @@ func TestSysconf(t *testing.T) {
|
|||||||
// Event Ports
|
// Event Ports
|
||||||
|
|
||||||
func TestBasicEventPort(t *testing.T) {
|
func TestBasicEventPort(t *testing.T) {
|
||||||
tmpfile, err := os.CreateTemp("", "eventport")
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), "eventport"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create a tempfile: %v", err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer tmpfile.Close()
|
||||||
path := tmpfile.Name()
|
path := tmpfile.Name()
|
||||||
defer os.Remove(path)
|
|
||||||
|
|
||||||
stat, err := os.Stat(path)
|
stat, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -126,11 +126,10 @@ func TestSignalNum(t *testing.T) {
|
|||||||
|
|
||||||
func TestFcntlInt(t *testing.T) {
|
func TestFcntlInt(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
file, err := ioutil.TempFile("", "TestFcntlInt")
|
file, err := os.Create(filepath.Join(t.TempDir(), t.Name()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(file.Name())
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
f := file.Fd()
|
f := file.Fd()
|
||||||
flags, err := unix.FcntlInt(f, unix.F_GETFD, 0)
|
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.
|
// We make it in tempDir, which our parent will clean up.
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
tempDir := flag.Arg(0)
|
tempDir := flag.Arg(0)
|
||||||
f, err := ioutil.TempFile(tempDir, "")
|
f, err := os.Create(filepath.Join(tempDir, "file"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("TempFile: %v", err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,11 +451,10 @@ func TestSetsockoptString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDup(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 {
|
if err != nil {
|
||||||
t.Fatalf("Tempfile failed: %v", err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(file.Name())
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
f := int(file.Fd())
|
f := int(file.Fd())
|
||||||
|
|
||||||
|
|||||||
@@ -124,11 +124,10 @@ func TestSignalNum(t *testing.T) {
|
|||||||
|
|
||||||
func TestFcntlInt(t *testing.T) {
|
func TestFcntlInt(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
file, err := ioutil.TempFile("", "TestFnctlInt")
|
file, err := os.Create(filepath.Join(t.TempDir(), "TestFnctlInt"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(file.Name())
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
f := file.Fd()
|
f := file.Fd()
|
||||||
flags, err := unix.FcntlInt(f, unix.F_GETFD, 0)
|
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.
|
// We make it in tempDir, which our parent will clean up.
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
tempDir := flag.Arg(0)
|
tempDir := flag.Arg(0)
|
||||||
f, err := ioutil.TempFile(tempDir, "")
|
f, err := os.Create(filepath.Join(tempDir, "file"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("TempFile: %v", err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -424,11 +423,10 @@ func TestSetsockoptString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDup(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 {
|
if err != nil {
|
||||||
t.Fatalf("Tempfile failed: %v", err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(file.Name())
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
f := int(file.Fd())
|
f := int(file.Fd())
|
||||||
|
|
||||||
@@ -615,9 +613,9 @@ func TestMountUnmount(t *testing.T) {
|
|||||||
func TestChroot(t *testing.T) {
|
func TestChroot(t *testing.T) {
|
||||||
// create temp dir and tempfile 1
|
// create temp dir and tempfile 1
|
||||||
tempDir := t.TempDir()
|
tempDir := t.TempDir()
|
||||||
f, err := ioutil.TempFile(tempDir, "chroot_test_file")
|
f, err := os.Create(filepath.Join(tempDir, "chroot_test_file"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("TempFile: %s", err.Error())
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
// chroot temp dir
|
// chroot temp dir
|
||||||
err = unix.Chroot(tempDir)
|
err = unix.Chroot(tempDir)
|
||||||
@@ -667,9 +665,9 @@ func TestFlock(t *testing.T) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// create temp dir and tempfile 1
|
// 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 {
|
if err != nil {
|
||||||
t.Fatalf("TempFile: %s", err.Error())
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fd := int(f.Fd())
|
fd := int(f.Fd())
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -8,8 +8,8 @@
|
|||||||
package unix_test
|
package unix_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -127,11 +127,10 @@ func TestXattr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFdXattr(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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.Remove(file.Name())
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
fd := int(file.Fd())
|
fd := int(file.Fd())
|
||||||
|
|||||||
Reference in New Issue
Block a user