vfst

package
v5.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package vfst provides helper functions for testing code that uses github.com/twpayne/go-vfs.

Index

Examples

Constants

This section is empty.

Variables

View Source
var TestDoesNotExist = func() PathTest { return testDoesNotExist }

TestDoesNotExist is a PathTest that verifies that a file or directory does not exist.

View Source
var TestIsDir = func() PathTest { return TestModeType(fs.ModeDir) }

TestIsDir is a PathTest that verifies that the path is a directory.

View Source
var TestModeIsRegular = func() PathTest { return TestModeType(0) }

TestModeIsRegular is a PathTest that tests that the path is a regular file.

Functions

func PermEqual

func PermEqual(perm1, perm2 fs.FileMode) bool

PermEqual returns if perm1 and perm2 represent the same permissions. On Windows, it always returns true.

func RunTests

func RunTests(t *testing.T, fileSystem vfs.FS, name string, tests ...any)

RunTests recursively runs tests on fileSystem.

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) Build

func (b *Builder) Build(fileSystem vfs.FS, root any) error

Build populates fileSystem from root.

func (*Builder) Mkdir

func (b *Builder) Mkdir(fileSystem vfs.FS, path string, perm fs.FileMode) error

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

func (b *Builder) MkdirAll(fileSystem vfs.FS, path string, perm fs.FileMode) error

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 (b *Builder) Symlink(fileSystem vfs.FS, oldname, newname string) error

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.

func (*Builder) WriteFile

func (b *Builder) WriteFile(fileSystem vfs.FS, path string, contents []byte, perm fs.FileMode) error

WriteFile writes file path with contents and permissions perm. It will create any missing parent directories with default permissions. It is idempotent and will not fail if the file already exists, has contents contents, and permissions perm.

type BuilderOption

type BuilderOption func(*Builder)

A BuilderOption sets an option on a Builder.

func BuilderUmask

func BuilderUmask(umask fs.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 Dir

type Dir struct {
	Perm    fs.FileMode
	Entries map[string]any
}

A Dir is a directory with a specified permissions and zero or more Entries.

type File

type File struct {
	Perm     fs.FileMode
	Contents []byte
}

A File is a file with a specified permissions and contents.

type PathTest

type PathTest func(*testing.T, vfs.FS, string)

A PathTest is a test on a specified path in an vfs.FS.

func TestContents

func TestContents(wantContents []byte) PathTest

TestContents returns a PathTest that verifies the contents of the file are equal to wantContents.

func TestContentsString

func TestContentsString(wantContentsStr string) PathTest

TestContentsString returns a PathTest that verifies the contetnts of the file are equal to wantContentsStr.

func TestMinSize

func TestMinSize(wantMinSize int64) PathTest

TestMinSize returns a PathTest that tests that path's Size() is at least wantMinSize.

func TestModePerm

func TestModePerm(wantPerm fs.FileMode) PathTest

TestModePerm returns a PathTest that verifies that the path's permissions are equal to wantPerm.

func TestModeType

func TestModeType(wantModeType fs.FileMode) PathTest

TestModeType returns a PathTest that verifies that the path's mode type is equal to wantModeType.

func TestSize

func TestSize(wantSize int64) PathTest

TestSize returns a PathTest that tests that path's Size() is equal to wantSize.

func TestSymlinkTarget

func TestSymlinkTarget(wantTarget string) PathTest

TestSymlinkTarget returns a PathTest that tests that path's target is wantTarget.

func TestSysNlink(wantNlink int) PathTest

TestSysNlink returns a PathTest that verifies that 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 struct {
	Target string
}

A Symlink is a symbolic link with a specified target.

type Test

type Test func(*testing.T, vfs.FS)

A Test is a test on an vfs.FS.

func TestPath

func TestPath(path string, pathTests ...PathTest) Test

TestPath returns a Test that runs pathTests on path.

type TestFS

type TestFS struct {
	vfs.PathFS
	// contains filtered or unexported fields
}

A TestFS is a virtual filesystem based in a temporary directory.

func NewEmptyTestFS

func NewEmptyTestFS() (*TestFS, func(), error)

NewEmptyTestFS returns a new empty TestFS and a cleanup function.

func NewTestFS

func NewTestFS(root any, 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/v5/vfst"
)

func main() {
	Test := func(t *testing.T) {
		t.Helper()

		fileSystem, cleanup, err := vfst.NewTestFS(map[string]any{
			"/home/user/.bashrc": "# contents of user's .bashrc\n",
		})
		if err != nil {
			t.Fatal(err)
		}
		defer cleanup()

		vfst.RunTests(t, fileSystem, "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/v5/vfst"
)

func main() {
	Test := func(t *testing.T) {
		t.Helper()

		// Describe the structure of the filesystem using a map from filenames to
		// file or directory contents.
		root := map[string]any{
			// 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]any{
				"bar": map[string]any{
					"baz": "qux",
				},
			},
			// To set non-default permissions on a directory, create an
			// &vfst.Dir.
			"/root": &vfst.Dir{
				Perm: 0o700,
				Entries: map[string]any{
					".bashrc": "# contents of root's .bashrc\n",
				},
			},
		}

		// Create and populate an *vfst.TestFS
		fileSystem, cleanup, err := vfst.NewTestFS(root)
		if err != nil {
			t.Fatal(err)
		}
		defer cleanup()

		// Create tests by creating data structures containing Tests.
		tests := []any{
			// 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]any{
				"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": []any{
					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, fileSystem, "", tests)

		// Optionally, calling fileSystem.Keep() prevents the cleanup function
		// from removing the temporary directory, so you can inspect it later.
		// The directory itself is returned by fileSystem.TempDir().
		// fileSystem.Keep()
		t.Logf("fs.TempDir() == %s", fileSystem.TempDir())
	}

	Test(&testing.T{})
}
Output:

func (*TestFS) Keep

func (t *TestFS) Keep()

Keep prevents t's cleanup function from removing the temporary directory. It has no effect if cleanup has already been called.

func (*TestFS) TempDir

func (t *TestFS) TempDir() string

TempDir returns t's temporary directory.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL