fakefs

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0, BSD-3-Clause, MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fakefs provides a fake file system implementation for testing.

Index

Constants

View Source
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

func CopyTestData(tb testing.TB, srcDir string) string

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

func CreateSymlinkEscapeFixture(tb testing.TB, linkRelPath string) (scanRoot, outsidePath string)

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

func DirFS(tb testing.TB, dir string) scalibrfs.FS

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

func RealFSScanRoot(tb testing.TB, path string) *scalibrfs.ScanRoot

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

func SimLinkModifier(name string, f *fstest.MapFile) error

SimLinkModifier dynamically loads a file content from a specific location, example:

-- /path/to/file --
-> testdata/file

func TarGzModifier added in v0.5.2

func TarGzModifier(name string, f *fstest.MapFile) error

TarGzModifier parses the content of a .tar.gz file like a nested txtar and builds the tarball content, Example:

-- example.tar.gz --
== FILE1.ext ==
content
== FILE2.ext ==
content

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

func (i FakeFileInfo) Name() string

Name returns the name of the file.

func (FakeFileInfo) Size

func (i FakeFileInfo) Size() int64

Size returns the size 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

type FileModifier func(name string, f *fstest.MapFile) error

FileModifier allows mutating a file (e.g., compressing data, changing mode)

type MockEvalSymlinksFS added in v0.3.2

type MockEvalSymlinksFS struct {
	scalibrfs.FS

	Symlinks map[string]string
}

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 (fs *MockEvalSymlinksFS) EvalSymlink(name string) (string, error)

EvalSymlink mocks the evaluation of symlinks.

Jump to

Keyboard shortcuts

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