-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.rs
More file actions
153 lines (136 loc) · 4.72 KB
/
Copy pathtest_runner.rs
File metadata and controls
153 lines (136 loc) · 4.72 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
use colored::*;
pub fn run_tests(extra: &[String], verbose: bool) -> anyhow::Result<()> {
println!();
println!(" {} H# Test Runner", "bytes test".cyan().bold());
println!();
let files = collect_test_files(extra);
if files.is_empty() {
println!(" {}", "No .h# files found to test.".dimmed());
return Ok(());
}
let mut total_passed = 0usize;
let mut total_failed = 0usize;
let mut total_files = 0usize;
for file in &files {
let (passed, failed) = run_file_tests(file, verbose)?;
total_passed += passed;
total_failed += failed;
if passed + failed > 0 { total_files += 1; }
}
println!();
println!(" {}", "─".repeat(48).dimmed());
if total_failed == 0 {
println!(
" {} {} test(s) passed in {} file(s)",
"✓".green().bold(), total_passed, total_files,
);
} else {
println!(
" {} {}/{} passed, {} failed",
"✗".red().bold(),
total_passed, total_passed + total_failed,
total_failed,
);
std::process::exit(1);
}
Ok(())
}
fn collect_test_files(extra: &[String]) -> Vec<String> {
if !extra.is_empty() {
return extra.iter().filter(|s| !s.starts_with("--")).cloned().collect();
}
walkdir::WalkDir::new(".")
.max_depth(6).into_iter().flatten()
.filter(|e| {
let p = e.path();
let ext = p.extension().and_then(|x| x.to_str());
let is_test = p.to_string_lossy().contains("test")
|| p.to_string_lossy().contains("spec")
|| ext == Some("h#") || ext == Some("hsp");
e.file_type().is_file()
&& is_test
&& !p.starts_with("./build")
&& !p.starts_with("./.cache")
})
.map(|e| e.path().display().to_string())
.collect()
}
fn run_file_tests(file: &str, verbose: bool) -> anyhow::Result<(usize, usize)> {
let source = match std::fs::read_to_string(file) {
Ok(s) => s,
Err(e) => { eprintln!(" {} {}: {}", "warn:".yellow(), file, e); return Ok((0, 0)); }
};
let test_fns = collect_test_functions(&source);
if test_fns.is_empty() { return Ok((0, 0)); }
println!(" {} {}", "Testing:".cyan().bold(), file.dimmed());
let mut passed = 0usize;
let mut failed = 0usize;
if try_hsharp_test(file, verbose) {
passed = test_fns.len();
} else {
for fn_name in &test_fns {
match run_single_test(file, fn_name, verbose) {
Ok(true) => { passed += 1; println!(" {} {}", "✓".green(), fn_name.dimmed()); }
Ok(false) | Err(_) => {
failed += 1;
println!(" {} {}", "✗".red(), fn_name);
}
}
}
}
println!(
" {} {}: {}/{} passed",
if failed == 0 { "✓".green() } else { "✗".red() },
file, passed, passed + failed,
);
Ok((passed, failed))
}
fn collect_test_functions(source: &str) -> Vec<String> {
let mut fns = Vec::new();
let mut lines = source.lines().peekable();
while let Some(line) = lines.next() {
let trimmed = line.trim();
if trimmed == "#[test]" || trimmed.starts_with("#[test]") {
while let Some(next) = lines.peek() {
let nt = next.trim();
if nt.is_empty() { lines.next(); continue; }
if nt.starts_with("fn ") || nt.starts_with("pub fn ") {
let name = nt
.trim_start_matches("pub ")
.trim_start_matches("fn ")
.split('(').next().unwrap_or("").trim().to_string();
if !name.is_empty() { fns.push(name); }
}
break;
}
}
}
fns
}
fn try_hsharp_test(file: &str, _verbose: bool) -> bool {
std::process::Command::new("hsharp")
.args(["preview", "--test", file])
.status()
.map(|s| s.success())
.unwrap_or(false)
}
fn run_single_test(file: &str, fn_name: &str, _verbose: bool) -> anyhow::Result<bool> {
// Build a temp file: original source + a main that calls the test fn
let orig = std::fs::read_to_string(file)?;
let full_src = format!(
"{}\n\nfn __bytes_test_entry__() is\n {}()\nend\n",
orig, fn_name
);
// Write to a temp file using prefix (with_suffix not available in tempfile 3.8)
let tmp = tempfile::Builder::new()
.prefix("bytes_test_")
.suffix(".h#")
.tempfile()?;
std::fs::write(tmp.path(), &full_src)?;
let ok = std::process::Command::new("hsharp")
.args(["preview", tmp.path().to_str().unwrap_or("")])
.status()
.map(|s| s.success())
.unwrap_or(false);
Ok(ok)
}