-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: integrate inline config in tests (#6473)
- Loading branch information
Showing
8 changed files
with
132 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/// Errors returned by the [`InlineConfigParser`] trait. | ||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] | ||
pub enum InlineConfigParserError { | ||
/// An invalid configuration property has been provided. | ||
/// The property cannot be mapped to the configuration object | ||
#[error("'{0}' is an invalid config property")] | ||
InvalidConfigProperty(String), | ||
/// An invalid profile has been provided | ||
#[error("'{0}' specifies an invalid profile. Available profiles are: {1}")] | ||
InvalidProfile(String, String), | ||
/// An error occurred while trying to parse an integer configuration value | ||
#[error("Invalid config value for key '{0}'. Unable to parse '{1}' into an integer value")] | ||
ParseInt(String, String), | ||
/// An error occurred while trying to parse a boolean configuration value | ||
#[error("Invalid config value for key '{0}'. Unable to parse '{1}' into a boolean value")] | ||
ParseBool(String, String), | ||
} | ||
|
||
/// Wrapper error struct that catches config parsing | ||
/// errors [`InlineConfigParserError`], enriching them with context information | ||
/// reporting the misconfigured line. | ||
#[derive(thiserror::Error, Debug)] | ||
#[error("Inline config error detected at {line}")] | ||
pub struct InlineConfigError { | ||
/// Specifies the misconfigured line. This is something of the form | ||
/// `dir/TestContract.t.sol:FuzzContract:10:12:111` | ||
pub line: String, | ||
/// The inner error | ||
pub source: InlineConfigParserError, | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn can_format_inline_config_errors() { | ||
let source = InlineConfigParserError::ParseBool("key".into(), "invalid-bool-value".into()); | ||
let line = "dir/TestContract.t.sol:FuzzContract".to_string(); | ||
let error = InlineConfigError { line: line.clone(), source }; | ||
|
||
let expected = format!("Inline config error detected at {line}"); | ||
assert_eq!(error.to_string(), expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity 0.8.18; | ||
|
||
import "ds-test/test.sol"; | ||
import "../cheats/Vm.sol"; | ||
|
||
// https://github.com/foundry-rs/foundry/issues/5948 | ||
contract Issue5948Test is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
|
||
/// forge-config: default.fuzz.runs = 2 | ||
function testSleepFuzzed(uint256 _milliseconds) public { | ||
// Limit sleep time to 2 seconds to decrease test time | ||
uint256 milliseconds = _milliseconds % 2000; | ||
|
||
string[] memory inputs = new string[](2); | ||
inputs[0] = "date"; | ||
// OS X does not support precision more than 1 second | ||
inputs[1] = "+%s000"; | ||
|
||
bytes memory res = vm.ffi(inputs); | ||
uint256 start = vm.parseUint(string(res)); | ||
|
||
vm.sleep(milliseconds); | ||
|
||
res = vm.ffi(inputs); | ||
uint256 end = vm.parseUint(string(res)); | ||
|
||
// Limit precision to 1000 ms | ||
assertGe(end - start, milliseconds / 1000 * 1000, "sleep failed"); | ||
} | ||
} |