Skip to content

Commit

Permalink
Implement vm.prompt cheatcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Tudmotu committed Feb 4, 2024
1 parent e2d3278 commit afe4598
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cheatcodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ k256.workspace = true
walkdir = "2"
p256 = "0.13.2"
thiserror = "1"
dialoguer = "0.11.0"
40 changes: 40 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions crates/cheatcodes/spec/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,16 @@ interface Vm {
#[cheatcode(group = Filesystem)]
function tryFfi(string[] calldata commandInput) external returns (FfiResult memory result);

// -------- User Interaction --------

/// Prompts the user for a string value in the terminal.
#[cheatcode(group = Filesystem)]
function prompt(string calldata promptText) external returns (string memory input);

/// Prompts the user for a hidden string value in the terminal.
#[cheatcode(group = Filesystem)]
function promptSecret(string calldata promptText) external returns (string memory input);

// ======== Environment Variables ========

/// Sets environment variables.
Expand Down
28 changes: 28 additions & 0 deletions crates/cheatcodes/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{Cheatcode, Cheatcodes, Result, Vm::*};
use alloy_json_abi::ContractObject;
use alloy_primitives::U256;
use alloy_sol_types::SolValue;
use dialoguer::{Input, Password};
use foundry_common::{fs, get_artifact_path};
use foundry_config::fs_permissions::FsAccessKind;
use std::{
Expand Down Expand Up @@ -296,6 +297,20 @@ impl Cheatcode for tryFfiCall {
}
}

impl Cheatcode for promptCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { promptText: text } = self;
prompt(text).map(|res| res.abi_encode())
}
}

impl Cheatcode for promptSecretCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { promptText: text } = self;
prompt_secret(text).map(|res| res.abi_encode())
}
}

pub(super) fn write_file(state: &Cheatcodes, path: &Path, contents: &[u8]) -> Result {
let path = state.config.ensure_path_allowed(path, FsAccessKind::Write)?;
// write access to foundry.toml is not allowed
Expand Down Expand Up @@ -370,6 +385,19 @@ fn ffi(state: &Cheatcodes, input: &[String]) -> Result<FfiResult> {
})
}

fn prompt(prompt_text: &String) -> Result<String> {
let input: String =
Input::new().allow_empty(true).with_prompt(prompt_text).interact_text().unwrap();

Ok(input)
}

fn prompt_secret(prompt_text: &String) -> Result<String> {
let input: String = Password::new().with_prompt(prompt_text).interact().unwrap();

Ok(input)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit afe4598

Please sign in to comment.