|
| 1 | +use std::path::PathBuf; |
1 | 2 | use std::process::Command; |
| 3 | +use walkdir::WalkDir; |
2 | 4 |
|
3 | 5 | use crate::{cli::TestArgs, shell::Shell}; |
4 | 6 |
|
| 7 | +fn collect_ts_files(path: &PathBuf) -> anyhow::Result<Vec<PathBuf>> { |
| 8 | + let mut files = Vec::new(); |
| 9 | + |
| 10 | + if path.is_file() { |
| 11 | + if path.extension().and_then(|e| e.to_str()) == Some("ts") { |
| 12 | + files.push(path.clone()); |
| 13 | + } |
| 14 | + } else if path.is_dir() { |
| 15 | + for entry in WalkDir::new(path) |
| 16 | + .follow_links(false) |
| 17 | + .into_iter() |
| 18 | + .filter_map(|e| e.ok()) |
| 19 | + { |
| 20 | + let entry_path = entry.path(); |
| 21 | + if entry_path.is_file() && entry_path.extension().and_then(|e| e.to_str()) == Some("ts") |
| 22 | + { |
| 23 | + files.push(entry_path.to_path_buf()); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + Ok(files) |
| 29 | +} |
| 30 | + |
5 | 31 | pub async fn test(_shell: &mut Shell, args: TestArgs) -> anyhow::Result<()> { |
6 | 32 | let exe_path = std::env::current_exe()?; |
7 | 33 |
|
| 34 | + let mut test_files = Vec::new(); |
| 35 | + |
| 36 | + if args.files.is_empty() { |
| 37 | + let default_tests_dir = PathBuf::from("tests"); |
| 38 | + if default_tests_dir.exists() && default_tests_dir.is_dir() { |
| 39 | + test_files = collect_ts_files(&default_tests_dir)?; |
| 40 | + } |
| 41 | + |
| 42 | + if test_files.is_empty() { |
| 43 | + anyhow::bail!("No test files found. Either create a 'tests/' directory with .ts files, or specify test files/directories as arguments."); |
| 44 | + } |
| 45 | + } else { |
| 46 | + for path in &args.files { |
| 47 | + let mut collected = collect_ts_files(path)?; |
| 48 | + test_files.append(&mut collected); |
| 49 | + } |
| 50 | + |
| 51 | + if test_files.is_empty() { |
| 52 | + anyhow::bail!("No .ts test files found in the specified paths."); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + test_files.sort(); |
| 57 | + |
8 | 58 | let mut cmd = Command::new("deno"); |
9 | 59 | cmd.arg("test") |
10 | 60 | .arg("--allow-ffi") |
11 | 61 | .arg("--allow-env") |
12 | 62 | .arg("--no-check") |
13 | 63 | .env("LIB_PATH", exe_path); |
14 | 64 |
|
15 | | - for file in &args.files { |
| 65 | + for file in &test_files { |
16 | 66 | cmd.arg(file); |
17 | 67 | } |
18 | 68 |
|
|
0 commit comments