Documentation
¶
Overview ¶
Package fakefs provides a fake file system implementation for testing.
Index ¶
- Constants
- func CopyTestData(tb testing.TB, srcDir string) string
- func CreateSymlinkEscapeFixture(tb testing.TB, linkRelPath string) (scanRoot, outsidePath string)
- func DirFS(tb testing.TB, dir string) scalibrfs.FS
- func PrepareFS(txt string, modifiers ...FileModifier) (scalibrfs.FS, error)
- func RealFSScanRoot(tb testing.TB, path string) *scalibrfs.ScanRoot
- func SimLinkModifier(name string, f *fstest.MapFile) error
- func TarGzModifier(name string, f *fstest.MapFile) error
- type FakeFileInfo
- type FileModifier
- type MockEvalSymlinksFS
Constants ¶
const OutsideFileContent = "outside-secret"
OutsideFileContent is the content of the file that the symlink created by CreateSymlinkEscapeFixture points to.
Variables ¶
This section is empty.
Functions ¶
func CopyTestData ¶ added in v0.5.4
CopyTestData copies the directory tree rooted at srcDir into a new temporary directory created via tb.TempDir() and returns the path of the copy.
Symlinks to files and directories (including srcDir itself) are followed and copied as regular files and directories, so the copy can be opened with os.Root even when the test environment provides fixtures as symlinks that point outside srcDir. Unlike os.CopyFS, which recreates symlinks as symlinks, the copy contains no symlinks. Copied files are made owner-writable.
func CreateSymlinkEscapeFixture ¶ added in v0.5.4
CreateSymlinkEscapeFixture creates a scan root directory inside tb.TempDir() containing a symlink at linkRelPath (a local, slash-separated path) that points to a file outside the scan root. The file contains OutsideFileContent. It returns the scan root and the path of the outside file.
The test is skipped if symlinks can't be created on Windows.
func DirFS ¶ added in v0.5.4
DirFS returns a scalibrfs.DirFS rooted at dir and registers a tb.Cleanup hook to release its underlying os.Root handle before tb.TempDir() cleanup runs.
func PrepareFS ¶ added in v0.5.2
func PrepareFS(txt string, modifiers ...FileModifier) (scalibrfs.FS, error)
PrepareFS parses a txtar string into a mock filesystem. Optional modifiers can be provided to intercept and modify files (e.g., compression). Files ending in "/" in the txtar archive are treated as empty directories.
func RealFSScanRoot ¶ added in v0.5.4
RealFSScanRoot returns a scalibrfs.ScanRoot rooted at path and registers a tb.Cleanup hook to close its underlying os.Root handle.
func SimLinkModifier ¶ added in v0.5.2
SimLinkModifier dynamically loads a file content from a specific location, example:
-- /path/to/file -- -> testdata/file
Types ¶
type FakeFileInfo ¶
type FakeFileInfo struct {
FileName string
FileSize int64
FileMode fs.FileMode
FileModTime time.Time
}
FakeFileInfo is a fake implementation of fs.FileInfo.
func (FakeFileInfo) IsDir ¶
func (i FakeFileInfo) IsDir() bool
IsDir returns true if the file is a directory.
func (FakeFileInfo) ModTime ¶
func (i FakeFileInfo) ModTime() time.Time
ModTime returns the modification time of the file.
func (FakeFileInfo) Mode ¶
func (i FakeFileInfo) Mode() fs.FileMode
Mode returns the mode of the file.
func (FakeFileInfo) Sys ¶
func (i FakeFileInfo) Sys() any
Sys is an implementation of FileInfo.Sys() that returns nothing (nil).
type FileModifier ¶ added in v0.5.2
FileModifier allows mutating a file (e.g., compressing data, changing mode)
type MockEvalSymlinksFS ¶ added in v0.3.2
MockEvalSymlinksFS is a mock implementation of image.EvalSymlinksFS for testing.
func NewMockEvalSymlinksFS ¶ added in v0.3.2
func NewMockEvalSymlinksFS(fs scalibrfs.FS, symlinks map[string]string) *MockEvalSymlinksFS
NewMockEvalSymlinksFS creates a new MockEvalSymlinksFS.
func (*MockEvalSymlinksFS) EvalSymlink ¶ added in v0.3.2
func (fs *MockEvalSymlinksFS) EvalSymlink(name string) (string, error)
EvalSymlink mocks the evaluation of symlinks.