mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 11:07:46 +00:00
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.
This commit is contained in:
@@ -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.")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user