src/{syscall, os}: add File.Stat, with smoke test

This is File.Stat from https://github.com/tinygo-org/tinygo/pull/2371,
plus the windows bits,
plus a smoke test more or less from upstream,
all pulled together and rebased by dkegel-fastly.
This commit is contained in:
Damian Gryski
2021-12-09 18:53:51 -08:00
committed by Ron Evans
parent b85cb55aab
commit 322abf6d22
7 changed files with 108 additions and 5 deletions
+35
View File
@@ -8,6 +8,7 @@ import (
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
@@ -48,6 +49,40 @@ func TestStatBadDir(t *testing.T) {
}
}
func equal(name1, name2 string) (r bool) {
switch runtime.GOOS {
case "windows":
r = strings.ToLower(name1) == strings.ToLower(name2)
default:
r = name1 == name2
}
return
}
func TestFstat(t *testing.T) {
sfname := "TestFstat"
path := TempDir() + "/" + sfname
payload := writeFile(t, path, O_CREATE|O_TRUNC|O_RDWR, "Hello")
defer Remove(path)
file, err1 := Open(path)
if err1 != nil {
t.Fatal("open failed:", err1)
}
defer file.Close()
dir, err2 := file.Stat()
if err2 != nil {
t.Fatal("fstat failed:", err2)
}
if !equal(sfname, dir.Name()) {
t.Error("name should be ", sfname, "; is", dir.Name())
}
filesize := len(payload)
if dir.Size() != int64(filesize) {
t.Error("size should be", filesize, "; is", dir.Size())
}
}
func writeFile(t *testing.T, fname string, flag int, text string) string {
f, err := OpenFile(fname, flag, 0666)
if err != nil {