2024-03-31 10:55:33 -04:00
|
|
|
use anyhow::{bail, Result};
|
2020-02-20 14:11:53 -05:00
|
|
|
use console::style;
|
2022-02-05 16:54:11 -05:00
|
|
|
use indicatif::{ProgressBar, ProgressStyle};
|
2024-03-31 10:55:33 -04:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
io::{stdout, Write},
|
|
|
|
process::Output,
|
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::exercise::{Exercise, Mode, State};
|
2019-01-09 14:33:43 -05:00
|
|
|
|
2020-06-04 10:31:17 -04: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 12:21:56 -04:00
|
|
|
pending_exercises: impl IntoIterator<Item = &'a Exercise>,
|
2022-02-05 16:54:11 -05:00
|
|
|
progress: (usize, usize),
|
2020-08-10 10:42:54 -04:00
|
|
|
verbose: bool,
|
2023-04-30 20:49:19 -04:00
|
|
|
success_hints: bool,
|
2020-06-04 10:31:17 -04:00
|
|
|
) -> Result<(), &'a Exercise> {
|
2023-02-27 15:17:45 -05:00
|
|
|
let (num_done, total) = progress;
|
2022-02-05 16:54:11 -05:00
|
|
|
let bar = ProgressBar::new(total as u64);
|
2023-02-27 15:17:45 -05:00
|
|
|
let mut percentage = num_done as f32 / total as f32 * 100.0;
|
2023-08-26 17:35:07 -04: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-05 16:54:11 -05:00
|
|
|
);
|
|
|
|
bar.set_position(num_done as u64);
|
2024-03-26 12:49:05 -04:00
|
|
|
bar.set_message(format!("({percentage:.1} %)"));
|
2023-01-03 09:24:01 -05:00
|
|
|
|
2024-04-01 12:21:56 -04:00
|
|
|
for exercise in pending_exercises {
|
2019-11-18 12:11:22 -05:00
|
|
|
let compile_result = match exercise.mode {
|
2023-04-30 20:49:19 -04: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 07:38:24 -05:00
|
|
|
};
|
2019-11-18 12:11:22 -05:00
|
|
|
if !compile_result.unwrap_or(false) {
|
|
|
|
return Err(exercise);
|
2019-03-06 15:47:00 -05:00
|
|
|
}
|
2023-02-27 15:17:45 -05:00
|
|
|
percentage += 100.0 / total as f32;
|
2022-02-05 16:54:11 -05:00
|
|
|
bar.inc(1);
|
2024-03-26 12:49:05 -04:00
|
|
|
bar.set_message(format!("({percentage:.1} %)"));
|
2019-03-06 15:47:00 -05:00
|
|
|
}
|
2024-04-01 12:21:56 -04:00
|
|
|
|
|
|
|
bar.finish();
|
|
|
|
println!("You completed all exercises!");
|
|
|
|
|
2019-01-09 14:33:43 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-03-26 12:49:48 -04:00
|
|
|
#[derive(PartialEq, Eq)]
|
2020-02-20 14:11:53 -05:00
|
|
|
enum RunMode {
|
|
|
|
Interactive,
|
|
|
|
NonInteractive,
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:31:17 -04:00
|
|
|
// Compile and run the resulting test harness of the given Exercise
|
2024-03-31 10:55:33 -04:00
|
|
|
pub fn test(exercise: &Exercise, verbose: bool) -> Result<()> {
|
2023-04-30 20:49:19 -04:00
|
|
|
compile_and_test(exercise, RunMode::NonInteractive, verbose, false)?;
|
2019-11-12 05:35:40 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:31:17 -04:00
|
|
|
// Invoke the rust compiler without running the resulting binary
|
2024-03-31 10:55:33 -04:00
|
|
|
fn compile_only(exercise: &Exercise, success_hints: bool) -> Result<bool> {
|
2019-03-11 10:09:20 -04:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2022-10-12 16:30:52 -04:00
|
|
|
progress_bar.set_message(format!("Compiling {exercise}..."));
|
2023-08-26 17:35:07 -04:00
|
|
|
progress_bar.enable_steady_tick(Duration::from_millis(100));
|
2020-02-26 14:38:44 -05:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
let _ = exercise.run()?;
|
2019-03-11 10:09:20 -04:00
|
|
|
progress_bar.finish_and_clear();
|
2019-01-09 14:33:43 -05:00
|
|
|
|
2024-03-31 12:25:54 -04:00
|
|
|
prompt_for_completion(exercise, None, success_hints)
|
2019-11-12 05:35:40 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:31:17 -04:00
|
|
|
// Compile the given Exercise and run the resulting binary in an interactive mode
|
2024-03-31 10:55:33 -04:00
|
|
|
fn compile_and_run_interactively(exercise: &Exercise, success_hints: bool) -> Result<bool> {
|
2019-03-11 10:09:20 -04:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2024-03-31 10:55:33 -04:00
|
|
|
progress_bar.set_message(format!("Running {exercise}..."));
|
2023-08-26 17:35:07 -04:00
|
|
|
progress_bar.enable_steady_tick(Duration::from_millis(100));
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
let output = exercise.run()?;
|
2020-02-26 14:38:44 -05:00
|
|
|
progress_bar.finish_and_clear();
|
|
|
|
|
2024-03-31 10:55:33 -04: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 14:11:53 -05:00
|
|
|
}
|
2024-03-31 10:55:33 -04:00
|
|
|
bail!("TODO");
|
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-03-31 12:25:54 -04:00
|
|
|
prompt_for_completion(exercise, Some(output), success_hints)
|
2020-02-26 14:38:44 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:31:17 -04:00
|
|
|
// Compile the given Exercise as a test harness and display
|
|
|
|
// the output if verbose is set to true
|
2023-08-26 17:35:07 -04:00
|
|
|
fn compile_and_test(
|
|
|
|
exercise: &Exercise,
|
|
|
|
run_mode: RunMode,
|
|
|
|
verbose: bool,
|
|
|
|
success_hints: bool,
|
2024-03-31 10:55:33 -04:00
|
|
|
) -> Result<bool> {
|
2020-02-26 14:38:44 -05:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2022-10-12 16:30:52 -04:00
|
|
|
progress_bar.set_message(format!("Testing {exercise}..."));
|
2023-08-26 17:35:07 -04:00
|
|
|
progress_bar.enable_steady_tick(Duration::from_millis(100));
|
2020-02-26 14:38:44 -05:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
let output = exercise.run()?;
|
2020-02-20 14:11:53 -05:00
|
|
|
progress_bar.finish_and_clear();
|
2019-02-15 06:06:05 -05:00
|
|
|
|
2024-03-31 10:55:33 -04: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 06:06:05 -05:00
|
|
|
}
|
2024-03-31 10:55:33 -04:00
|
|
|
bail!("TODO");
|
2019-01-09 14:33:43 -05:00
|
|
|
}
|
2019-11-11 07:38:24 -05:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
if verbose {
|
|
|
|
stdout().write_all(&output.stdout)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if run_mode == RunMode::Interactive {
|
2024-03-31 12:25:54 -04:00
|
|
|
prompt_for_completion(exercise, None, success_hints)
|
2024-03-31 10:55:33 -04:00
|
|
|
} else {
|
|
|
|
Ok(true)
|
2020-02-26 14:38:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 17:35:07 -04:00
|
|
|
fn prompt_for_completion(
|
|
|
|
exercise: &Exercise,
|
2024-03-31 10:55:33 -04:00
|
|
|
prompt_output: Option<Output>,
|
2023-08-26 17:35:07 -04:00
|
|
|
success_hints: bool,
|
2024-03-31 12:25:54 -04:00
|
|
|
) -> Result<bool> {
|
|
|
|
let context = match exercise.state()? {
|
|
|
|
State::Done => return Ok(true),
|
2019-11-12 05:35:40 -05:00
|
|
|
State::Pending(context) => context,
|
|
|
|
};
|
2022-02-05 16:54:11 -05: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 05:16:07 -04:00
|
|
|
let no_emoji = env::var("NO_EMOJI").is_ok();
|
|
|
|
|
2021-03-20 14:52:57 -04: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 05:16:07 -04:00
|
|
|
};
|
|
|
|
|
2019-11-12 05:35:40 -05:00
|
|
|
let success_msg = match exercise.mode {
|
2019-11-11 07:38:24 -05:00
|
|
|
Mode::Compile => "The code is compiling!",
|
|
|
|
Mode::Test => "The code is compiling, and the tests pass!",
|
2021-03-19 05:16:07 -04:00
|
|
|
Mode::Clippy => clippy_success_msg,
|
2019-11-11 07:38:24 -05:00
|
|
|
};
|
2024-03-26 12:49:05 -04:00
|
|
|
|
2021-03-20 14:52:57 -04:00
|
|
|
if no_emoji {
|
2024-03-26 12:49:05 -04:00
|
|
|
println!("\n~*~ {success_msg} ~*~\n");
|
2021-03-20 14:52:57 -04:00
|
|
|
} else {
|
2024-03-26 12:49:05 -04:00
|
|
|
println!("\n🎉 🎉 {success_msg} 🎉 🎉\n");
|
2021-03-20 14:52:57 -04:00
|
|
|
}
|
2020-02-26 14:38:44 -05:00
|
|
|
|
|
|
|
if let Some(output) = prompt_output {
|
2024-03-31 10:55:33 -04:00
|
|
|
let separator = separator();
|
|
|
|
println!("Output:\n{separator}");
|
|
|
|
stdout().write_all(&output.stdout).unwrap();
|
|
|
|
println!("\n{separator}\n");
|
2020-02-26 14:38:44 -05:00
|
|
|
}
|
2023-04-30 20:49:19 -04:00
|
|
|
if success_hints {
|
2024-03-26 12:49:05 -04:00
|
|
|
println!(
|
|
|
|
"Hints:\n{separator}\n{}\n{separator}\n",
|
|
|
|
exercise.hint,
|
|
|
|
separator = separator(),
|
|
|
|
);
|
2023-04-30 20:49:19 -04:00
|
|
|
}
|
2020-02-26 14:38:44 -05:00
|
|
|
|
2019-11-11 07:38:24 -05: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-03 17:18:48 -04:00
|
|
|
println!();
|
2019-11-11 07:38:24 -05:00
|
|
|
for context_line in context {
|
|
|
|
let formatted_line = if context_line.important {
|
|
|
|
format!("{}", style(context_line.line).bold())
|
|
|
|
} else {
|
2024-03-23 17:08:25 -04:00
|
|
|
context_line.line
|
2019-11-11 07:38:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{:>2} {} {}",
|
|
|
|
style(context_line.number).blue().bold(),
|
|
|
|
style("|").blue(),
|
2024-03-26 12:49:05 -04:00
|
|
|
formatted_line,
|
2019-11-11 07:38:24 -05:00
|
|
|
);
|
|
|
|
}
|
2019-11-12 05:35:40 -05:00
|
|
|
|
2024-03-31 12:25:54 -04:00
|
|
|
Ok(false)
|
2019-11-11 07:38:24 -05:00
|
|
|
}
|
2020-02-26 14:38:44 -05:00
|
|
|
|
|
|
|
fn separator() -> console::StyledObject<&'static str> {
|
|
|
|
style("====================").bold()
|
|
|
|
}
|