use crate::compiler::Compiler;
use crate::exercise::{Exercise, VerifyResult};
use anyhow::Result;
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub enum RunStatus {
Passed(VerifyResult),
Failed(VerifyResult),
Unsupported,
Missing,
}
impl RunStatus {
pub fn passed(&self) -> bool {
matches!(self, RunStatus::Passed(_))
}
}
pub fn evaluate(exercise: &Exercise, compiler: &Compiler, build_dir: &Path) -> Result<RunStatus> {
if !exercise.supported {
return Ok(RunStatus::Unsupported);
}
if !exercise.exists() {
return Ok(RunStatus::Missing);
}
let result = exercise.verify(compiler, build_dir)?;
Ok(if result.success {
RunStatus::Passed(result)
} else {
RunStatus::Failed(result)
})
}
pub fn reveal_if_passed(exercise: &Exercise, status: &RunStatus) -> Option<PathBuf> {
if !status.passed() {
return None;
}
exercise.reveal_solution().ok()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compiler::CompilerKind;
use crate::info_file::ExerciseInfo;
use std::path::PathBuf;
fn make_exercise(name: &str, dir: &Path) -> Exercise {
Exercise {
info: ExerciseInfo {
name: name.to_string(),
dir: "test".to_string(),
test: false,
sanitizers: false,
flags: Vec::new(),
compilers: Some(vec!["gcc".to_string()]),
hint: None,
hints: None,
},
path: dir.join(format!("{name}.c")),
solution_path: PathBuf::from("/nonexistent"),
reveal_path: dir.join(format!("{name}_revealed.c")),
supported: true,
}
}
fn compiler_for(dir: &Path) -> Option<Compiler> {
Compiler::new(CompilerKind::Gcc, dir).ok()
}
#[test]
fn unsupported_wins_over_missing_file() {
let tmp = tempfile::TempDir::new().unwrap();
let Some(compiler) = compiler_for(tmp.path()) else {
eprintln!("skipping: no gcc available");
return;
};
let mut ex = make_exercise("ghost", tmp.path());
ex.supported = false;
assert!(!ex.exists(), "the test relies on the file being absent");
let status = evaluate(&ex, &compiler, tmp.path()).unwrap();
assert!(matches!(status, RunStatus::Unsupported));
assert!(!status.passed());
}
#[test]
fn missing_working_copy_is_not_compiled() {
let tmp = tempfile::TempDir::new().unwrap();
let Some(compiler) = compiler_for(tmp.path()) else {
eprintln!("skipping: no gcc available");
return;
};
let ex = make_exercise("ghost", tmp.path());
let status = evaluate(&ex, &compiler, tmp.path()).unwrap();
assert!(matches!(status, RunStatus::Missing));
}
}