whatcanGOwrong

This commit is contained in:
2024-09-19 21:38:24 -04:00
commit d0ae4d841d
17908 changed files with 4096831 additions and 0 deletions
@@ -0,0 +1,140 @@
// Copyright 2018 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.
// Package txtar implements a trivial text-based file archive format.
//
// The goals for the format are:
//
// - be trivial enough to create and edit by hand.
// - be able to store trees of text files describing go command test cases.
// - diff nicely in git history and code reviews.
//
// Non-goals include being a completely general archive format,
// storing binary data, storing file modes, storing special files like
// symbolic links, and so on.
//
// # Txtar format
//
// A txtar archive is zero or more comment lines and then a sequence of file entries.
// Each file entry begins with a file marker line of the form "-- FILENAME --"
// and is followed by zero or more file content lines making up the file data.
// The comment or file content ends at the next file marker line.
// The file marker line must begin with the three-byte sequence "-- "
// and end with the three-byte sequence " --", but the enclosed
// file name can be surrounding by additional white space,
// all of which is stripped.
//
// If the txtar file is missing a trailing newline on the final line,
// parsers should consider a final newline to be present anyway.
//
// There are no possible syntax errors in a txtar archive.
package txtar
import (
"bytes"
"fmt"
"os"
"strings"
)
// An Archive is a collection of files.
type Archive struct {
Comment []byte
Files []File
}
// A File is a single file in an archive.
type File struct {
Name string // name of file ("foo/bar.txt")
Data []byte // text content of file
}
// Format returns the serialized form of an Archive.
// It is assumed that the Archive data structure is well-formed:
// a.Comment and all a.File[i].Data contain no file marker lines,
// and all a.File[i].Name is non-empty.
func Format(a *Archive) []byte {
var buf bytes.Buffer
buf.Write(fixNL(a.Comment))
for _, f := range a.Files {
fmt.Fprintf(&buf, "-- %s --\n", f.Name)
buf.Write(fixNL(f.Data))
}
return buf.Bytes()
}
// ParseFile parses the named file as an archive.
func ParseFile(file string) (*Archive, error) {
data, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return Parse(data), nil
}
// Parse parses the serialized form of an Archive.
// The returned Archive holds slices of data.
func Parse(data []byte) *Archive {
a := new(Archive)
var name string
a.Comment, name, data = findFileMarker(data)
for name != "" {
f := File{name, nil}
f.Data, name, data = findFileMarker(data)
a.Files = append(a.Files, f)
}
return a
}
var (
newlineMarker = []byte("\n-- ")
marker = []byte("-- ")
markerEnd = []byte(" --")
)
// findFileMarker finds the next file marker in data,
// extracts the file name, and returns the data before the marker,
// the file name, and the data after the marker.
// If there is no next marker, findFileMarker returns before = fixNL(data), name = "", after = nil.
func findFileMarker(data []byte) (before []byte, name string, after []byte) {
var i int
for {
if name, after = isMarker(data[i:]); name != "" {
return data[:i], name, after
}
j := bytes.Index(data[i:], newlineMarker)
if j < 0 {
return fixNL(data), "", nil
}
i += j + 1 // positioned at start of new possible marker
}
}
// isMarker checks whether data begins with a file marker line.
// If so, it returns the name from the line and the data after the line.
// Otherwise it returns name == "" with an unspecified after.
func isMarker(data []byte) (name string, after []byte) {
if !bytes.HasPrefix(data, marker) {
return "", nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
data, after = data[:i], data[i+1:]
}
if !(bytes.HasSuffix(data, markerEnd) && len(data) >= len(marker)+len(markerEnd)) {
return "", nil
}
return strings.TrimSpace(string(data[len(marker) : len(data)-len(markerEnd)])), after
}
// If data is empty or ends in \n, fixNL returns data.
// Otherwise fixNL returns a new slice consisting of data with a final \n added.
func fixNL(data []byte) []byte {
if len(data) == 0 || data[len(data)-1] == '\n' {
return data
}
d := make([]byte, len(data)+1)
copy(d, data)
d[len(data)] = '\n'
return d
}
@@ -0,0 +1,111 @@
// Copyright 2018 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.
package txtar
import (
"bytes"
"fmt"
"reflect"
"testing"
)
func TestParse(t *testing.T) {
var tests = []struct {
name string
text string
parsed *Archive
}{
{
name: "basic",
text: `comment1
comment2
-- file1 --
File 1 text.
-- foo ---
More file 1 text.
-- file 2 --
File 2 text.
-- empty --
-- noNL --
hello world
-- empty filename line --
some content
-- --`,
parsed: &Archive{
Comment: []byte("comment1\ncomment2\n"),
Files: []File{
{"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
{"file 2", []byte("File 2 text.\n")},
{"empty", []byte{}},
{"noNL", []byte("hello world\n")},
{"empty filename line", []byte("some content\n-- --\n")},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := Parse([]byte(tt.text))
if !reflect.DeepEqual(a, tt.parsed) {
t.Fatalf("Parse: wrong output:\nhave:\n%s\nwant:\n%s", shortArchive(a), shortArchive(tt.parsed))
}
text := Format(a)
a = Parse(text)
if !reflect.DeepEqual(a, tt.parsed) {
t.Fatalf("Parse after Format: wrong output:\nhave:\n%s\nwant:\n%s", shortArchive(a), shortArchive(tt.parsed))
}
})
}
}
func TestFormat(t *testing.T) {
var tests = []struct {
name string
input *Archive
wanted string
}{
{
name: "basic",
input: &Archive{
Comment: []byte("comment1\ncomment2\n"),
Files: []File{
{"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
{"file 2", []byte("File 2 text.\n")},
{"empty", []byte{}},
{"noNL", []byte("hello world")},
},
},
wanted: `comment1
comment2
-- file1 --
File 1 text.
-- foo ---
More file 1 text.
-- file 2 --
File 2 text.
-- empty --
-- noNL --
hello world
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Format(tt.input)
if string(result) != tt.wanted {
t.Errorf("Wrong output. \nGot:\n%s\nWant:\n%s\n", string(result), tt.wanted)
}
})
}
}
func shortArchive(a *Archive) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "comment: %q\n", a.Comment)
for _, f := range a.Files {
fmt.Fprintf(&buf, "file %q: %q\n", f.Name, f.Data)
}
return buf.String()
}
@@ -0,0 +1,259 @@
// Copyright 2024 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.
package txtar
import (
"errors"
"fmt"
"io"
"io/fs"
"path"
"time"
)
// FS returns the file system form of an Archive.
// It returns an error if any of the file names in the archive
// are not valid file system names.
// The archive must not be modified while the FS is in use.
//
// If the file system detects that it has been modified, calls to the
// file system return an ErrModified error.
func FS(a *Archive) (fs.FS, error) {
// Create a filesystem with a root directory.
root := &node{fileinfo: fileinfo{path: ".", mode: readOnlyDir}}
fsys := &filesystem{a, map[string]*node{root.path: root}}
if err := initFiles(fsys); err != nil {
return nil, fmt.Errorf("cannot create fs.FS from txtar.Archive: %s", err)
}
return fsys, nil
}
const (
readOnly fs.FileMode = 0o444 // read only mode
readOnlyDir = readOnly | fs.ModeDir
)
// ErrModified indicates that file system returned by FS
// noticed that the underlying archive has been modified
// since the call to FS. Detection of modification is best effort,
// to help diagnose misuse of the API, and is not guaranteed.
var ErrModified error = errors.New("txtar.Archive has been modified during txtar.FS")
// A filesystem is a simple in-memory file system for txtar archives,
// represented as a map from valid path names to information about the
// files or directories they represent.
//
// File system operations are read only. Modifications to the underlying
// *Archive may race. To help prevent this, the filesystem tries
// to detect modification during Open and return ErrModified if it
// is able to detect a modification.
type filesystem struct {
ar *Archive
nodes map[string]*node
}
// node is a file or directory in the tree of a filesystem.
type node struct {
fileinfo // fs.FileInfo and fs.DirEntry implementation
idx int // index into ar.Files (for files)
entries []fs.DirEntry // subdirectories and files (for directories)
}
var _ fs.FS = (*filesystem)(nil)
var _ fs.DirEntry = (*node)(nil)
// initFiles initializes fsys from fsys.ar.Files. Returns an error if there are any
// invalid file names or collisions between file or directories.
func initFiles(fsys *filesystem) error {
for idx, file := range fsys.ar.Files {
name := file.Name
if !fs.ValidPath(name) {
return fmt.Errorf("file %q is an invalid path", name)
}
n := &node{idx: idx, fileinfo: fileinfo{path: name, size: len(file.Data), mode: readOnly}}
if err := insert(fsys, n); err != nil {
return err
}
}
return nil
}
// insert adds node n as an entry to its parent directory within the filesystem.
func insert(fsys *filesystem, n *node) error {
if m := fsys.nodes[n.path]; m != nil {
return fmt.Errorf("duplicate path %q", n.path)
}
fsys.nodes[n.path] = n
// fsys.nodes contains "." to prevent infinite loops.
parent, err := directory(fsys, path.Dir(n.path))
if err != nil {
return err
}
parent.entries = append(parent.entries, n)
return nil
}
// directory returns the directory node with the path dir and lazily-creates it
// if it does not exist.
func directory(fsys *filesystem, dir string) (*node, error) {
if m := fsys.nodes[dir]; m != nil && m.IsDir() {
return m, nil // pre-existing directory
}
n := &node{fileinfo: fileinfo{path: dir, mode: readOnlyDir}}
if err := insert(fsys, n); err != nil {
return nil, err
}
return n, nil
}
// dataOf returns the data associated with the file t.
// May return ErrModified if fsys.ar has been modified.
func dataOf(fsys *filesystem, n *node) ([]byte, error) {
if n.idx >= len(fsys.ar.Files) {
return nil, ErrModified
}
f := fsys.ar.Files[n.idx]
if f.Name != n.path || len(f.Data) != n.size {
return nil, ErrModified
}
return f.Data, nil
}
func (fsys *filesystem) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
}
n := fsys.nodes[name]
switch {
case n == nil:
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
case n.IsDir():
return &openDir{fileinfo: n.fileinfo, entries: n.entries}, nil
default:
data, err := dataOf(fsys, n)
if err != nil {
return nil, err
}
return &openFile{fileinfo: n.fileinfo, data: data}, nil
}
}
func (fsys *filesystem) ReadFile(name string) ([]byte, error) {
file, err := fsys.Open(name)
if err != nil {
return nil, err
}
if file, ok := file.(*openFile); ok {
// TODO: use slices.Clone once x/tools has 1.21 available.
cp := make([]byte, file.size)
copy(cp, file.data)
return cp, err
}
return nil, &fs.PathError{Op: "read", Path: name, Err: fs.ErrInvalid}
}
// A fileinfo implements fs.FileInfo and fs.DirEntry for a given archive file.
type fileinfo struct {
path string // unique path to the file or directory within a filesystem
size int
mode fs.FileMode
}
var _ fs.FileInfo = (*fileinfo)(nil)
var _ fs.DirEntry = (*fileinfo)(nil)
func (i *fileinfo) Name() string { return path.Base(i.path) }
func (i *fileinfo) Size() int64 { return int64(i.size) }
func (i *fileinfo) Mode() fs.FileMode { return i.mode }
func (i *fileinfo) Type() fs.FileMode { return i.mode.Type() }
func (i *fileinfo) ModTime() time.Time { return time.Time{} }
func (i *fileinfo) IsDir() bool { return i.mode&fs.ModeDir != 0 }
func (i *fileinfo) Sys() any { return nil }
func (i *fileinfo) Info() (fs.FileInfo, error) { return i, nil }
// An openFile is a regular (non-directory) fs.File open for reading.
type openFile struct {
fileinfo
data []byte
offset int64
}
var _ fs.File = (*openFile)(nil)
func (f *openFile) Stat() (fs.FileInfo, error) { return &f.fileinfo, nil }
func (f *openFile) Close() error { return nil }
func (f *openFile) Read(b []byte) (int, error) {
if f.offset >= int64(len(f.data)) {
return 0, io.EOF
}
if f.offset < 0 {
return 0, &fs.PathError{Op: "read", Path: f.path, Err: fs.ErrInvalid}
}
n := copy(b, f.data[f.offset:])
f.offset += int64(n)
return n, nil
}
func (f *openFile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
// offset += 0
case 1:
offset += f.offset
case 2:
offset += int64(len(f.data))
}
if offset < 0 || offset > int64(len(f.data)) {
return 0, &fs.PathError{Op: "seek", Path: f.path, Err: fs.ErrInvalid}
}
f.offset = offset
return offset, nil
}
func (f *openFile) ReadAt(b []byte, offset int64) (int, error) {
if offset < 0 || offset > int64(len(f.data)) {
return 0, &fs.PathError{Op: "read", Path: f.path, Err: fs.ErrInvalid}
}
n := copy(b, f.data[offset:])
if n < len(b) {
return n, io.EOF
}
return n, nil
}
// A openDir is a directory fs.File (so also an fs.ReadDirFile) open for reading.
type openDir struct {
fileinfo
entries []fs.DirEntry
offset int
}
var _ fs.ReadDirFile = (*openDir)(nil)
func (d *openDir) Stat() (fs.FileInfo, error) { return &d.fileinfo, nil }
func (d *openDir) Close() error { return nil }
func (d *openDir) Read(b []byte) (int, error) {
return 0, &fs.PathError{Op: "read", Path: d.path, Err: fs.ErrInvalid}
}
func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error) {
n := len(d.entries) - d.offset
if n == 0 && count > 0 {
return nil, io.EOF
}
if count > 0 && n > count {
n = count
}
list := make([]fs.DirEntry, n)
copy(list, d.entries[d.offset:d.offset+n])
d.offset += n
return list, nil
}
@@ -0,0 +1,183 @@
// Copyright 2024 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.
package txtar_test
import (
"io/fs"
"strings"
"testing"
"testing/fstest"
"golang.org/x/tools/txtar"
)
func TestFS(t *testing.T) {
var fstestcases = []struct {
name, input, files string
}{
{
name: "empty",
input: ``,
files: "",
},
{
name: "one",
input: `
-- one.txt --
one
`,
files: "one.txt",
},
{
name: "two",
input: `
-- one.txt --
one
-- two.txt --
two
`,
files: "one.txt two.txt",
},
{
name: "subdirectories",
input: `
-- one.txt --
one
-- 2/two.txt --
two
-- 2/3/three.txt --
three
-- 4/four.txt --
four
`,
files: "one.txt 2/two.txt 2/3/three.txt 4/four.txt",
},
}
for _, tc := range fstestcases {
t.Run(tc.name, func(t *testing.T) {
a := txtar.Parse([]byte(tc.input))
fsys, err := txtar.FS(a)
if err != nil {
t.Fatal(err)
}
files := strings.Fields(tc.files)
if err := fstest.TestFS(fsys, files...); err != nil {
t.Fatal(err)
}
for _, f := range a.Files {
b, err := fs.ReadFile(fsys, f.Name)
if err != nil {
t.Errorf("ReadFile(%q) failed with error: %v", f.Name, err)
}
if got, want := string(b), string(f.Data); got != want {
t.Errorf("ReadFile(%q) = %q; want %q", f.Name, got, want)
}
}
})
}
}
func TestInvalid(t *testing.T) {
invalidtestcases := []struct {
name, want string
input string
}{
{"unclean file names", "invalid path", `
-- 1/../one.txt --
one
-- 2/sub/../two.txt --
two
`},
{"duplicate name", `cannot create fs.FS from txtar.Archive: duplicate path "1/2/one.txt"`, `
-- 1/2/one.txt --
one
-- 1/2/one.txt --
two
`},
{"file conflicts with directory", `duplicate path "1/2"`, `
-- 1/2 --
one
-- 1/2/one.txt --
two
`},
}
for _, tc := range invalidtestcases {
t.Run(tc.name, func(t *testing.T) {
a := txtar.Parse([]byte(tc.input))
_, err := txtar.FS(a)
if err == nil {
t.Fatal("txtar.FS(...) succeeded; expected an error")
}
if got := err.Error(); !strings.Contains(got, tc.want) || tc.want == "" {
t.Errorf("txtar.FS(...) got error %q; want %q", got, tc.want)
}
})
}
}
func TestModified(t *testing.T) {
const input = `
-- one.txt --
one
`
for _, mod := range []func(a *txtar.Archive){
func(a *txtar.Archive) { a.Files[0].Data = []byte("other") },
func(a *txtar.Archive) { a.Files[0].Name = "other" },
func(a *txtar.Archive) { a.Files = nil },
} {
a := txtar.Parse([]byte(input))
if n := len(a.Files); n != 1 {
t.Fatalf("txtar.Parse(%q) got %d files; expected 1", input, n)
}
fsys, err := txtar.FS(a)
if err != nil {
t.Fatal(err)
}
// Confirm we can open "one.txt".
_, err = fsys.Open("one.txt")
if err != nil {
t.Fatal(err)
}
// Modify a to get ErrModified when opening "one.txt".
mod(a)
_, err = fsys.Open("one.txt")
if err != txtar.ErrModified {
t.Errorf("Open(%q) got error %s; want ErrModified", "one.txt", err)
}
}
}
func TestReadFile(t *testing.T) {
const input = `
-- 1/one.txt --
one
`
a := txtar.Parse([]byte(input))
fsys, err := txtar.FS(a)
if err != nil {
t.Fatal(err)
}
readfs := fsys.(fs.ReadFileFS)
_, err = readfs.ReadFile("1")
if err == nil {
t.Errorf("ReadFile(%q) succeeded; expected an error when reading a directory", "1")
}
content, err := readfs.ReadFile("1/one.txt")
if err != nil {
t.Fatal(err)
}
want := "one\n"
if got := string(content); want != got {
t.Errorf("ReadFile(%q) = %q; want %q", "1/one.txt", got, want)
}
}