Skip to content

File cheat codes

// Reads the entire content of file to string, (path) => (data)
function readFile(string calldata) external returns (string memory);
/// Reads the entire content of file as binary. `path` is relative to the project root.
function readFileBinary(
string calldata path
) external view returns (bytes memory data);
/// Reads the directory at the given path recursively, up to `maxDepth`.
/// `maxDepth` defaults to 1, meaning only the direct children of the given directory will be returned.
/// Follows symbolic links if `followLinks` is true.
function readDir(
string calldata path
) external view returns (DirEntry[] memory entries);
function readDir(
string calldata path,
uint64 maxDepth
) external view returns (DirEntry[] memory entries);
function readDir(
string calldata path,
uint64 maxDepth,
bool followLinks
) external view returns (DirEntry[] memory entries);
// Reads next line of file to string, (path) => (line)
function readLine(string calldata) external returns (string memory);
/// Reads a symbolic link, returning the path that the link points to.
/// This cheatcode will revert in the following situations, but is not limited to just these cases:
/// - `path` is not a symbolic link.
/// - `path` does not exist.
function readLink(
string calldata linkPath
) external view returns (string memory targetPath);
// Writes data to file, creating a file if it does not exist, and entirely replacing its contents if it does.
// (path, data) => ()
function writeFile(string calldata, string calldata) external;
// Writes line to file, creating a file if it does not exist.
// (path, data) => ()
function writeLine(string calldata, string calldata) external;
// Closes file for reading, resetting the offset and allowing to read it from beginning with readLine.
// (path) => ()
function closeFile(string calldata) external;
// Removes file. This cheatcode will revert in the following situations, but is not limited to just these cases:
// - Path points to a directory.
// - The file doesn't exist.
// - The user lacks permissions to remove the file.
// (path) => ()
function removeFile(string calldata) external;
// Returns true if the given path points to an existing entity, else returns false
// (path) => (bool)
function exists(string calldata) external returns (bool);
// Returns true if the path exists on disk and is pointing at a regular file, else returns false
// (path) => (bool)
function isFile(string calldata) external returns (bool);
// Returns true if the path exists on disk and is pointing at a directory, else returns false
// (path) => (bool)
function isDir(string calldata) external returns (bool);

These cheatcodes provided by forge-std can be used for filesystem manipulation operations.

By default, filesystem access is disallowed and requires the fsPermissions setting in Solidity tests configuration:

  • fsPermissions: An optional object to configure file system permissions for cheatcodes. Defaults to no permissions. Exact path matching is used for file permissions. Prefix matching is used for directory permissions.
    • readFile: An array of file paths that can be read.
    • writeFile: An array of file paths that can be written.
    • readWriteFile: An array of file paths that can be both read and written.
    • readDirectory: An array of directory paths. All files and directories inside these directories can be read.
    • dangerouslyWriteDirectory: An array of directory paths. All files and directories inside these directories can be written. See warning above to understand why it’s dangerous.
    • dangerouslyReadWriteDirectory: An array of directory paths. All files and directories inside these directories can be both read and written. See warning above to understand why it’s dangerous.

Append a line to a file, this will create the file if it does not exist yet

This requires read access to the file / project root

fsPermissions: {
readDirectory: ["./"],
}
string memory path = "output.txt";
string memory line1 = "first line";
vm.writeLine(path, line1);
string memory line2 = "second line";
vm.writeLine(path, line2);

Write to and read from a file

This requires read-write access to file / project root:

fsPermissions: {
dangerouslyReadWriteDirectory: ["./"],
}
string memory path = "file.txt";
string memory data = "hello world";
vm.writeFile(path, data);
assertEq(vm.readFile(path), data);

Remove a file

This requires write access to file / project root:

fsPermissions: {
dangerouslyWriteDirectory: ["./"],
}
string memory path = "file.txt";
vm.removeFile(path);
assertFalse(vm.exists(validPath));

Verify that a filesystem path is valid

// Verify that path 'foo/files/bar.txt' exists
string memory validPath = "foo/files/bar.txt";
assertTrue(vm.exists(validPath));

Verify that a filesystem path points to a file or directory

// Verify that path 'foo/file/bar.txt' points to a file
string memory validFilePath = "foo/files/bar.txt";
assertTrue(vm.isFile(validFilePath));
// Verify that 'foo/file' points to a directory
string memory validDirPath = "foo/files";
assertTrue(vm.isDir(validDirPath));