rustlings/src/verify.rs

124 lines
4.8 KiB
Rust
Raw Normal View History

2019-01-09 14:33:58 -05:00
use crate::util::clean;
2019-01-09 14:33:43 -05:00
use console::{style, Emoji};
use indicatif::ProgressBar;
use std::process::Command;
pub fn verify() -> Result<(), ()> {
compile_only("exercises/variables/variables1.rs")?;
compile_only("exercises/variables/variables2.rs")?;
compile_only("exercises/variables/variables3.rs")?;
compile_only("exercises/variables/variables4.rs")?;
2019-01-23 14:48:01 -05:00
test("exercises/if/if1.rs")?;
2019-01-09 14:33:43 -05:00
compile_only("exercises/functions/functions1.rs")?;
compile_only("exercises/functions/functions2.rs")?;
compile_only("exercises/functions/functions3.rs")?;
compile_only("exercises/functions/functions4.rs")?;
compile_only("exercises/functions/functions5.rs")?;
test("exercises/test1.rs")?;
2019-01-09 14:33:43 -05:00
compile_only("exercises/primitive_types/primitive_types1.rs")?;
compile_only("exercises/primitive_types/primitive_types2.rs")?;
compile_only("exercises/primitive_types/primitive_types3.rs")?;
compile_only("exercises/primitive_types/primitive_types4.rs")?;
compile_only("exercises/primitive_types/primitive_types5.rs")?;
compile_only("exercises/primitive_types/primitive_types6.rs")?;
test("exercises/tests/tests1.rs")?;
test("exercises/tests/tests2.rs")?;
test("exercises/tests/tests3.rs")?;
2019-01-23 14:48:01 -05:00
test("exercises/test2.rs")?;
2019-01-09 14:33:43 -05:00
compile_only("exercises/strings/strings1.rs")?;
compile_only("exercises/strings/strings2.rs")?;
2019-01-23 14:48:01 -05:00
compile_only("exercises/test3.rs")?;
2019-01-09 14:33:43 -05:00
compile_only("exercises/modules/modules1.rs")?;
compile_only("exercises/modules/modules2.rs")?;
compile_only("exercises/macros/macros1.rs")?;
compile_only("exercises/macros/macros2.rs")?;
compile_only("exercises/macros/macros3.rs")?;
compile_only("exercises/macros/macros4.rs")?;
2019-01-23 14:48:01 -05:00
compile_only("exercises/test4.rs")?;
compile_only("exercises/move_semantics/move_semantics1.rs")?;
compile_only("exercises/move_semantics/move_semantics2.rs")?;
compile_only("exercises/move_semantics/move_semantics3.rs")?;
compile_only("exercises/move_semantics/move_semantics4.rs")?;
2019-01-09 14:33:43 -05:00
test("exercises/error_handling/errors1.rs")?;
test("exercises/error_handling/errors2.rs")?;
test("exercises/error_handling/errors3.rs")?;
test("exercises/error_handling/errorsn.rs")?;
compile_only("exercises/error_handling/option1.rs")?;
test("exercises/error_handling/result1.rs")?;
Ok(())
}
fn compile_only(filename: &str) -> Result<(), ()> {
let bar = ProgressBar::new_spinner();
bar.set_message(format!("Compiling {}...", filename).as_str());
bar.enable_steady_tick(100);
let compilecmd = Command::new("rustc")
.args(&[filename, "-o", "temp", "--color", "always"])
.output()
.expect("fail");
bar.finish_and_clear();
if compilecmd.status.success() {
let formatstr = format!(
"{} Successfully compiled {}!",
Emoji("", ""),
filename
);
println!("{}", style(formatstr).green());
clean();
Ok(())
} else {
let formatstr = format!(
"{} Compilation of {} failed! Compiler error message:\n",
Emoji("⚠️ ", "!"),
filename
);
println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
clean();
Err(())
}
}
2019-01-09 16:04:08 -05:00
pub fn test(filename: &str) -> Result<(), ()> {
2019-01-09 14:33:43 -05:00
let bar = ProgressBar::new_spinner();
bar.set_message(format!("Testing {}...", filename).as_str());
bar.enable_steady_tick(100);
let testcmd = Command::new("rustc")
.args(&["--test", filename, "-o", "temp", "--color", "always"])
2019-01-09 14:33:43 -05:00
.output()
.expect("fail");
if testcmd.status.success() {
bar.set_message(format!("Running {}...", filename).as_str());
let runcmd = Command::new("./temp").output().expect("fail");
bar.finish_and_clear();
if runcmd.status.success() {
let formatstr = format!("{} Successfully tested {}!", Emoji("", ""), filename);
println!("{}", style(formatstr).green());
clean();
Ok(())
} else {
let formatstr = format!(
"{} Testing of {} failed! Please try again. Here's the output:",
Emoji("⚠️ ", "!"),
filename
);
println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
clean();
Err(())
}
2019-01-09 14:33:43 -05:00
} else {
bar.finish_and_clear();
2019-01-09 14:33:43 -05:00
let formatstr = format!(
"{} Compiling of {} failed! Please try again. Here's the output:",
2019-01-09 14:33:43 -05:00
Emoji("⚠️ ", "!"),
filename
);
println!("{}", style(formatstr).red());
println!("{}", String::from_utf8_lossy(&testcmd.stderr));
2019-01-09 14:33:43 -05:00
clean();
Err(())
}
}