forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
441 lines (398 loc) · 13.3 KB
/
Copy pathtest.rs
File metadata and controls
441 lines (398 loc) · 13.3 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Test runner for Fe tests.
//!
//! Discovers functions marked with `#[test]` attribute, compiles them, and
//! executes them using revm.
use camino::Utf8PathBuf;
use codegen::{ExpectedRevert, TestMetadata, emit_test_module_yul};
use colored::Colorize;
use common::InputDb;
use contract_harness::{ExecutionOptions, RuntimeInstance};
use driver::DriverDataBase;
use hir::hir_def::{HirIngot, TopLevelMod};
use solc_runner::compile_single_contract;
use url::Url;
/// Result of running a single test.
#[derive(Debug)]
pub struct TestResult {
pub name: String,
pub passed: bool,
pub error_message: Option<String>,
}
#[derive(Debug)]
struct TestOutcome {
result: TestResult,
logs: Vec<String>,
}
/// Run tests in the given path.
///
/// # Arguments
/// * `path` - Path to a .fe file or directory containing an ingot
/// * `filter` - Optional filter pattern for test names
/// * `show_logs` - Whether to show event logs from test execution
///
/// Returns nothing; exits the process on invalid input or test failures.
pub fn run_tests(path: &Utf8PathBuf, filter: Option<&str>, show_logs: bool) {
let mut db = DriverDataBase::default();
// Determine if we're dealing with a single file or an ingot directory
let test_results = if path.is_file() && path.extension() == Some("fe") {
run_tests_single_file(&mut db, path, filter, show_logs)
} else if path.is_dir() {
run_tests_ingot(&mut db, path, filter, show_logs)
} else {
eprintln!("Error: Path must be either a .fe file or a directory containing fe.toml");
std::process::exit(1);
};
// Print summary
print_summary(&test_results);
// Exit with code 1 if any tests failed
if test_results.iter().any(|r| !r.passed) {
std::process::exit(1);
}
}
/// Runs tests defined in a single `.fe` source file.
///
/// * `db` - Driver database used for compilation.
/// * `file_path` - Path to the `.fe` file.
/// * `filter` - Optional substring filter for test names.
/// * `show_logs` - Whether to show event logs from test execution.
///
/// Returns the collected test results.
fn run_tests_single_file(
db: &mut DriverDataBase,
file_path: &Utf8PathBuf,
filter: Option<&str>,
show_logs: bool,
) -> Vec<TestResult> {
// Create a file URL for the single .fe file
let file_url = match Url::from_file_path(file_path.canonicalize_utf8().unwrap()) {
Ok(url) => url,
Err(_) => {
eprintln!("Error: Invalid file path: {file_path}");
std::process::exit(1);
}
};
// Read the file content
let content = match std::fs::read_to_string(file_path) {
Ok(content) => content,
Err(err) => {
eprintln!("Error reading file {file_path}: {err}");
std::process::exit(1);
}
};
// Add the file to the workspace
db.workspace().touch(db, file_url.clone(), Some(content));
// Get the top-level module
let Some(file) = db.workspace().get(db, &file_url) else {
eprintln!("Error: Could not process file {file_path}");
std::process::exit(1);
};
let top_mod = db.top_mod(file);
// Check for compilation errors first
let diags = db.run_on_top_mod(top_mod);
if !diags.is_empty() {
eprintln!("Compilation errors in {file_url}");
eprintln!();
diags.emit(db);
std::process::exit(1);
}
// Discover and run tests
discover_and_run_tests(db, top_mod, filter, show_logs)
}
/// Runs tests in an ingot directory (containing `fe.toml`).
///
/// * `db` - Driver database used for compilation.
/// * `dir_path` - Path to the ingot directory.
/// * `filter` - Optional substring filter for test names.
/// * `show_logs` - Whether to show event logs from test execution.
///
/// Returns the collected test results.
fn run_tests_ingot(
db: &mut DriverDataBase,
dir_path: &Utf8PathBuf,
filter: Option<&str>,
show_logs: bool,
) -> Vec<TestResult> {
let canonical_path = match dir_path.canonicalize_utf8() {
Ok(path) => path,
Err(_) => {
eprintln!("Error: Invalid or non-existent directory path: {dir_path}");
std::process::exit(1);
}
};
let ingot_url = match Url::from_directory_path(canonical_path.as_str()) {
Ok(url) => url,
Err(_) => {
eprintln!("Error: Invalid directory path: {dir_path}");
std::process::exit(1);
}
};
let had_init_diagnostics = driver::init_ingot(db, &ingot_url);
if had_init_diagnostics {
std::process::exit(1);
}
let Some(ingot) = db.workspace().containing_ingot(db, ingot_url.clone()) else {
eprintln!("Error: Could not resolve ingot from directory");
std::process::exit(1);
};
// Check for compilation errors
let diags = db.run_on_ingot(ingot);
if !diags.is_empty() {
diags.emit(db);
std::process::exit(1);
}
let root_mod = ingot.root_mod(db);
discover_and_run_tests(db, root_mod, filter, show_logs)
}
/// Discovers `#[test]` functions, compiles them, and executes each one.
///
/// * `db` - Driver database used for compilation.
/// * `top_mod` - Root module to scan for tests.
/// * `filter` - Optional substring filter for test names.
/// * `show_logs` - Whether to show event logs from test execution.
///
/// Returns the collected test results.
fn discover_and_run_tests(
db: &DriverDataBase,
top_mod: TopLevelMod<'_>,
filter: Option<&str>,
show_logs: bool,
) -> Vec<TestResult> {
let output = match emit_test_module_yul(db, top_mod) {
Ok(output) => output,
Err(err) => {
eprintln!("Failed to emit test Yul: {err}");
std::process::exit(1);
}
};
if output.tests.is_empty() {
eprintln!("No tests found");
return Vec::new();
}
let mut results = Vec::new();
for case in &output.tests {
// Apply filter if provided
if let Some(pattern) = filter
&& !case.hir_name.contains(pattern)
&& !case.symbol_name.contains(pattern)
&& !case.display_name.contains(pattern)
{
continue;
}
// Print test name
print!("test {} ... ", case.display_name);
// Compile and run the test
let outcome = compile_and_run_test(case, show_logs);
if outcome.result.passed {
println!("{}", "ok".green());
} else {
println!("{}", "FAILED".red());
if let Some(ref msg) = outcome.result.error_message {
eprintln!(" {}", msg);
}
}
if show_logs {
if !outcome.logs.is_empty() {
for log in &outcome.logs {
println!(" log {}", log);
}
} else if outcome.result.passed {
println!(" log (none)");
} else {
println!(" log (unavailable for failed tests)");
}
}
results.push(outcome.result);
}
results
}
/// Compiles a test function to bytecode and executes it in revm.
///
/// * `case` - Test metadata describing the Yul object and parameters.
/// * `show_logs` - Whether to capture EVM logs for the test run.
///
/// Returns the test outcome (result + logs).
fn compile_and_run_test(case: &TestMetadata, show_logs: bool) -> TestOutcome {
if case.value_param_count > 0 {
return TestOutcome {
result: TestResult {
name: case.display_name.clone(),
passed: false,
error_message: Some(format!(
"tests with value parameters are not supported (found {})",
case.value_param_count
)),
},
logs: Vec::new(),
};
}
if case.object_name.trim().is_empty() {
return TestOutcome {
result: TestResult {
name: case.display_name.clone(),
passed: false,
error_message: Some(format!(
"missing test object name for `{}`",
case.display_name
)),
},
logs: Vec::new(),
};
}
// Compile to bytecode using solc
if case.yul.trim().is_empty() {
return TestOutcome {
result: TestResult {
name: case.display_name.clone(),
passed: false,
error_message: Some(format!("missing test Yul for `{}`", case.display_name)),
},
logs: Vec::new(),
};
}
let bytecode = match compile_single_contract(&case.object_name, &case.yul, false, true) {
Ok(contract) => contract.bytecode,
Err(err) => {
return TestOutcome {
result: TestResult {
name: case.display_name.clone(),
passed: false,
error_message: Some(format!("Failed to compile test: {}", err.0)),
},
logs: Vec::new(),
};
}
};
// Execute the test bytecode in revm
let (result, logs) = execute_test(
&case.display_name,
&bytecode,
show_logs,
case.expected_revert.as_ref(),
);
TestOutcome { result, logs }
}
/// Deploys and executes compiled test bytecode in revm.
///
/// The test passes if the function returns normally, fails if it reverts.
/// When `expected_revert` is set, the logic is inverted: the test passes if it reverts.
///
/// * `name` - Display name used for reporting.
/// * `bytecode_hex` - Hex-encoded init bytecode for the test object.
/// * `show_logs` - Whether to execute with log collection enabled.
/// * `expected_revert` - If set, the test is expected to revert.
///
/// Returns the test result and any emitted logs.
fn execute_test(
name: &str,
bytecode_hex: &str,
show_logs: bool,
expected_revert: Option<&ExpectedRevert>,
) -> (TestResult, Vec<String>) {
// Deploy the test contract
let mut instance = match RuntimeInstance::deploy(bytecode_hex) {
Ok(instance) => instance,
Err(err) => {
return (
TestResult {
name: name.to_string(),
passed: false,
error_message: Some(format!("Failed to deploy test: {err}")),
},
Vec::new(),
);
}
};
// Execute the test (empty calldata since test functions take no args)
let options = ExecutionOptions::default();
let call_result = if show_logs {
instance
.call_raw_with_logs(&[], options)
.map(|outcome| outcome.logs)
} else {
instance.call_raw(&[], options).map(|_| Vec::new())
};
match (call_result, expected_revert) {
// Normal test: execution succeeded
(Ok(logs), None) => (
TestResult {
name: name.to_string(),
passed: true,
error_message: None,
},
logs,
),
// Normal test: execution reverted (failure)
(Err(err), None) => (
TestResult {
name: name.to_string(),
passed: false,
error_message: Some(format_harness_error(err)),
},
Vec::new(),
),
// Expected revert: execution succeeded (failure - should have reverted)
(Ok(_), Some(_)) => (
TestResult {
name: name.to_string(),
passed: false,
error_message: Some("Expected test to revert, but it succeeded".to_string()),
},
Vec::new(),
),
// Expected revert: execution reverted (success)
(Err(_err), Some(ExpectedRevert::Any)) => (
TestResult {
name: name.to_string(),
passed: true,
error_message: None,
},
Vec::new(),
),
// Future: match specific revert data
// (Err(HarnessError::Revert(data)), Some(ExpectedRevert::ExactData(expected))) => { ... }
}
}
/// Formats a harness error into a human-readable message.
fn format_harness_error(err: contract_harness::HarnessError) -> String {
match err {
contract_harness::HarnessError::Revert(data) => format!("Test reverted: {data}"),
contract_harness::HarnessError::Halted { reason, gas_used } => {
format!("Test halted: {reason:?} (gas: {gas_used})")
}
other => format!("Test execution error: {other}"),
}
}
/// Prints a summary for the completed test run.
///
/// * `results` - Per-test results to summarize.
///
/// Returns nothing.
fn print_summary(results: &[TestResult]) {
if results.is_empty() {
return;
}
let passed = results.iter().filter(|r| r.passed).count();
let failed = results.len() - passed;
println!();
if failed == 0 {
println!(
"test result: {}. {} passed; {} failed",
"ok".green(),
passed,
failed
);
} else {
println!(
"test result: {}. {} passed; {} failed",
"FAILED".red(),
passed,
failed
);
// Print failed tests
println!();
println!("failures:");
for result in results.iter().filter(|r| !r.passed) {
println!(" {}", result.name);
}
}
}