forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
182 lines (160 loc) · 5.8 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#![doc = include_str!("../README.md")]
#![warn(missing_docs, unreachable_pub, unused_crate_dependencies, rust_2018_idioms)]
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, fmt};
mod cheatcode;
pub use cheatcode::{Cheatcode, CheatcodeDef, Group, Safety, Status};
mod function;
pub use function::{Function, Mutability, Visibility};
mod items;
pub use items::{Enum, EnumVariant, Error, Event, Struct, StructField};
mod vm;
pub use vm::Vm;
// The `cheatcodes.json` schema.
/// Foundry cheatcodes. Learn more: <https://book.getfoundry.sh/cheatcodes/>
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "camelCase")]
pub struct Cheatcodes<'a> {
/// Cheatcode errors.
#[serde(borrow)]
pub errors: Cow<'a, [Error<'a>]>,
/// Cheatcode events.
#[serde(borrow)]
pub events: Cow<'a, [Event<'a>]>,
/// Cheatcode enums.
#[serde(borrow)]
pub enums: Cow<'a, [Enum<'a>]>,
/// Cheatcode structs.
#[serde(borrow)]
pub structs: Cow<'a, [Struct<'a>]>,
/// All the cheatcodes.
#[serde(borrow)]
pub cheatcodes: Cow<'a, [Cheatcode<'a>]>,
}
impl fmt::Display for Cheatcodes<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for error in self.errors.iter() {
writeln!(f, "{error}")?;
}
for event in self.events.iter() {
writeln!(f, "{event}")?;
}
for enumm in self.enums.iter() {
writeln!(f, "{enumm}")?;
}
for strukt in self.structs.iter() {
writeln!(f, "{strukt}")?;
}
for cheatcode in self.cheatcodes.iter() {
writeln!(f, "{}", cheatcode.func)?;
}
Ok(())
}
}
impl Default for Cheatcodes<'static> {
fn default() -> Self {
Self::new()
}
}
impl Cheatcodes<'static> {
/// Returns the default cheatcodes.
pub fn new() -> Self {
Self {
// unfortunately technology has not yet advanced to the point where we can get all
// items of a certain type in a module, so we have to hardcode them here
structs: Cow::Owned(vec![
Vm::Log::STRUCT.clone(),
Vm::Rpc::STRUCT.clone(),
Vm::EthGetLogs::STRUCT.clone(),
Vm::DirEntry::STRUCT.clone(),
Vm::FsMetadata::STRUCT.clone(),
Vm::Wallet::STRUCT.clone(),
Vm::FfiResult::STRUCT.clone(),
Vm::ChainInfo::STRUCT.clone(),
Vm::AccountAccess::STRUCT.clone(),
Vm::StorageAccess::STRUCT.clone(),
]),
enums: Cow::Owned(vec![
Vm::CallerMode::ENUM.clone(),
Vm::AccountAccessKind::ENUM.clone(),
]),
errors: Vm::VM_ERRORS.iter().map(|&x| x.clone()).collect(),
events: Cow::Borrowed(&[]),
// events: Vm::VM_EVENTS.iter().map(|&x| x.clone()).collect(),
cheatcodes: Vm::CHEATCODES.iter().map(|&x| x.clone()).collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{fs, path::Path};
const JSON_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../assets/cheatcodes.json");
#[cfg(feature = "schema")]
const SCHEMA_PATH: &str =
concat!(env!("CARGO_MANIFEST_DIR"), "/../assets/cheatcodes.schema.json");
const IFACE_PATH: &str =
concat!(env!("CARGO_MANIFEST_DIR"), "/../../../testdata/cheats/Vm.sol");
/// Generates the `cheatcodes.json` file contents.
fn json_cheatcodes() -> String {
serde_json::to_string_pretty(&Cheatcodes::new()).unwrap()
}
/// Generates the [cheatcodes](json_cheatcodes) JSON schema.
#[cfg(feature = "schema")]
fn json_schema() -> String {
serde_json::to_string_pretty(&schemars::schema_for!(Cheatcodes<'_>)).unwrap()
}
fn sol_iface() -> String {
let mut cheats = Cheatcodes::new();
cheats.errors = Default::default(); // Skip errors to allow <0.8.4.
let cheats = cheats.to_string().trim().replace('\n', "\n ");
format!(
"\
// Automatically generated from `foundry-cheatcodes` Vm definitions. Do not modify manually.
// This interface is just for internal testing purposes. Use `forge-std` instead.
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.2 <0.9.0;
pragma experimental ABIEncoderV2;
interface Vm {{
{cheats}
}}
"
)
}
#[test]
fn spec_up_to_date() {
ensure_file_contents(Path::new(JSON_PATH), &json_cheatcodes());
}
#[test]
#[cfg(feature = "schema")]
fn schema_up_to_date() {
ensure_file_contents(Path::new(SCHEMA_PATH), &json_schema());
}
#[test]
fn iface_up_to_date() {
ensure_file_contents(Path::new(IFACE_PATH), &sol_iface());
}
/// Checks that the `file` has the specified `contents`. If that is not the
/// case, updates the file and then fails the test.
fn ensure_file_contents(file: &Path, contents: &str) {
if let Ok(old_contents) = fs::read_to_string(file) {
if normalize_newlines(&old_contents) == normalize_newlines(contents) {
// File is already up to date.
return
}
}
eprintln!("\n\x1b[31;1merror\x1b[0m: {} was not up-to-date, updating\n", file.display());
if std::env::var("CI").is_ok() {
eprintln!(" NOTE: run `cargo cheats` locally and commit the updated files\n");
}
if let Some(parent) = file.parent() {
let _ = fs::create_dir_all(parent);
}
fs::write(file, contents).unwrap();
panic!("some file was not up to date and has been updated, simply re-run the tests");
}
fn normalize_newlines(s: &str) -> String {
s.replace("\r\n", "\n")
}
}