cargo_regression/
regression.rs

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use core::fmt;
use std::{
  io,
  path::PathBuf,
  process::{ExitCode, Termination},
  sync::Arc,
  time::Instant,
};

use itertools::{Either, Itertools};
use tokio::{fs::remove_dir_all, sync::Semaphore};

use crate::{
  args::match_extension,
  assert::{AssertError, DisplayErrs},
  config::FullConfig,
  Args,
};

#[derive(Debug, thiserror::Error)]
pub enum BuildError {
  #[error("file \"{0}\": {1}")]
  Toml(PathBuf, toml::de::Error),
  #[error("task \"{0}\": its permit = {1}, exceed total permits = {2}")]
  PermitEcxceed(PathBuf, u32, u32),
  #[error("task \"{0}\": need to specify 'exe_path'")]
  NoExePath(PathBuf),
  #[error("file \"{0}\": {1}")]
  UnableToRead(PathBuf, io::Error),
  #[error("read dir \"{0}\": {1}")]
  ReadDir(PathBuf, io::Error),
  #[error("clean dir \"{0}\": {1}")]
  CleanDir(PathBuf, io::Error),
  #[error("input extension can not contains 'toml'")]
  InputExtToml,
  #[error("This is regolden mode, all test case will pass!")]
  Regolden,
}

pub(crate) enum FailedState {
  ReportSaved(PathBuf),
  NoReport(Vec<AssertError>),
}
pub(crate) enum State {
  Ok,
  Failed(Option<FailedState>),
  Ignored,
  FilteredOut,
}

impl fmt::Display for FailedState {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      Self::ReportSaved(report) => {
        write!(f, "report: {}", report.display())
      }
      Self::NoReport(errs) => {
        write!(f, "errs:\n{}", DisplayErrs(errs))
      }
    }
  }
}
impl fmt::Display for State {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      Self::Ok => write!(f, "\x1B[32mok\x1B[0m"),
      Self::Failed(None) => write!(f, "\x1B[31mFAILED\x1B[0m"),
      Self::Failed(Some(fail)) => {
        write!(f, "\x1B[31mFAILED\x1B[0m\n     {fail}")
      }
      Self::Ignored => write!(f, "\x1B[33mignored\x1B[0m"),
      Self::FilteredOut => write!(f, "\x1B[2mfiltered out\x1B[0m"),
    }
  }
}

pub(crate) struct TestResult {
  count_ok: usize,
  count_ignored: usize,
  count_filtered: usize,
  faileds: Vec<FailedState>,
}

pub struct TestExitCode(Result<TestResult, Vec<BuildError>>, Instant);
impl From<BuildError> for TestExitCode {
  fn from(e: BuildError) -> Self {
    Self(Err(vec![e]), Instant::now())
  }
}

impl Termination for TestExitCode {
  fn report(self) -> ExitCode {
    let time = self.1.elapsed().as_secs_f32();
    match self.0 {
      Ok(TestResult { count_ok, count_ignored, count_filtered, faileds }) => {
        if faileds.is_empty() {
          println!("test result: {}. {count_ok} passed; 0 failed; {count_ignored} ignored; {count_filtered} filtered out; finished in {time:.2}s", State::Ok);
          ExitCode::SUCCESS
        } else {
          println!("\nfailures:");
          for failed in &faileds {
            println!("     {failed}");
          }
          println!("\ntest result: {}. {count_ok} passed; {} failed; {count_ignored} ignored; {count_filtered} filtered out; finished in {time:.2}s", State::Failed(None), faileds.len());
          ExitCode::FAILURE
        }
      }
      Err(build_errs) => {
        println!("Fail to build test:");
        for err in &build_errs {
          println!("{err}");
        }
        ExitCode::FAILURE
      }
    }
  }
}

