vfst

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2020 License: MIT Imports: 11 Imported by: 3

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 TestIsDir = TestModeType(os.ModeDir)

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

View Source
var TestModeIsRegular = TestModeType(0)

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

Functions

func RunTests

func RunTests(t *testing.T, fs vfs.FS, name string, tests ...interface{})

RunTests recursively runs tests on fs.

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(fs vfs.FS, root interface{}) error

Build populates fs from root.

func (*Builder) Mkdir

func (b *Builder) Mkdir(fs vfs.FS, path string, perm os.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(fs vfs.FS, path string, perm os.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(fs 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(fs vfs.FS, path string, contents []byte, perm os.FileMode) error

WriteFile writes file path withe contents 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 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 Dir

type Dir struct {
	Perm    os.FileMode
	Entries map[string]interface{}
}

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

type File

type File struct {
	Perm     os.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.

var TestDoesNotExist PathTest = testDoesNotExist

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

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 os.FileMode) PathTest

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

func TestModeType

func TestModeType(wantModeType os.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 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 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:

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 added in v0.1.1

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