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
117 lines (101 loc) · 3.23 KB
/
Copy pathtest.rs
File metadata and controls
117 lines (101 loc) · 3.23 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
#![cfg(feature = "solc-backend")]
use std::path::Path;
use clap::Args;
use colored::Colorize;
use fe_common::diagnostics::print_diagnostics;
use fe_common::utils::files::load_fe_files_from_dir;
use fe_driver::CompiledTest;
use fe_test_runner::TestSink;
#[derive(Args)]
#[clap(about = "Execute tests in the current project")]
pub struct TestArgs {
input_path: String,
#[clap(long, takes_value(true))]
optimize: Option<bool>,
}
pub fn test(args: TestArgs) {
let path = &args.input_path;
let test_sink = if Path::new(path).is_file() {
test_single_file(&args)
} else {
test_ingot(&args)
};
println!("{test_sink}");
if test_sink.failure_count() != 0 {
std::process::exit(1)
}
}
fn test_single_file(args: &TestArgs) -> TestSink {
let input_path = &args.input_path;
let mut db = fe_driver::Db::default();
let content = match std::fs::read_to_string(input_path) {
Err(err) => {
eprintln!("Failed to load file: `{input_path}`. Error: {err}");
std::process::exit(1)
}
Ok(content) => content,
};
match fe_driver::compile_single_file_tests(&mut db, input_path, &content, true) {
Ok((name, tests)) => {
let mut sink = TestSink::default();
execute_tests(&name, &tests, &mut sink);
sink
}
Err(error) => {
eprintln!("Unable to compile {input_path}.");
print_diagnostics(&db, &error.0);
std::process::exit(1)
}
}
}
pub fn execute_tests(module_name: &str, tests: &[CompiledTest], sink: &mut TestSink) {
if tests.len() == 1 {
println!("executing 1 test in {module_name}:");
} else {
println!("executing {} tests in {}:", tests.len(), module_name);
}
for test in tests {
print!(" {} ...", test.name);
let test_passed = test.execute(sink);
if test_passed {
println!(" {}", "passed".green())
} else {
println!(" {}", "failed".red())
}
}
println!();
}
fn test_ingot(args: &TestArgs) -> TestSink {
let input_path = &args.input_path;
let optimize = args.optimize.unwrap_or(true);
if !Path::new(input_path).exists() {
eprintln!("Input directory does not exist: `{input_path}`.");
std::process::exit(1)
}
let content = match load_fe_files_from_dir(input_path) {
Ok(files) if files.is_empty() => {
eprintln!("Input directory is not an ingot: `{input_path}`");
std::process::exit(1)
}
Ok(files) => files,
Err(err) => {
eprintln!("Failed to load project files. Error: {err}");
std::process::exit(1)
}
};
let mut db = fe_driver::Db::default();
match fe_driver::compile_ingot_tests(&mut db, input_path, &content, optimize) {
Ok(test_batches) => {
let mut sink = TestSink::default();
for (module_name, tests) in test_batches {
execute_tests(&module_name, &tests, &mut sink);
}
sink
}
Err(error) => {
eprintln!("Unable to compile {input_path}.");
print_diagnostics(&db, &error.0);
std::process::exit(1)
}
}
}