From 07908f3f54cb6e6524cf2d179c9f960892936015 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 7 Apr 2020 16:05:38 +0100 Subject: [PATCH] vfs: bring open_tests.go generator back into line with the generated tests In 54deb01f0006d0af vfs: Make OpenFile and friends return EINVAL if O_RDONLY and O_TRUNC The generated file open_test.go was edited directly without editing the generator. This commit brings the generator make_open_tests.go back into line with that edit. It also makes it so `go generate` can be used to regenerate the tests. --- vfs/make_open_tests.go | 28 +++++++++++++++++++++++++--- vfs/open_test.go | 2 +- vfs/vfs.go | 3 +++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/vfs/make_open_tests.go b/vfs/make_open_tests.go index 9d3f4a684..d610b87e0 100644 --- a/vfs/make_open_tests.go +++ b/vfs/make_open_tests.go @@ -1,6 +1,11 @@ -// This makes the open test suite +// This makes the open test suite. It tries to open a file (existing +// or not existing) with all possible file modes and writes a test +// matrix. // -// Run with go run make_open_tests.go | gofmt > open_test.go +// The behaviour is as run on Linux, with the small modification that +// O_TRUNC with O_RDONLY does **not** truncate the file. +// +// Run with go generate (defined in vfs.go) // //+build none @@ -26,6 +31,8 @@ func whichError(err error) string { return "nil" case io.EOF: return "io.EOF" + case os.ErrInvalid: + return "EINVAL" } s := err.Error() switch { @@ -40,6 +47,8 @@ func whichError(err error) string { return "" } +const accessModeMask = (os.O_RDONLY | os.O_WRONLY | os.O_RDWR) + // test Opening, reading and writing the file handle with the flags given func test(fileName string, flags int, mode string) { // first try with file not existing @@ -123,6 +132,19 @@ func test(fileName string, flags int, mode string) { log.Fatalf("failed to remove: %v", err) } + // http://pubs.opengroup.org/onlinepubs/7908799/xsh/open.html + // The result of using O_TRUNC with O_RDONLY is undefined. + // Linux seems to truncate the file, but we prefer to return EINVAL + if (flags&accessModeMask) == os.O_RDONLY && flags&os.O_TRUNC != 0 { + openNonExistentErr = os.ErrInvalid // EINVAL + readNonExistentErr = nil + writeNonExistentErr = nil + openExistingErr = os.ErrInvalid // EINVAL + readExistingErr = nil + writeExistingErr = nil + contents = "hello" + } + // output the struct fmt.Printf(`{ flags: %s, @@ -147,7 +169,7 @@ func test(fileName string, flags int, mode string) { } func main() { - fmt.Printf(`// data generated by go run make_open_tests.go | gofmt > open_test.go + fmt.Printf(`// AUTO GENERATED by make_open_tests.go - DO NOT EDIT - use go generate to rebuild package vfs diff --git a/vfs/open_test.go b/vfs/open_test.go index 22f661606..ad7fce7dc 100644 --- a/vfs/open_test.go +++ b/vfs/open_test.go @@ -1,4 +1,4 @@ -// data generated by go run make_open_tests.go | gofmt > open_test.go +// AUTO GENERATED by make_open_tests.go - DO NOT EDIT - use go generate to rebuild package vfs diff --git a/vfs/vfs.go b/vfs/vfs.go index 2fa71db69..fa2947237 100644 --- a/vfs/vfs.go +++ b/vfs/vfs.go @@ -16,6 +16,9 @@ // error conditions have ocurred. It may also return general errors // it receives. It tries to use os Error values (eg os.ErrExist) // where possible. + +//go:generate sh -c "go run make_open_tests.go | gofmt > open_test.go" + package vfs import (