Documentation ¶
Overview ¶
Package fs provides tools for creating and working with temporary files and directories.
Index ¶
- type Dir
- type File
- type Path
- type PathOp
- func AsUser(uid, gid int) PathOp
- func FromDir(source string) 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 WithSymlink(path, target string) PathOp
- func WithTimestamps(atime, mtime time.Time) PathOp
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dir ¶
type Dir struct {
// contains filtered or unexported fields
}
Dir is a temporary directory
func NewDir ¶
NewDir returns a new temporary directory using prefix as part of the directory name. The PathOps are applied before returning the Dir.
Example ¶
Create a temporary directory which contains a single file
package main import ( "io/ioutil" "testing" "github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert/cmp" "github.com/gotestyourself/gotestyourself/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.
Example ¶
Create a new file with some content
package main import ( "io/ioutil" "testing" "github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/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 Path ¶
type Path interface { Path() string Remove() }
Path objects return their filesystem path. Both File and Dir implement Path.
type PathOp ¶
PathOp is a function which accepts a Path to perform some operation
func WithContent ¶
WithContent writes content to a file at Path
func WithDir ¶ added in v1.2.0
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" "github.com/gotestyourself/gotestyourself/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 ¶ added in v1.3.0
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 WithSymlink ¶ added in v1.3.0
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 ¶ added in v1.3.0
WithTimestamps sets the access and modification times of the file system object at path.