Documentation ¶
Overview ¶
Package fs provides tools for creating temporary files, and testing the contents and structure of a directory.
Index ¶
- func Apply(t assert.TestingT, path Path, ops ...PathOp)
- func Equal(path string, expected Manifest) cmp.Comparison
- func MatchAnyFileContent(path Path) error
- func MatchAnyFileMode(path Path) error
- func MatchContentIgnoreCarriageReturn(path Path) error
- func MatchExtraFiles(path Path) error
- type CompareResult
- type Dir
- type File
- type Manifest
- type Path
- type PathOp
- func AsUser(uid, gid int) PathOp
- func FromDir(source string) PathOp
- func MatchFileContent(f func([]byte) CompareResult) PathOp
- func MatchFilesWithGlob(glob string, ops ...PathOp) PathOp
- func WithBytes(raw []byte) PathOp
- func WithContent(content string) PathOp
- func WithDir(name string, ops ...PathOp) PathOp
- func WithFile(filename, content string, ops ...PathOp) PathOp
- func WithFiles(files map[string]string) PathOp
- func WithHardlink(path, target string) PathOp
- func WithMode(mode os.FileMode) PathOp
- func WithReaderContent(r io.Reader) PathOp
- func WithSymlink(path, target string) PathOp
- func WithTimestamps(atime, mtime time.Time) PathOp
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply the PathOps to the File
Example ¶
Add a file to an existing directory
package main import ( "testing" "gotest.tools/v3/fs" ) var t = &testing.T{} func main() { dir := fs.NewDir(t, "test-name") defer dir.Remove() fs.Apply(t, dir, fs.WithFile("file1", "content\n")) }
Output:
func Equal ¶
func Equal(path string, expected Manifest) cmp.Comparison
Equal compares a directory to the expected structured described by a manifest and returns success if they match. If they do not match the failure message will contain all the differences between the directory structure and the expected structure defined by the Manifest.
Equal is a cmp.Comparison which can be used with assert.Assert().
Example ¶
Test that a directory contains the expected files, and all the files have the expected properties.
package main import ( "testing" "gotest.tools/v3/assert" "gotest.tools/v3/fs" "gotest.tools/v3/golden" ) var t = &testing.T{} func main() { path := operationWhichCreatesFiles() expected := fs.Expected(t, fs.WithFile("one", "", fs.WithBytes(golden.Get(t, "one.golden")), fs.WithMode(0600)), fs.WithDir("data", fs.WithFile("config", "", fs.MatchAnyFileContent))) assert.Assert(t, fs.Equal(path, expected)) } func operationWhichCreatesFiles() string { return "example-path" }
Output:
func MatchAnyFileContent ¶
MatchAnyFileContent is a PathOp that updates a Manifest so that the file at path may contain any content.
func MatchAnyFileMode ¶
MatchAnyFileMode is a PathOp that updates a Manifest so that the resource at path will match any file mode.
func MatchContentIgnoreCarriageReturn ¶
MatchContentIgnoreCarriageReturn is a PathOp that ignores cariage return discrepancies.
func MatchExtraFiles ¶
MatchExtraFiles is a PathOp that updates a Manifest to allow a directory to contain unspecified files.
Types ¶
type CompareResult ¶
CompareResult is the result of comparison.
See gotest.tools/assert/cmp.StringResult for a convenient implementation of this interface.
type Dir ¶
type Dir struct {
// contains filtered or unexported fields
}
Dir is a temporary directory
func DirFromPath ¶ added in v3.1.0
DirFromPath returns a Dir for a path that already exists. No directory is created. Unlike NewDir the directory will not be removed automatically when the test exits, it is the callers responsibly to remove the directory. DirFromPath can be used with Apply to modify an existing directory.
If the path does not already exist, use NewDir instead.
func NewDir ¶
NewDir returns a new temporary directory using prefix as part of the directory name. The PathOps are applied before returning the Dir.
When used with Go 1.14+ the directory will be automatically removed when the test ends, unless the TEST_NOCLEANUP env var is set to true.
Example ¶
Create a temporary directory which contains a single file
package main import ( "io/ioutil" "testing" "gotest.tools/v3/assert" "gotest.tools/v3/assert/cmp" "gotest.tools/v3/fs" ) var t = &testing.T{} func main() { dir := fs.NewDir(t, "test-name", fs.WithFile("file1", "content\n")) defer dir.Remove() files, err := ioutil.ReadDir(dir.Path()) assert.NilError(t, err) assert.Assert(t, cmp.Len(files, 0)) }
Output:
type File ¶
type File struct {
// contains filtered or unexported fields
}
File is a temporary file on the filesystem
func NewFile ¶
NewFile creates a new file in a temporary directory using prefix as part of the filename. The PathOps are applied to the before returning the File.
When used with Go 1.14+ the file will be automatically removed when the test ends, unless the TEST_NOCLEANUP env var is set to true.
Example ¶
Create a new file with some content
package main import ( "io/ioutil" "testing" "gotest.tools/v3/assert" "gotest.tools/v3/fs" ) var t = &testing.T{} func main() { file := fs.NewFile(t, "test-name", fs.WithContent("content\n"), fs.AsUser(0, 0)) defer file.Remove() content, err := ioutil.ReadFile(file.Path()) assert.NilError(t, err) assert.Equal(t, "content\n", content) }
Output:
type Manifest ¶
type Manifest struct {
// contains filtered or unexported fields
}
Manifest stores the expected structure and properties of files and directories in a filesystem.
type Path ¶
type Path interface { Path() string Remove() }
Path objects return their filesystem path. Path may be implemented by a real filesystem object (such as File and Dir) or by a type which updates entries in a Manifest.
type PathOp ¶
PathOp is a function which accepts a Path and performs an operation on that path. When called with real filesystem objects (File or Dir) a PathOp modifies the filesystem at the path. When used with a Manifest object a PathOp updates the manifest to expect a value.
func MatchFileContent ¶
func MatchFileContent(f func([]byte) CompareResult) PathOp
MatchFileContent is a PathOp that updates a Manifest to use the provided function to determine if a file's content matches the expectation.
func MatchFilesWithGlob ¶
MatchFilesWithGlob is a PathOp that updates a Manifest to match files using glob pattern, and check them using the ops.
func WithContent ¶
WithContent writes content to a file at Path
func WithDir ¶
WithDir creates a subdirectory in the directory at path. Additional PathOp can be used to modify the subdirectory
Example ¶
Create a directory and subdirectory with files
package main import ( "os" "testing" "gotest.tools/v3/fs" ) var t = &testing.T{} func main() { dir := fs.NewDir(t, "test-name", fs.WithDir("subdir", fs.WithMode(os.FileMode(0700)), fs.WithFile("file1", "content\n")), ) defer dir.Remove() }
Output:
func WithHardlink ¶
WithHardlink creates a link in the directory which links to target. Target must be a path relative to the directory.
Note: the argument order is the inverse of os.Link to be consistent with the other functions in this package.
func WithReaderContent ¶ added in v3.0.1
WithReaderContent copies the reader contents to the file at Path
func WithSymlink ¶
WithSymlink creates a symlink in the directory which links to target. Target must be a path relative to the directory.
Note: the argument order is the inverse of os.Symlink to be consistent with the other functions in this package.
func WithTimestamps ¶
WithTimestamps sets the access and modification times of the file system object at path.