summaryrefslogtreecommitdiff
path: root/src/run.rs
blob: 2fd6f407194c37b0ab5544548e731968c95d5dd3 (plain)
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
use anyhow::{bail, Result};
use crossterm::style::Stylize;
use std::io::{stdout, Write};

use crate::exercise::Exercise;

// Invoke the rust compiler on the path of the given exercise,
// and run the ensuing binary.
// The verbose argument helps determine whether or not to show
// the output from the test harnesses (if the mode of the exercise is test)
pub fn run(exercise: &Exercise) -> Result<()> {
    let output = exercise.run()?;

    {
        let mut stdout = stdout().lock();
        stdout.write_all(&output.stdout)?;
        stdout.write_all(&output.stderr)?;
        stdout.flush()?;
    }

    if !output.status.success() {
        bail!("Ran {exercise} with errors");
    }

    println!("{}", "✓ Successfully ran {exercise}".green());

    Ok(())
}