Skip to content

Commit

Permalink
ci: forge fmt over all sol files in testdata (#2831)
Browse files Browse the repository at this point in the history
* run forge fmt over testdata

* add forge fmt check to ci

* run formatter over generated solidity

* use own fmt build to check testdata

* add and use fmt helper

* should only be used with stdin tty

* fmt: fix atty comparison
  • Loading branch information
joshieDo authored Aug 18, 2022
1 parent d9f0438 commit 6b3db2a
Show file tree
Hide file tree
Showing 59 changed files with 1,682 additions and 1,564 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ jobs:
args: --all --all-features -- -D warnings
token: ${{ secrets.GITHUB_TOKEN }}

- name: forge fmt
run: cargo run --bin forge -- fmt --check testdata/

cross-platform:
name: Cross-platform tests
if: github.event_name != 'pull_request'
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/src/cmd/forge/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl FmtArgs {
let mut paths = self.paths.iter().peekable();

if let Some(path) = paths.peek() {
if *path == Path::new("-") || !atty::is(atty::Stream::Stdin) {
if *path == Path::new("-") && !atty::is(atty::Stream::Stdin) {
let mut buf = String::new();
io::stdin().read_to_string(&mut buf).expect("Failed to read from stdin");
return vec![Input::Stdin(buf)]
Expand Down
10 changes: 10 additions & 0 deletions fmt/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ pub fn format<W: std::fmt::Write>(
Formatter::new(writer, parsed.src, parsed.comments, parsed.inline_config, config);
parsed.pt.visit(&mut formatter)
}

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

let mut output = String::new();
format(&mut output, parsed, FormatterConfig::default())?;

Ok(output)
}
2 changes: 1 addition & 1 deletion fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub use foundry_config::fmt::*;

pub use comments::Comments;
pub use formatter::{Formatter, FormatterError};
pub use helpers::{format, parse, Parsed};
pub use helpers::{fmt, format, parse, Parsed};
pub use inline_config::InlineConfig;
pub use visit::{Visitable, Visitor};
2 changes: 1 addition & 1 deletion testdata/cheats/Addr.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract AddrTest is DSTest {
}

function testAddr() public {
uint pk = 77814517325470205911140941194401928579557062014761831930645393041380819009408;
uint256 pk = 77814517325470205911140941194401928579557062014761831930645393041380819009408;
address expected = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;

assertEq(cheats.addr(pk), expected, "expected address did not match");
Expand Down
13 changes: 3 additions & 10 deletions testdata/cheats/Broadcast.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ contract Test is DSTest {
changed += 1;
}

function multiple_arguments(uint256 a, address b, uint256[] memory c)
public
returns (uint256)
{}
function multiple_arguments(uint256 a, address b, uint256[] memory c) public returns (uint256) {}

function echoSender() public view returns (address) {
return msg.sender;
Expand Down Expand Up @@ -300,9 +297,7 @@ contract TestInitialBalance is DSTest {

function runCustomSender() public {
// Make sure we're testing a different caller than the default one.
assert(
msg.sender != address(0x00a329c0648769A73afAc7F9381E08FB43dBEA72)
);
assert(msg.sender != address(0x00a329c0648769A73afAc7F9381E08FB43dBEA72));

// NodeConfig::test() sets the balance of the address used in this test to 100 ether.
assert(msg.sender.balance == 100 ether);
Expand All @@ -313,9 +308,7 @@ contract TestInitialBalance is DSTest {

function runDefaultSender() public {
// Make sure we're testing with the default caller.
assert(
msg.sender == address(0x00a329c0648769A73afAc7F9381E08FB43dBEA72)
);
assert(msg.sender == address(0x00a329c0648769A73afAc7F9381E08FB43dBEA72));

assert(msg.sender.balance == type(uint256).max);

Expand Down
66 changes: 35 additions & 31 deletions testdata/cheats/Cheats.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ pragma solidity >=0.8.0;

interface Cheats {
// This allows us to getRecordedLogs()
struct Log {bytes32[] topics; bytes data;}
struct Log {
bytes32[] topics;
bytes data;
}
// Set block.timestamp (newTimestamp)

function warp(uint256) external;
// Set block.difficulty (newDifficulty)
function difficulty(uint256) external;
Expand All @@ -15,11 +19,11 @@ interface Cheats {
// Set block.coinbase (who)
function coinbase(address) external;
// Loads a storage slot from an address (who, slot)
function load(address,bytes32) external returns (bytes32);
function load(address, bytes32) external returns (bytes32);
// Stores a value to an address' storage slot, (who, slot, value)
function store(address,bytes32,bytes32) external;
function store(address, bytes32, bytes32) external;
// Signs data, (privateKey, digest) => (v, r, s)
function sign(uint256,bytes32) external returns (uint8,bytes32,bytes32);
function sign(uint256, bytes32) external returns (uint8, bytes32, bytes32);
// Gets address for a given private key, (privateKey) => (address)
function addr(uint256) external returns (address);
// Derive a private key from a provided mnemonic string (or mnemonic file path) at the derivation path m/44'/60'/0'/0/{index}
Expand Down Expand Up @@ -51,9 +55,9 @@ interface Cheats {
// Sets all subsequent calls' msg.sender to be the input address until `stopPrank` is called
function startPrank(address) external;
// Sets the *next* call's msg.sender to be the input address, and the tx.origin to be the second input
function prank(address,address) external;
function prank(address, address) external;
// Sets all subsequent calls' msg.sender to be the input address until `stopPrank` is called, and the tx.origin to be the second input
function startPrank(address,address) external;
function startPrank(address, address) external;
// Resets subsequent calls' msg.sender to be `address(this)`
function stopPrank() external;
// Sets an address' balance, (who, newBalance)
Expand All @@ -76,33 +80,33 @@ interface Cheats {
// Call this function, then emit an event, then call a function. Internally after the call, we check if
// logs were emitted in the expected order with the expected topics and data (as specified by the booleans).
// Second form also checks supplied address against emitting contract.
function expectEmit(bool,bool,bool,bool) external;
function expectEmit(bool,bool,bool,bool,address) external;
function expectEmit(bool, bool, bool, bool) external;
function expectEmit(bool, bool, bool, bool, address) external;
// Mocks a call to an address, returning specified data.
// Calldata can either be strict or a partial match, e.g. if you only
// pass a Solidity selector to the expected calldata, then the entire Solidity
// function will be mocked.
function mockCall(address,bytes calldata,bytes calldata) external;
function mockCall(address, bytes calldata, bytes calldata) external;
// Mocks a call to an address with a specific msg.value, returning specified data.
// Calldata match takes precedence over msg.value in case of ambiguity.
function mockCall(address,uint256,bytes calldata,bytes calldata) external;
function mockCall(address, uint256, bytes calldata, bytes calldata) external;
// Clears all mocked calls
function clearMockedCalls() external;
// Expect a call to an address with the specified calldata.
// Calldata can either be strict or a partial match
function expectCall(address,bytes calldata) external;
function expectCall(address, bytes calldata) external;
// Expect a call to an address with the specified msg.value and calldata
function expectCall(address,uint256,bytes calldata) external;
function expectCall(address, uint256, bytes calldata) external;
// Gets the code from an artifact file. Takes in the relative path to the json file
function getCode(string calldata) external returns (bytes memory);
// Labels an address in call traces
function label(address, string calldata) external;
// If the condition is false, discard this run's fuzz inputs and generate new ones
function assume(bool) external;
// Set nonce for an account
function setNonce(address,uint64) external;
function setNonce(address, uint64) external;
// Get nonce for an account
function getNonce(address) external returns(uint64);
function getNonce(address) external returns (uint64);
// Set block.chainid (newChainId)
function chainId(uint256) external;
// Using the address that calls the test contract, has the next call (at this call depth only) create a transaction that can later be signed and sent onchain
Expand Down Expand Up @@ -137,33 +141,33 @@ interface Cheats {
// Path is relative to the project root. (path) => ()
function removeFile(string calldata) external;

function toString(address) external returns(string memory);
function toString(bytes calldata) external returns(string memory);
function toString(bytes32) external returns(string memory);
function toString(bool) external returns(string memory);
function toString(uint256) external returns(string memory);
function toString(int256) external returns(string memory);
function toString(address) external returns (string memory);
function toString(bytes calldata) external returns (string memory);
function toString(bytes32) external returns (string memory);
function toString(bool) external returns (string memory);
function toString(uint256) external returns (string memory);
function toString(int256) external returns (string memory);
// Snapshot the current state of the evm.
// Returns the id of the snapshot that was created.
// To revert a snapshot use `revertTo`
function snapshot() external returns(uint256);
function snapshot() external returns (uint256);
// Revert the state of the evm to a previous snapshot
// Takes the snapshot id to revert to.
// This deletes the snapshot and all snapshots taken after the given snapshot id.
function revertTo(uint256) external returns(bool);
function revertTo(uint256) external returns (bool);
// Creates a new fork with the given endpoint and block and returns the identifier of the fork
function createFork(string calldata,uint256) external returns(uint256);
function createFork(string calldata, uint256) external returns (uint256);
// Creates a new fork with the given endpoint and the _latest_ block and returns the identifier of the fork
function createFork(string calldata) external returns(uint256);
function createFork(string calldata) external returns (uint256);
// Creates _and_ also selects a new fork with the given endpoint and block and returns the identifier of the fork
function createSelectFork(string calldata,uint256) external returns(uint256);
function createSelectFork(string calldata, uint256) external returns (uint256);
// Creates _and_ also selects a new fork with the given endpoint and the latest block and returns the identifier of the fork
function createSelectFork(string calldata) external returns(uint256);
function createSelectFork(string calldata) external returns (uint256);
// Takes a fork identifier created by `createFork` and sets the corresponding forked state as active.
function selectFork(uint256) external;
// Returns the currently active fork
// Reverts if no fork is currently active
function activeFork() external returns(uint256);
function activeFork() external returns (uint256);
// Marks that the account(s) should use persistent storage across fork swaps.
// Meaning, changes made to the state of this account will be kept when switching forks
function makePersistent(address) external;
Expand All @@ -181,9 +185,9 @@ interface Cheats {
// Updates the given fork to given block number
function rollFork(uint256 forkId, uint256 blockNumber) external;
/// Returns the RPC url for the given alias
function rpcUrl(string calldata) external returns(string memory);
function rpcUrl(string calldata) external returns (string memory);
/// Returns all rpc urls and their aliases `[alias, url][]`
function rpcUrls() external returns(string[2][] memory);
function parseJson(string calldata, string calldata) external returns(bytes memory);
function parseJson(string calldata) external returns(bytes memory);
function rpcUrls() external returns (string[2][] memory);
function parseJson(string calldata, string calldata) external returns (bytes memory);
function parseJson(string calldata) external returns (bytes memory);
}
Loading

0 comments on commit 6b3db2a

Please sign in to comment.