Skip to content

Commit 7197ad4

Browse files
committed
chore: add tests
1 parent 40aa0fa commit 7197ad4

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

crates/forge/tests/cli/cmd.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,6 +3586,66 @@ forgetest!(inspect_custom_counter_method_identifiers, |prj, cmd| {
35863586
╰----------------------------+------------╯
35873587
35883588
3589+
"#]]);
3590+
});
3591+
3592+
const CUSTOM_COUNTER_HUGE_METHOD_IDENTIFIERS: &str = r#"
3593+
contract Counter {
3594+
struct BigStruct {
3595+
uint256 a;
3596+
uint256 b;
3597+
uint256 c;
3598+
uint256 d;
3599+
uint256 e;
3600+
uint256 f;
3601+
}
3602+
3603+
struct NestedBigStruct {
3604+
BigStruct a;
3605+
BigStruct b;
3606+
BigStruct c;
3607+
}
3608+
3609+
function hugeIdentifier(NestedBigStruct[] calldata _bigStructs, NestedBigStruct calldata _bigStruct) external {}
3610+
}
3611+
"#;
3612+
3613+
forgetest!(inspect_custom_counter_very_huge_method_identifiers_unwrapped, |prj, cmd| {
3614+
prj.add_source("Counter.sol", CUSTOM_COUNTER_HUGE_METHOD_IDENTIFIERS).unwrap();
3615+
3616+
cmd.args(["inspect", "Counter", "method-identifiers"]).assert_success().stdout_eq(str![[r#"
3617+
3618+
╭-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------╮
3619+
| Method | Identifier |
3620+
+================================================================================================================================================================================================================================================================================================================================================+
3621+
| hugeIdentifier(((uint256,uint256,uint256,uint256,uint256,uint256),(uint256,uint256,uint256,uint256,uint256,uint256),(uint256,uint256,uint256,uint256,uint256,uint256))[],((uint256,uint256,uint256,uint256,uint256,uint256),(uint256,uint256,uint256,uint256,uint256,uint256),(uint256,uint256,uint256,uint256,uint256,uint256))) | f38dafbb |
3622+
╰-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------╯
3623+
3624+
3625+
"#]]);
3626+
});
3627+
3628+
forgetest!(inspect_custom_counter_very_huge_method_identifiers_wrapped, |prj, cmd| {
3629+
prj.add_source("Counter.sol", CUSTOM_COUNTER_HUGE_METHOD_IDENTIFIERS).unwrap();
3630+
3631+
// Force a specific terminal width to test wrapping
3632+
cmd.args(["inspect", "--wrap", "Counter", "method-identifiers"])
3633+
.assert_with_terminal_width(80)
3634+
.success()
3635+
.stdout_eq(str![[r#"
3636+
3637+
╭-----------------------------------------------------------------+------------╮
3638+
| Method | Identifier |
3639+
+==============================================================================+
3640+
| hugeIdentifier(((uint256,uint256,uint256,uint256,uint256,uint25 | f38dafbb |
3641+
| 6),(uint256,uint256,uint256,uint256,uint256,uint256),(uint256,u | |
3642+
| int256,uint256,uint256,uint256,uint256))[],((uint256,uint256,ui | |
3643+
| nt256,uint256,uint256,uint256),(uint256,uint256,uint256,uint256 | |
3644+
| ,uint256,uint256),(uint256,uint256,uint256,uint256,uint256,uint | |
3645+
| 256))) | |
3646+
╰-----------------------------------------------------------------+------------╯
3647+
3648+
35893649
"#]]);
35903650
});
35913651

crates/test-utils/src/util.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,18 @@ impl TestCommand {
948948
assert
949949
}
950950

951+
/// Runs the command with specific terminal width, returning a [`snapbox`] object to assert the
952+
/// command output.
953+
#[track_caller]
954+
pub fn assert_with_terminal_width(&mut self, width: u16) -> OutputAssert {
955+
let assert =
956+
OutputAssert::new(self.try_execute_via_tty_with_size(Some((width, 24))).unwrap());
957+
if self.redact_output {
958+
return assert.with_assert(test_assert());
959+
}
960+
assert
961+
}
962+
951963
/// Runs the command and asserts that it resulted in success.
952964
#[track_caller]
953965
pub fn assert_success(&mut self) -> OutputAssert {
@@ -1017,14 +1029,61 @@ impl TestCommand {
10171029

10181030
#[track_caller]
10191031
pub fn try_execute(&mut self) -> std::io::Result<Output> {
1020-
println!("executing {:?}", self.cmd);
10211032
let mut child =
10221033
self.cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::piped()).spawn()?;
10231034
if let Some(fun) = self.stdin_fun.take() {
10241035
fun(child.stdin.take().unwrap());
10251036
}
10261037
child.wait_with_output()
10271038
}
1039+
1040+
#[track_caller]
1041+
fn try_execute_via_tty_with_size(
1042+
&mut self,
1043+
size: Option<(u16, u16)>,
1044+
) -> std::io::Result<Output> {
1045+
// Get the program and args from the current command
1046+
let program = self.cmd.get_program().to_string_lossy().to_string();
1047+
let args: Vec<String> =
1048+
self.cmd.get_args().map(|arg| arg.to_string_lossy().to_string()).collect();
1049+
1050+
// Build the command string
1051+
let mut cmd_str = program;
1052+
for arg in &args {
1053+
cmd_str.push(' ');
1054+
// Simple shell escaping - wrap in single quotes and escape any single quotes
1055+
if arg.contains(' ') || arg.contains('"') || arg.contains('\'') {
1056+
cmd_str.push('\'');
1057+
cmd_str.push_str(&arg.replace("'", "'\\'\''"));
1058+
cmd_str.push('\'');
1059+
} else {
1060+
cmd_str.push_str(arg);
1061+
}
1062+
}
1063+
1064+
// If size is specified, wrap the command with stty to set terminal size
1065+
if let Some((cols, rows)) = size {
1066+
cmd_str = format!("stty cols {cols} rows {rows}; {cmd_str}");
1067+
}
1068+
1069+
// Use script command to run in a pseudo-terminal
1070+
let mut script_cmd = Command::new("script");
1071+
script_cmd
1072+
.arg("-q") // quiet mode, no script started/done messages
1073+
.arg("-c") // command to run
1074+
.arg(&cmd_str)
1075+
.arg("/dev/null") // don't save typescript file
1076+
.current_dir(self.cmd.get_current_dir().unwrap_or(Path::new(".")));
1077+
1078+
// Copy environment variables
1079+
for (key, val) in self.cmd.get_envs() {
1080+
if let (Some(key), Some(val)) = (key.to_str(), val) {
1081+
script_cmd.env(key, val);
1082+
}
1083+
}
1084+
1085+
script_cmd.output()
1086+
}
10281087
}
10291088

10301089
fn test_assert() -> snapbox::Assert {

0 commit comments

Comments
 (0)