From 1d4d2d3355ba1cacb821d98ffd08b57bedf0424e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 13 Jan 2021 10:58:53 +0100 Subject: [PATCH] unix: small fixes for TestPipe Check error returned by Pipe, use %v to format error values and strip trailing newlines from Errorf messages. Also move TestPipe above the test helper functions. Change-Id: Id431c8ffbcf525347cb4c138df69f92cc730f54a Reviewed-on: https://go-review.googlesource.com/c/sys/+/283592 Trust: Tobias Klauser Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- unix/syscall_unix_test.go | 81 ++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/unix/syscall_unix_test.go b/unix/syscall_unix_test.go index c8a75062..0639ee97 100644 --- a/unix/syscall_unix_test.go +++ b/unix/syscall_unix_test.go @@ -782,6 +782,48 @@ func TestMkdev(t *testing.T) { } } +func TestPipe(t *testing.T) { + const s = "hello" + var pipes [2]int + err := unix.Pipe(pipes[:]) + if err != nil { + t.Fatalf("pipe: %v", err) + } + r := pipes[0] + w := pipes[1] + go func() { + n, err := unix.Write(w, []byte(s)) + if err != nil { + t.Errorf("bad write: %v", err) + return + } + if n != len(s) { + t.Errorf("bad write count: %d", n) + return + } + err = unix.Close(w) + if err != nil { + t.Errorf("bad close: %v", err) + return + } + }() + var buf [10 + len(s)]byte + n, err := unix.Read(r, buf[:]) + if err != nil { + t.Fatalf("bad read: %v", err) + } + if n != len(s) { + t.Fatalf("bad read count: %d", n) + } + if string(buf[:n]) != s { + t.Fatalf("bad contents: %s", string(buf[:n])) + } + err = unix.Close(r) + if err != nil { + t.Fatalf("bad close: %v", err) + } +} + func TestRenameat(t *testing.T) { defer chtmpdir(t)() @@ -896,42 +938,3 @@ func chtmpdir(t *testing.T) func() { os.RemoveAll(d) } } - -func TestPipe(t *testing.T) { - const s = "hello" - var pipes [2]int - unix.Pipe(pipes[:]) - r := pipes[0] - w := pipes[1] - go func() { - n, err := unix.Write(w, []byte(s)) - if err != nil { - t.Errorf("bad write: %s\n", err) - return - } - if n != len(s) { - t.Errorf("bad write count: %d\n", n) - return - } - err = unix.Close(w) - if err != nil { - t.Errorf("bad close: %s\n", err) - return - } - }() - var buf [10 + len(s)]byte - n, err := unix.Read(r, buf[:]) - if err != nil { - t.Fatalf("bad read: %s\n", err) - } - if n != len(s) { - t.Fatalf("bad read count: %d\n", n) - } - if string(buf[:n]) != s { - t.Fatalf("bad contents: %s\n", string(buf[:n])) - } - err = unix.Close(r) - if err != nil { - t.Fatalf("bad close: %s\n", err) - } -}