summaryrefslogtreecommitdiff
path: root/src/util.rs
blob: 6bac972f56eed6b097299f3263c64a3dfc46e819 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::fs::remove_file;
use std::process::{self, Command, Output};

const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];

fn temp_file() -> String {
    format!("./temp_{}", process::id())
}

pub fn compile_test_cmd(filename: &str) -> Output {
    Command::new("rustc")
        .args(&["--test", filename, "-o", &temp_file()])
        .args(RUSTC_COLOR_ARGS)
        .output()
        .expect("failed to compile exercise")
}

pub fn compile_cmd(filename: &str) -> Output {
    Command::new("rustc")
        .args(&[filename, "-o", &temp_file()])
        .args(RUSTC_COLOR_ARGS)
        .output()
        .expect("failed to compile exercise")
}

pub fn run_cmd() -> Output {
    Command::new(&temp_file())
        .output()
        .expect("failed to run exercise")
}

pub fn clean() {
    let _ignored = remove_file(&temp_file());
}

#[test]
fn test_clean() {
    std::fs::File::create(&temp_file()).unwrap();
    clean();
    assert!(!std::path::Path::new(&temp_file()).exists());
}