mirror of
https://github.com/rwinkhart/sys.git
synced 2026-08-28 04:46:44 -04:00
This removes the remaining (and trivial) use of deprecated ioutil package from test files. Replacements are easy: ioutil.ReadAll -> io.ReadAll ioutil.ReadDir -> os.ReadDir ioutil.ReadFile -> os.ReadFile ioutil.WriteFile -> os.WriteFile While at it, simplify some error reporting. Change-Id: I60a242fd3c08d8fe571a18f16716439a9acdd59d Reviewed-on: https://go-review.googlesource.com/c/sys/+/526299 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build darwin || dragonfly || freebsd || openbsd || netbsd || zos
|
|
// +build darwin dragonfly freebsd openbsd netbsd zos
|
|
|
|
package unix_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func TestGetdirentries(t *testing.T) {
|
|
for _, count := range []int{10, 1000} {
|
|
t.Run(fmt.Sprintf("n=%d", count), func(t *testing.T) {
|
|
testGetdirentries(t, count)
|
|
})
|
|
}
|
|
}
|
|
func testGetdirentries(t *testing.T, count int) {
|
|
if count > 100 && testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
|
|
t.Skip("skipping in -short mode")
|
|
}
|
|
d := t.TempDir()
|
|
|
|
var names []string
|
|
for i := 0; i < count; i++ {
|
|
names = append(names, fmt.Sprintf("file%03d", i))
|
|
}
|
|
|
|
// Make files in the temp directory
|
|
for _, name := range names {
|
|
err := os.WriteFile(filepath.Join(d, name), []byte("data"), 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Read files using Getdirentries
|
|
fd, err := unix.Open(d, unix.O_RDONLY, 0)
|
|
if err != nil {
|
|
t.Fatalf("Open: %v", err)
|
|
}
|
|
defer unix.Close(fd)
|
|
var base uintptr
|
|
var buf [2048]byte
|
|
names2 := make([]string, 0, count)
|
|
for {
|
|
n, err := unix.Getdirentries(fd, buf[:], &base)
|
|
if err != nil {
|
|
t.Fatalf("Getdirentries: %v", err)
|
|
}
|
|
if n == 0 {
|
|
break
|
|
}
|
|
data := buf[:n]
|
|
for len(data) > 0 {
|
|
var bc int
|
|
bc, _, names2 = unix.ParseDirent(data, -1, names2)
|
|
if bc == 0 && len(data) > 0 {
|
|
t.Fatal("no progress")
|
|
}
|
|
data = data[bc:]
|
|
}
|
|
}
|
|
|
|
sort.Strings(names)
|
|
sort.Strings(names2)
|
|
if strings.Join(names, ":") != strings.Join(names2, ":") {
|
|
t.Errorf("names don't match\n names: %q\nnames2: %q", names, names2)
|
|
}
|
|
}
|