Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more fs cheatcodes #4803

Merged
merged 8 commits into from
Apr 29, 2023

Conversation

DaniPopes
Copy link
Member

@DaniPopes DaniPopes commented Apr 22, 2023

Motivation

Fixes #4797

Added to Cheats.sol:

struct DirEntry {
    string errorMessage;
    string path;
    uint64 depth;
    bool isDir;
    bool isSymlink;
}

function createDir(string calldata, bool) external;

function removeDir(string calldata, bool) external;

function readDir(string calldata) external returns (DirEntry[] memory);
function readDir(string calldata, uint64) external returns (DirEntry[] memory);
function readDir(string calldata, uint64, bool) external returns (DirEntry[] memory);

function readLink(string calldata) external returns (string memory);

TODO: read dir test fails due to memory allocation overflow, I'm not sure why

Solution

@DaniPopes DaniPopes marked this pull request as draft April 22, 2023 10:28
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File.t.sol -> Fs.t.sol

// string memory path = "../testdata/fixtures/Dir";

// {
// Cheats.DirEntry[] memory entries = cheats.readDir(path);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a single call fails with "allocation overflow", any ideas why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remembered this issue:

#4409

same error but turned out to be abi decoding related
but perhaps configured memory limit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it was an ABI encoding error: 66723e4
Really weird that it throws a "memory overflow" tho

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeh, perhaps our mapping of the internal solidity error is not entirely accurate

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awesome!

codewise looks great, test look good.

suggestion re file layout, and failing test

Comment on lines +138 to +143
createDir(string, bool)
removeDir(string, bool)
readDir(string)(DirEntry[])
readDir(string, uint64)(DirEntry[])
readDir(string, uint64, bool)(DirEntry[])
readLink(string)(string)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very useful imo

Comment on lines 310 to 329
/// Creates a new, empty directory at the provided path.
///
/// If `recursive` is true, it will also create all the parent directories if they don't exist.
///
/// # Errors
///
/// This function will return an error in the following situations, but is not limited to just these
/// cases:
///
/// - User lacks permissions to modify `path`.
/// - A parent of the given path doesn't exist and `recursive` is false.
/// - `path` already exists and `recursive` is false.
fn create_dir(state: &Cheatcodes, path: impl AsRef<Path>, recursive: bool) -> Result<Bytes, Bytes> {
let path =
state.config.ensure_path_allowed(path, FsAccessKind::Write).map_err(error::encode_error)?;
if recursive { fs::create_dir_all(path) } else { fs::create_dir(path) }
.map(|()| Bytes::new())
.map_err(error::encode_error)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect fs to grow eventually so would like to move all of them to new file fs.rs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in f9eb363

// string memory path = "../testdata/fixtures/Dir";

// {
// Cheats.DirEntry[] memory entries = cheats.readDir(path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remembered this issue:

#4409

same error but turned out to be abi decoding related
but perhaps configured memory limit?

@DaniPopes DaniPopes marked this pull request as ready for review April 27, 2023 21:41
@DaniPopes DaniPopes requested a review from mattsse April 27, 2023 21:41
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great!

needs forge-std and book followups

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat (cheatcode) allow to read folder
2 participants