Documentation ¶
Overview ¶
Package vfst provides helper functions for testing code that uses github.com/twpayne/go-vfs.
Index ¶
- Variables
- func RunTests(t *testing.T, fs vfs.FS, name string, tests ...interface{})
- type Builder
- func (b *Builder) Build(fs vfs.FS, root interface{}) error
- func (b *Builder) Mkdir(fs vfs.FS, path string, perm os.FileMode) error
- func (b *Builder) MkdirAll(fs vfs.FS, path string, perm os.FileMode) error
- func (b *Builder) Symlink(fs vfs.FS, oldname, newname string) error
- func (b *Builder) WriteFile(fs vfs.FS, path string, contents []byte, perm os.FileMode) error
- type BuilderOption
- type Dir
- type File
- type PathTest
- func TestContents(wantContents []byte) PathTest
- func TestContentsString(wantContentsStr string) PathTest
- func TestMinSize(wantMinSize int64) PathTest
- func TestModePerm(wantPerm os.FileMode) PathTest
- func TestModeType(wantModeType os.FileMode) PathTest
- func TestSize(wantSize int64) PathTest
- func TestSymlinkTarget(wantTarget string) PathTest
- func TestSysNlink(wantNlink int) PathTest
- type Symlink
- type Test
- type TestFS
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var TestIsDir = TestModeType(os.ModeDir)
TestIsDir is a PathTest that verifies that the path is a directory.
var TestModeIsRegular = TestModeType(0)
TestModeIsRegular is a PathTest that tests that the path is a regular file.
Functions ¶
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
A Builder populates an vfs.FS.
func NewBuilder ¶
func NewBuilder(options ...BuilderOption) *Builder
NewBuilder returns a new Builder with the given options set.
func (*Builder) Mkdir ¶
Mkdir creates directory path with permissions perm. It is idempotent and will not fail if path already exists, is a directory, and has permissions perm.
func (*Builder) MkdirAll ¶
MkdirAll creates directory path and any missing parent directories with permissions perm. It is idempotent and will not file if path already exists and is a directory.
func (*Builder) Symlink ¶
Symlink creates a symbolic link from newname to oldname. It will create any missing parent directories with default permissions. It is idempotent and will not fail if the symbolic link already exists and points to oldname.
type BuilderOption ¶
type BuilderOption func(*Builder)
A BuilderOption sets an option on a Builder.
func BuilderUmask ¶
func BuilderUmask(umask os.FileMode) BuilderOption
BuilderUmask sets a builder's umask.
func BuilderVerbose ¶
func BuilderVerbose(verbose bool) BuilderOption
BuilderVerbose sets a builder's verbose flag. When true, the builder will log all operations with the standard log package.
type PathTest ¶
A PathTest is a test on a specified path in an vfs.FS.
var TestDoesNotExist PathTest = testDoesNotExist
TestDoesNotExist is a PathTest that verifies that a file or directory does not exist.
func TestContents ¶
TestContents returns a PathTest that verifies the contents of the file are equal to wantContents.
func TestContentsString ¶
TestContentsString returns a PathTest that verifies the contetnts of the file are equal to wantContentsStr.
func TestMinSize ¶
TestMinSize returns a PathTest that tests that path's Size() is at least wantMinSize.
func TestModePerm ¶
TestModePerm returns a PathTest that verifies that the path's permissions are equal to wantPerm.
func TestModeType ¶
TestModeType returns a PathTest that verifies that the path's mode type is equal to wantModeType.
func TestSymlinkTarget ¶
TestSymlinkTarget returns a PathTest that tests that path's target is wantTarget.
func TestSysNlink ¶
TestSysNlink returns a PathTest that verifies that the the path's Sys().(*syscall.Stat_t).Nlink is equal to wantNlink. If path's Sys() cannot be converted to a *syscall.Stat_t, it does nothing.
type Symlink ¶
type Symlink struct {
Target string
}
A Symlink is a symbolic link with a specified target.
type TestFS ¶
A TestFS is a virtual filesystem based in a temporary directory.
func NewTestFS ¶
func NewTestFS(root interface{}, builderOptions ...BuilderOption) (*TestFS, func(), error)
NewTestFS returns a new *TestFS populated with root and a cleanup function.
Example ¶
package main import ( "testing" "github.com/twpayne/go-vfs/vfst" ) func main() { Test := func(t *testing.T) { fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{ "/home/user/.bashrc": "# contents of user's .bashrc\n", }) if err != nil { t.Fatal(err) } defer cleanup() vfst.RunTests(t, fs, "bashrc", vfst.TestPath("/home/user/.bashrc", vfst.TestModeIsRegular, vfst.TestContentsString("# contents of user's .bashrc\n"), ), ) } Test(&testing.T{}) }
Output:
Example (Complex) ¶
package main import ( "testing" "github.com/twpayne/go-vfs/vfst" ) func main() { Test := func(t *testing.T) { // Describe the structure of the filesystem using a map from filenames to // file or directory contents. root := map[string]interface{}{ // A string or []byte is sets a file's contents. "/home/user/.bashrc": "# contents of user's .bashrc\n", "/home/user/empty": []byte{}, // To set non-default permissions on a file, create an &vfst.File. "/home/user/bin/hello.sh": &vfst.File{ Perm: 0o755, Contents: []byte("echo hello\n"), }, // Directories can be nested. "/home/user/foo": map[string]interface{}{ "bar": map[string]interface{}{ "baz": "qux", }, }, // To set non-default permissions on a directory, create an // &vfst.Dir. "/root": &vfst.Dir{ Perm: 0o700, Entries: map[string]interface{}{ ".bashrc": "# contents of root's .bashrc\n", }, }, } // Create and populate an *vfst.TestFS fs, cleanup, err := vfst.NewTestFS(root) if err != nil { t.Fatal(err) } defer cleanup() // Create tests by creating data structures containing Tests. tests := []interface{}{ // Test multiple properties of a single path with TestPath. vfst.TestPath("/home", vfst.TestIsDir, vfst.TestModePerm(0o755), ), vfst.TestPath("/home/user", vfst.TestIsDir, vfst.TestModePerm(0o755), ), vfst.TestPath("/home/user/.bashrc", vfst.TestModeIsRegular, vfst.TestModePerm(0o644), vfst.TestContentsString("# contents of user's .bashrc\n"), ), // Maps with string keys create sub tests with testing.T.Run. The key // is used as the test name. map[string]interface{}{ "home_user_empty": vfst.TestPath("/home/user/empty", vfst.TestModeIsRegular, vfst.TestModePerm(0o644), vfst.TestSize(0), ), "foo_bar_baz": vfst.TestPath("/home/user/foo/bar/baz", vfst.TestModeIsRegular, vfst.TestModePerm(0o644), vfst.TestContentsString("qux"), ), "root": []interface{}{ vfst.TestPath("/root", vfst.TestIsDir, vfst.TestModePerm(0o700), ), vfst.TestPath("/root/.bashrc", vfst.TestModeIsRegular, vfst.TestModePerm(0o644), vfst.TestContentsString("# contents of root's .bashrc\n"), ), }, }, } // RunTests traverses the data structure and running all Tests. vfst.RunTests(t, fs, "", tests) // Optionally, calling fs.Keep() prevents the cleanup function from // removing the temporary directory, so you can inspect it later. The // directory itself is returned by fs.TempDir(). // fs.Keep() t.Logf("fs.TempDir() == %s", fs.TempDir()) } Test(&testing.T{}) }
Output: