1use core::fmt;
2use std::{
3  io,
4  path::PathBuf,
5  process::{ExitCode, Termination},
6  sync::Arc,
7  time::{Duration, Instant},
8};
9
10use colored::Colorize;
11use itertools::{Either, Itertools};
12use tokio::{
13  fs::remove_dir_all,
14  sync::{Mutex, Semaphore},
15};
16
17use crate::{
18  Args,
19  assert::{AssertError, DisplayErrs},
20  config::FullConfig,
21};
22
23pub(crate) const GOLDEN_DIR: &str = "__golden__";
24
25#[derive(Debug, thiserror::Error)]
26pub enum BuildError {
27  #[error("file \"{0}\": {1}")]
28  Toml(PathBuf, toml::de::Error),
29  #[error("task \"{0}\": its permit = {1}, exceed total permits = {2}")]
30  PermitEcxceed(PathBuf, u32, u32),
31  #[error("task \"{0}\": need to specify '{1}'")]
32  MissConfig(PathBuf, &'static str),
33  #[error("file \"{0}\": {1}")]
34  UnableToRead(PathBuf, io::Error),
35  #[error("read dir \"{0}\": {1}")]
36  ReadDir(PathBuf, io::Error),
37  #[error("clean dir \"{0}\": {1}")]
38  CleanDir(PathBuf, io::Error),
39  #[error("input extensions can not contains 'toml'")]
40  InputExtToml,
41}
42
43#[derive(Debug)]
44pub(crate) enum FailedState {
45  ReportSaved(PathBuf),
46  NoReport(PathBuf, Vec<AssertError>),
47}
48pub(crate) enum State {
49  Ok(Option<Duration>),
50  Failed(Option<(FailedState, Duration)>),
51  Ignored,
52  FilteredOut,
53}
54
55impl fmt::Display for FailedState {
56  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57    match self {
58      Self::ReportSaved(report) => {
59        write!(f, "\n     report: {}", report.display())
60      }
61      Self::NoReport(input, errs) => {
62        write!(f, "\n----------- {} -----------\n{}", input.display(), DisplayErrs(errs))
63      }
64    }
65  }
66}
67impl fmt::Display for State {
68  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69    match self {
70      Self::Ok(None) => write!(f, "{}", "ok".green()),
71      Self::Ok(Some(time)) => write!(f, "{:.2}s {}", time.as_secs_f32(), "ok".green()),
72      Self::Failed(Some((_, time))) => {
73        write!(f, "{:.2}s {}", time.as_secs_f32(), "FAILED".red())
74      }
75      Self::Failed(None) => write!(f, "{}", "FAILED".red()),
76      Self::Ignored => write!(f, "{}", "ignored".yellow()),
77      Self::FilteredOut => write!(f, "{}", "filtered out".bright_black()),
78    }
79  }
80}
81
82pub(crate) struct TestResult {
83  count_ok: usize,
84  count_ignored: usize,
85  count_filtered: usize,
86  faileds: Vec<FailedState>,
87}
88
89pub struct TestExitCode(Result<TestResult, Vec<BuildError>>, Instant);
90
91impl Termination for TestExitCode {
92  fn report(self) -> ExitCode {
93    let time = self.1.elapsed().as_secs_f32();
94    match self.0 {
95      Ok(TestResult { count_ok, count_ignored, count_filtered, faileds }) => {
96        println!();
97        let failed_num = faileds.len();
98        if failed_num == 0 {
99          println!(
100            "test result: {}. {count_ok} passed; {failed_num} failed; {count_ignored} ignored; {count_filtered} filtered out; finished in {time:.2}s",
101            State::Ok(None)
102          );
103          ExitCode::SUCCESS
104        } else {
105          eprint!("failures:");
106          for failed in &faileds {
107            eprint!("{failed}");
108          }
109          eprintln!(
110            "\n\ntest result: {}. {count_ok} passed; {failed_num} failed; {count_ignored} ignored; {count_filtered} filtered out; finished in {time:.2}s",
111            State::Failed(None)
112          );
113          ExitCode::FAILURE
114        }
115      }
116      Err(build_errs) => {
117        eprintln!("Fail to build test:");
118        for err in &build_errs {
119          eprintln!("{err}");
120        }
121        ExitCode::FAILURE
122      }
123    }
124  }
125}
126
127impl Args {
128  pub async fn test(self) -> TestExitCode {
129    let now = Instant::now();
130    TestExitCode(
131      match self.rebuild() {
132        Ok(args) => _test(args).await,
133        Err(e) => Err(vec![e]),
134      },
135      now,
136    )
137  }
138}
139async fn _test(args: &'static Args) -> Result<TestResult, Vec<BuildError>> {
140  let f1 = async {
141    if args.workdir.exists() {
142      remove_dir_all(&args.workdir)
143        .await
144        .map_err(|e| BuildError::CleanDir(args.workdir.to_path_buf(), e))
145    } else {
146      Ok(())
147    }
148  };
149  let f2 = walk(FullConfig::new(args), args.rootdir.to_path_buf(), args);
150  let (clean_dir, file_configs) = tokio::join!(f1, f2);
152  if let Err(e) = clean_dir {
153    return Err(vec![e]);
154  }
155  let file_configs = file_configs?;
156  let faileds = Arc::new(Mutex::new(Vec::with_capacity(file_configs.len())));
157  let scheduler = Arc::new(Semaphore::new(args.permits as usize));
158  let handles: Vec<_> = file_configs
159    .into_iter()
160    .map(|(path, config)| {
161      let scheduler = scheduler.clone();
162      let faileds = faileds.clone();
163      tokio::spawn(async move {
164        let _permit = scheduler
165          .acquire_many(*config.permit)
166          .await
167          .expect("Semaphore closed");
168        let state = config.test(&path, args).await;
169        println!("test {} ... {}", path.display(), state);
170        match state {
171          State::Ok(Some(_)) => (1, 0, 0),
172          State::Failed(Some((failed, _))) => {
173            faileds.lock().await.push(failed);
174            (0, 0, 0)
175          }
176          State::Ok(None) | State::Failed(None) => unreachable!(),
177          State::Ignored => (0, 1, 0),
178          State::FilteredOut => (0, 0, 1),
179        }
180      })
181    })
182    .collect();
183  let mut count_ok = 0;
184  let mut count_ignored = 0;
185  let mut count_filtered = 0;
186  for handle in handles {
187    let (ok, ignored, filtered) = handle.await.unwrap();
188    count_ok += ok;
189    count_ignored += ignored;
190    count_filtered += filtered;
191  }
192  scheduler.close();
193  Ok(TestResult {
194    count_ok,
195    count_ignored,
196    count_filtered,
197    faileds: Arc::try_unwrap(faileds).unwrap().into_inner(),
198  })
199}
200
201#[async_recursion::async_recursion]
202async fn walk(
203  mut current_config: FullConfig,
204  current_path: PathBuf,
205  args: &'static Args,
206) -> Result<Vec<(PathBuf, FullConfig)>, Vec<BuildError>> {
207  let all_path = current_path.join("__all__.toml");
208  if all_path.exists() {
209    match current_config.update(&all_path, !args.nodebug) {
210      Ok(_config) => current_config = _config,
211      Err(e) => return Err(vec![e]),
212    }
213  }
214  let read_dir = match current_path.read_dir() {
215    Ok(read_dir) => read_dir,
216    Err(e) => return Err(vec![BuildError::ReadDir(current_path, e)]),
217  };
218  let (sub_dir_futures, files): (Vec<_>, Vec<_>) =
219    read_dir.into_iter().partition_map(|entry| {
220      let path = entry.unwrap().path();
221      if path.is_dir() {
222        if path.file_name().unwrap() == GOLDEN_DIR {
223          Either::Left(None)
224        } else {
225          let current_config = current_config.clone();
226          Either::Left(Some(tokio::spawn(walk(current_config, path, args))))
227        }
228      } else {
229        Either::Right(path)
230      }
231    });
232  let mut errs = Vec::new();
233  let mut file_configs = files
234    .into_iter()
235    .filter_map(|file| {
236      if current_config.match_extension(&file) {
237        match args.filtered(&file) {
238          Ok(filtered) => {
239            if filtered {
240              Some((file, FullConfig::new_filtered()))
241            } else {
242              let config_file = file.with_extension("toml");
243              let current_config = current_config.clone();
244              if config_file.is_file() {
245                match current_config.update(&config_file, !args.nodebug) {
246                  Ok(config) => Some((file, config)),
247                  Err(e) => {
248                    errs.push(e);
249                    None
250                  }
251                }
252              } else {
253                Some((file, current_config))
254              }
255              .and_then(|(file, config)| match config.eval(&file, args) {
256                Ok(config) => Some((file, config)),
257                Err(e) => {
258                  errs.push(e);
259                  None
260                }
261              })
262            }
263          }
264          Err(e) => {
265            errs.push(e);
266            None
267          }
268        }
269      } else {
270        None
271      }
272    })
273    .collect::<Vec<_>>();
274  for f in sub_dir_futures.into_iter().flatten() {
275    match f.await.expect("join handle") {
276      Ok(res) => file_configs.extend(res),
277      Err(e) => errs.extend(e),
278    }
279  }
280  if errs.is_empty() { Ok(file_configs) } else { Err(errs) }
281}