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
134 lines (116 loc) · 3.73 KB
/
test.rs
File metadata and controls
134 lines (116 loc) · 3.73 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
#![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::{get_project_root, BuildFiles};
use fe_driver::CompiledTest;
use fe_test_runner::TestSink;
#[derive(Args)]
#[clap(about = "Execute tests in the current project")]
pub struct TestArgs {
#[clap(default_value_t = get_project_root().unwrap_or(".".to_string()))]
input_path: String,
#[clap(long, takes_value(true))]
filter: Option<String>,
#[clap(long, takes_value(true))]
optimize: Option<bool>,
#[clap(long)]
logs: 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)
}
}
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_single_file(args: &TestArgs) -> TestSink {
let input_path = &args.input_path;
let optimize = args.optimize.unwrap_or(true);
let logs = args.logs;
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, optimize) {
Ok((name, tests)) => {
let mut sink = TestSink::new(logs);
execute_tests(&name, &tests, &mut sink);
sink
}
Err(error) => {
eprintln!("Unable to compile {input_path}.");
print_diagnostics(&db, &error.0);
std::process::exit(1)
}
}
}
fn test_ingot(args: &TestArgs) -> TestSink {
let input_path = &args.input_path;
let optimize = args.optimize.unwrap_or(true);
let logs = args.logs;
if !Path::new(input_path).exists() {
eprintln!("Input directory does not exist: `{input_path}`.");
std::process::exit(1)
}
let build_files = match BuildFiles::load_fs(input_path) {
Ok(files) => files,
Err(err) => {
eprintln!("Failed to load project files.\nError: {err}");
std::process::exit(1)
}
};
let mut db = fe_driver::Db::default();
match fe_driver::compile_ingot_tests(&mut db, &build_files, optimize) {
Ok(test_batches) => {
let mut sink = TestSink::new(logs);
for (module_name, tests) in test_batches {
let tests = filter_tests(&tests, &args.filter);
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)
}
}
}
fn filter_tests(tests: &[CompiledTest], filter: &Option<String>) -> Vec<CompiledTest> {
match filter {
Some(word) if !word.is_empty() => tests
.iter()
.filter(|test| test.name.contains(word))
.cloned()
.collect(),
Some(_) | None => tests.to_vec(),
}
}