2024-03-31 17:55:33 +03:00
|
|
|
use anyhow::{bail, Result};
|
2020-02-20 22:11:53 +03:00
|
|
|
use console::style;
|
2022-02-06 00:54:11 +03:00
|
|
|
use indicatif::{ProgressBar, ProgressStyle};
|
2024-03-31 17:55:33 +03:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
io::{stdout, Write},
|
|
|
|
process::Output,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::exercise::{Exercise, Mode, State};
|
2019-01-09 22:33:43 +03:00
|
|
|
|
2020-06-04 17:31:17 +03:00
|
|
|
// Verify that the provided container of Exercise objects
|
|
|
|
// can be compiled and run without any failures.
|
|
|
|
// Any such failures will be reported to the end user.
|
|
|
|
// If the Exercise being verified is a test, the verbose boolean
|
|
|
|
// determines whether or not the test harness outputs are displayed.
|
|
|
|
pub fn verify<'a>(
|
2024-04-01 19:21:56 +03:00
|
|
|
pending_exercises: impl IntoIterator<Item = &'a Exercise>,
|
2022-02-06 00:54:11 +03:00
|
|
|
progress: (usize, usize),
|
2020-08-10 17:42:54 +03:00
|
|
|
verbose: bool,
|
2023-05-01 03:49:19 +03:00
|
|
|
success_hints: bool,
|
2020-06-04 17:31:17 +03:00
|
|
|
) -> Result<(), &'a Exercise> {
|
2023-02-27 23:17:45 +03:00
|
|
|
let (num_done, total) = progress;
|
2022-02-06 00:54:11 +03:00
|
|
|
let bar = ProgressBar::new(total as u64);
|
2023-02-27 23:17:45 +03:00
|
|
|
let mut percentage = num_done as f32 / total as f32 * 100.0;
|
2023-08-27 00:35:07 +03:00
|
|
|
bar.set_style(
|
|
|
|
ProgressStyle::default_bar()
|
|
|
|
.template("Progress: [{bar:60.green/red}] {pos}/{len} {msg}")
|
|
|
|
.expect("Progressbar template should be valid!")
|
|
|
|
.progress_chars("#>-"),
|
2022-02-06 00:54:11 +03:00
|
|
|
);
|
|
|
|
bar.set_position(num_done as u64);
|
2024-03-26 19:49:05 +03:00
|
|
|
bar.set_message(format!("({percentage:.1} %)"));
|
2023-01-03 17:24:01 +03:00
|
|
|
|
2024-04-01 19:21:56 +03:00
|
|
|
for exercise in pending_exercises {
|
2019-11-18 20:11:22 +03:00
|
|
|
let compile_result = match exercise.mode {
|
2023-05-01 03:49:19 +03:00
|
|
|
Mode::Test => compile_and_test(exercise, RunMode::Interactive, verbose, success_hints),
|
|
|
|
Mode::Compile => compile_and_run_interactively(exercise, success_hints),
|
|
|
|
Mode::Clippy => compile_only(exercise, success_hints),
|
2019-11-11 15:38:24 +03:00
|
|
|
};
|
2019-11-18 20:11:22 +03:00
|
|
|
if !compile_result.unwrap_or(false) {
|
|
|
|
return Err(exercise);
|
2019-03-06 23:47:00 +03:00
|
|
|
}
|
2023-02-27 23:17:45 +03:00
|
|
|
percentage += 100.0 / total as f32;
|
2022-02-06 00:54:11 +03:00
|
|
|
bar.inc(1);
|
2024-03-26 19:49:05 +03:00
|
|
|
bar.set_message(format!("({percentage:.1} %)"));
|
2019-03-06 23:47:00 +03:00
|
|
|
}
|
2024-04-01 19:21:56 +03:00
|
|
|
|
|
|
|
bar.finish();
|
|
|
|
println!("You completed all exercises!");
|
|
|
|
|
2019-01-09 22:33:43 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-03-26 19:49:48 +03:00
|
|
|
#[derive(PartialEq, Eq)]
|
2020-02-20 22:11:53 +03:00
|
|
|
enum RunMode {
|
|
|
|
Interactive,
|
|
|
|
NonInteractive,
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:31:17 +03:00
|
|
|
// Compile and run the resulting test harness of the given Exercise
|
2024-03-31 17:55:33 +03:00
|
|
|
pub fn test(exercise: &Exercise, verbose: bool) -> Result<()> {
|
2023-05-01 03:49:19 +03:00
|
|
|
compile_and_test(exercise, RunMode::NonInteractive, verbose, false)?;
|
2019-11-12 13:35:40 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:31:17 +03:00
|
|
|
// Invoke the rust compiler without running the resulting binary
|
2024-03-31 17:55:33 +03:00
|
|
|
fn compile_only(exercise: &Exercise, success_hints: bool) -> Result<bool> {
|
2019-03-11 17:09:20 +03:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2022-10-12 23:30:52 +03:00
|
|
|
progress_bar.set_message(format!("Compiling {exercise}..."));
|
2023-08-27 00:35:07 +03:00
|
|
|
progress_bar.enable_steady_tick(Duration::from_millis(100));
|
2020-02-26 22:38:44 +03:00
|
|
|
|
2024-03-31 17:55:33 +03:00
|
|
|
let _ = exercise.run()?;
|
2019-03-11 17:09:20 +03:00
|
|
|
progress_bar.finish_and_clear();
|
2019-01-09 22:33:43 +03:00
|
|
|
|
2024-03-31 19:25:54 +03:00
|
|
|
prompt_for_completion(exercise, None, success_hints)
|
2019-11-12 13:35:40 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:31:17 +03:00
|
|
|
// Compile the given Exercise and run the resulting binary in an interactive mode
|
2024-03-31 17:55:33 +03:00
|
|
|
fn compile_and_run_interactively(exercise: &Exercise, success_hints: bool) -> Result<bool> {
|
2019-03-11 17:09:20 +03:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2024-03-31 17:55:33 +03:00
|
|
|
progress_bar.set_message(format!("Running {exercise}..."));
|
2023-08-27 00:35:07 +03:00
|
|
|
progress_bar.enable_steady_tick(Duration::from_millis(100));
|
2019-04-11 23:41:24 +03:00
|
|
|
|
2024-03-31 17:55:33 +03:00
|
|
|
let output = exercise.run()?;
|
2020-02-26 22:38:44 +03:00
|
|
|
progress_bar.finish_and_clear();
|
|
|
|
|
2024-03-31 17:55:33 +03:00
|
|
|
if !output.status.success() {
|
|
|
|
warn!("Ran {} with errors", exercise);
|
|
|
|
{
|
|
|
|
let mut stdout = stdout().lock();
|
|
|
|
stdout.write_all(&output.stdout)?;
|
|
|
|
stdout.write_all(&output.stderr)?;
|
|
|
|
stdout.flush()?;
|
2020-02-20 22:11:53 +03:00
|
|
|
}
|
2024-03-31 17:55:33 +03:00
|
|
|
bail!("TODO");
|
|
|
|
}
|
2019-04-11 23:41:24 +03:00
|
|
|
|
2024-03-31 19:25:54 +03:00
|
|
|
prompt_for_completion(exercise, Some(output), success_hints)
|
2020-02-26 22:38:44 +03:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:31:17 +03:00
|
|
|
// Compile the given Exercise as a test harness and display
|
|
|
|
// the output if verbose is set to true
|
2023-08-27 00:35:07 +03:00
|
|
|
fn compile_and_test(
|
|
|
|
exercise: &Exercise,
|
|
|
|
run_mode: RunMode,
|
|
|
|
verbose: bool,
|
|
|
|
success_hints: bool,
|
2024-03-31 17:55:33 +03:00
|
|
|
) -> Result<bool> {
|
2020-02-26 22:38:44 +03:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2022-10-12 23:30:52 +03:00
|
|
|
progress_bar.set_message(format!("Testing {exercise}..."));
|
2023-08-27 00:35:07 +03:00
|
|
|
progress_bar.enable_steady_tick(Duration::from_millis(100));
|
2020-02-26 22:38:44 +03:00
|
|
|
|
2024-03-31 17:55:33 +03:00
|
|
|
let output = exercise.run()?;
|
2020-02-20 22:11:53 +03:00
|
|
|
progress_bar.finish_and_clear();
|
2019-02-15 14:06:05 +03:00
|
|
|
|
2024-03-31 17:55:33 +03:00
|
|
|
if !output.status.success() {
|
|
|
|
warn!(
|
|
|
|
"Testing of {} failed! Please try again. Here's the output:",
|
|
|
|
exercise
|
|
|
|
);
|
|
|
|
{
|
|
|
|
let mut stdout = stdout().lock();
|
|
|
|
stdout.write_all(&output.stdout)?;
|
|
|
|
stdout.write_all(&output.stderr)?;
|
|
|
|
stdout.flush()?;
|
2019-02-15 14:06:05 +03:00
|
|
|
}
|
2024-03-31 17:55:33 +03:00
|
|
|
bail!("TODO");
|
2019-01-09 22:33:43 +03:00
|
|
|
}
|
2019-11-11 15:38:24 +03:00
|
|
|
|
2024-03-31 17:55:33 +03:00
|
|
|
if verbose {
|
|
|
|
stdout().write_all(&output.stdout)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if run_mode == RunMode::Interactive {
|
2024-03-31 19:25:54 +03:00
|
|
|
prompt_for_completion(exercise, None, success_hints)
|
2024-03-31 17:55:33 +03:00
|
|
|
} else {
|
|
|
|
Ok(true)
|
2020-02-26 22:38:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 00:35:07 +03:00
|
|
|
fn prompt_for_completion(
|
|
|
|
exercise: &Exercise,
|
2024-03-31 17:55:33 +03:00
|
|
|
prompt_output: Option<Output>,
|
2023-08-27 00:35:07 +03:00
|
|
|
success_hints: bool,
|
2024-03-31 19:25:54 +03:00
|
|
|
) -> Result<bool> {
|
|
|
|
let context = match exercise.state()? {
|
|
|
|
State::Done => return Ok(true),
|
2019-11-12 13:35:40 +03:00
|
|
|
State::Pending(context) => context,
|
|
|
|
};
|
2022-02-06 00:54:11 +03:00
|
|
|
match exercise.mode {
|
|
|
|
Mode::Compile => success!("Successfully ran {}!", exercise),
|
|
|
|
Mode::Test => success!("Successfully tested {}!", exercise),
|
|
|
|
Mode::Clippy => success!("Successfully compiled {}!", exercise),
|
|
|
|
}
|
|
|
|
|
2021-03-19 12:16:07 +03:00
|
|
|
let no_emoji = env::var("NO_EMOJI").is_ok();
|
|
|
|
|
2021-03-20 21:52:57 +03:00
|
|
|
let clippy_success_msg = if no_emoji {
|
|
|
|
"The code is compiling, and Clippy is happy!"
|
|
|
|
} else {
|
|
|
|
"The code is compiling, and 📎 Clippy 📎 is happy!"
|
2021-03-19 12:16:07 +03:00
|
|
|
};
|
|
|
|
|
2019-11-12 13:35:40 +03:00
|
|
|
let success_msg = match exercise.mode {
|
2019-11-11 15:38:24 +03:00
|
|
|
Mode::Compile => "The code is compiling!",
|
|
|
|
Mode::Test => "The code is compiling, and the tests pass!",
|
2021-03-19 12:16:07 +03:00
|
|
|
Mode::Clippy => clippy_success_msg,
|
2019-11-11 15:38:24 +03:00
|
|
|
};
|
2024-03-26 19:49:05 +03:00
|
|
|
|
2021-03-20 21:52:57 +03:00
|
|
|
if no_emoji {
|
2024-03-26 19:49:05 +03:00
|
|
|
println!("\n~*~ {success_msg} ~*~\n");
|
2021-03-20 21:52:57 +03:00
|
|
|
} else {
|
2024-03-26 19:49:05 +03:00
|
|
|
println!("\n🎉 🎉 {success_msg} 🎉 🎉\n");
|
2021-03-20 21:52:57 +03:00
|
|
|
}
|
2020-02-26 22:38:44 +03:00
|
|
|
|
|
|
|
if let Some(output) = prompt_output {
|
2024-03-31 17:55:33 +03:00
|
|
|
let separator = separator();
|
|
|
|
println!("Output:\n{separator}");
|
|
|
|
stdout().write_all(&output.stdout).unwrap();
|
|
|
|
println!("\n{separator}\n");
|
2020-02-26 22:38:44 +03:00
|
|
|
}
|
2023-05-01 03:49:19 +03:00
|
|
|
if success_hints {
|
2024-03-26 19:49:05 +03:00
|
|
|
println!(
|
|
|
|
"Hints:\n{separator}\n{}\n{separator}\n",
|
|
|
|
exercise.hint,
|
|
|
|
separator = separator(),
|
|
|
|
);
|
2023-05-01 03:49:19 +03:00
|
|
|
}
|
2020-02-26 22:38:44 +03:00
|
|
|
|
2019-11-11 15:38:24 +03:00
|
|
|
println!("You can keep working on this exercise,");
|
|
|
|
println!(
|
|
|
|
"or jump into the next one by removing the {} comment:",
|
|
|
|
style("`I AM NOT DONE`").bold()
|
|
|
|
);
|
2020-06-04 00:18:48 +03:00
|
|
|
println!();
|
2019-11-11 15:38:24 +03:00
|
|
|
for context_line in context {
|
|
|
|
let formatted_line = if context_line.important {
|
|
|
|
format!("{}", style(context_line.line).bold())
|
|
|
|
} else {
|
2024-03-24 00:08:25 +03:00
|
|
|
context_line.line
|
2019-11-11 15:38:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{:>2} {} {}",
|
|
|
|
style(context_line.number).blue().bold(),
|
|
|
|
style("|").blue(),
|
2024-03-26 19:49:05 +03:00
|
|
|
formatted_line,
|
2019-11-11 15:38:24 +03:00
|
|
|
);
|
|
|
|
}
|
2019-11-12 13:35:40 +03:00
|
|
|
|
2024-03-31 19:25:54 +03:00
|
|
|
Ok(false)
|
2019-11-11 15:38:24 +03:00
|
|
|
}
|
2020-02-26 22:38:44 +03:00
|
|
|
|
|
|
|
fn separator() -> console::StyledObject<&'static str> {
|
|
|
|
style("====================").bold()
|
|
|
|
}
|