Skip to content

Commit

Permalink
fix: use provided name for inspect pretty (#6817)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 16, 2024
1 parent 24abca6 commit 78ed707
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
17 changes: 16 additions & 1 deletion crates/fmt/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ pub fn format_to<W: Write>(

/// Parse and format a string with default settings
pub fn format(src: &str) -> Result<String, FormatterError> {
let parsed = parse(src).map_err(|_| FormatterError::Fmt(std::fmt::Error))?;
let parsed = parse(src).map_err(|err| {
debug!(?err, "Parse error");
FormatterError::Fmt(std::fmt::Error)
})?;

let mut output = String::new();
format_to(&mut output, parsed, FormatterConfig::default())?;
Expand Down Expand Up @@ -105,3 +108,15 @@ pub fn import_path_string(path: &ImportPath) -> String {
ImportPath::Path(p) => p.to_string(),
}
}

#[cfg(test)]
mod tests {
use super::*;

// <https://github.com/foundry-rs/foundry/issues/6816>
#[test]
fn test_interface_format() {
let s = "interface I {\n function increment() external;\n function number() external view returns (uint256);\n function setNumber(uint256 newNumber) external;\n}";
let _formatted = format(s).unwrap();
}
}
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl InspectArgs {
.as_ref()
.ok_or_else(|| eyre::eyre!("Failed to fetch lossless ABI"))?;
if pretty {
let source = foundry_cli::utils::abi_to_solidity(abi, "")?;
let source = foundry_cli::utils::abi_to_solidity(abi, &contract.name)?;
println!("{source}");
} else {
print_json(abi)?;
Expand Down
14 changes: 14 additions & 0 deletions crates/forge/tests/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,3 +1611,17 @@ forgetest_init!(can_build_names_repeatedly, |prj, cmd| {
let unchanged = cmd.stdout_lossy();
assert!(unchanged.contains(list), "{}", list);
});

// <https://github.com/foundry-rs/foundry/issues/6816>
forgetest_init!(can_inspect_counter_pretty, |prj, cmd| {
cmd.args(["inspect", "src/Counter.sol:Counter", "abi", "--pretty"]);
let output = cmd.stdout_lossy();
assert_eq!(
output.trim(),
"interface Counter {
function increment() external;
function number() external view returns (uint256);
function setNumber(uint256 newNumber) external;
}"
);
});

0 comments on commit 78ed707

Please sign in to comment.