From 6f31712b7dd73d33ed363799efe85191d591fd51 Mon Sep 17 00:00:00 2001 From: ZauberNerd Date: Fri, 4 Mar 2022 20:49:37 +0000 Subject: [PATCH] test: write into a temp file and read from its fd This test creates a new temp file and writes some bytes into it. It then opens a new file for the file descriptor of the temp file and tries to read some bytes out of it. --- src/os/file_anyos_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/os/file_anyos_test.go b/src/os/file_anyos_test.go index fed50d2a..595d120e 100644 --- a/src/os/file_anyos_test.go +++ b/src/os/file_anyos_test.go @@ -4,7 +4,9 @@ package os_test import ( + "io" . "os" + "runtime" "testing" ) @@ -86,3 +88,29 @@ func TestStandardFd(t *testing.T) { t.Errorf("Stderr.Fd() = %d, want 2", fd) } } + +func TestFd(t *testing.T) { + if runtime.GOOS == "windows" { + t.Log("TODO: TestFd fails on Windows, skipping") + return + } + f := newFile("TestFd.txt", t) + defer Remove(f.Name()) + defer f.Close() + + const data = "hello, world\n" + io.WriteString(f, data) + + fd := NewFile(f.Fd(), "as-fd") + defer fd.Close() + + b := make([]byte, 5) + n, err := fd.ReadAt(b, 0) + if n != 5 && err != nil { + t.Errorf("Failed to read 5 bytes from file descriptor: %v", err) + } + + if string(b) != data[:5] { + t.Errorf("File descriptor contents not equal to file contents.") + } +}