pub async fn test(args: Args) -> TestExitCode {
  let now = Instant::now();
  TestExitCode(_test(args).await, now)
}
async fn _test(args: Args) -> Result<TestResult, Vec<BuildError>> {
  // check
  if args.extension.iter().any(|&s| s == "toml") {
    return Err(vec![BuildError::InputExtToml]);
  }
  let f1 = async move {
    let work_dir = PathBuf::from(args.work_dir);
    if work_dir.exists() {
      remove_dir_all(&work_dir)
        .await
        .map_err(|e| BuildError::CleanDir(work_dir, e))
    } else {
      Ok(())
    }
  };
  let f2 = async move {
    walk(args.extension, FullConfig::default(), PathBuf::from(args.cases_dir), args).await
  };
  // walkthrough all config
  let (clean_dir, file_configs) = tokio::join!(f1, f2);
  if let Err(e) = clean_dir {
    return Err(vec![e]);
  }
  let scheduler = Arc::new(Semaphore::new(args.permits as usize));
  let handles: Vec<_> = file_configs?
    .into_iter()
    .map(|(path, config)| {
      let scheduler = scheduler.clone();
      tokio::spawn(async move {
        let _permit = scheduler
          .acquire_many(*config.permit)
          .await
          .expect("Semaphore closed");
        let state = config.test(&path, args).await;
        println!("test {} ... {}", path.display(), state);
        state
      })
    })
    .collect();

  let mut count_ok = 0;
  let mut count_ignored = 0;
  let mut count_filtered = 0;
  let mut faileds = Vec::with_capacity(handles.len());
  // TODO: iter
  for handle in handles {
    match handle.await.unwrap() {
      State::Ok => count_ok += 1,
      State::Failed(Some(failed)) => faileds.push(failed),
      State::Failed(None) => unreachable!(),
      State::Ignored => count_ignored += 1,
      State::FilteredOut => count_filtered += 1,
    }
  }
  scheduler.close();
  if args.regolden {
    return Err(vec![BuildError::Regolden]);
  }
  Ok(TestResult { count_ok, count_ignored, count_filtered, faileds })
}

#[async_recursion::async_recursion]
async fn walk(
  extension: &'static [&'static str],
  mut current_config: FullConfig,
  current_path: PathBuf,
  args: Args,
) -> Result<Vec<(PathBuf, FullConfig)>, Vec<BuildError>> {
  let all_path = current_path.join("__all__.toml");
  if all_path.exists() {
    match current_config.update(&all_path, args.debug) {
      Ok(_config) => current_config = _config,
      Err(e) => return Err(vec![e]),
    }
  }
  let read_dir = match current_path.read_dir() {
    Ok(read_dir) => read_dir,
    Err(e) => return Err(vec![BuildError::ReadDir(current_path, e)]),
  };
  let (sub_dir_futures, files): (Vec<_>, Vec<_>) =
    read_dir.into_iter().partition_map(|entry| {
      let path = entry.unwrap().path();
      if path.is_dir() {
        let current_config = current_config.clone();
        Either::Left(tokio::spawn(async move {
          walk(extension, current_config, path, args).await
        }))
      } else {
        Either::Right(path)
      }
    });
  let mut errs = Vec::new();
  let mut file_configs = files
    .into_iter()
    .filter_map(|file| {
      if match_extension(&file, extension) {
        match args.filtered(&file) {
          Ok(filtered) => {
            if filtered {
              Some((file, FullConfig::new_filtered()))
            } else {
              let config_file = file.with_extension("toml");
              let current_config = current_config.clone();
              if config_file.is_file() {
                match current_config.update(&config_file, args.debug) {
                  Ok(config) => Some((file, config)),
                  Err(e) => {
                    errs.push(e);
                    None
                  }
                }
              } else {
                Some((file, current_config))
              }
              .and_then(|(file, config)| match config.eval(&file, args) {
                Ok(config) => Some((file, config)),
                Err(e) => {
                  errs.push(e);
                  None
                }
              })
            }
          }
          Err(e) => {
            errs.push(e);
            None
          }
        }
      } else {
        None
      }
    })
    .collect::<Vec<_>>();
  for f in sub_dir_futures.into_iter() {
    match f.await.expect("join handle") {
      Ok(res) => file_configs.extend(res),
      Err(e) => errs.extend(e),
    }
  }
  if errs.is_empty() {
    Ok(file_configs)
  } else {
    Err(errs)
  }
}