Skip to content

Commit 02ceb0b

Browse files
author
liv
committed
fix test command execution
seems like i forgot that you actually need to run executables created with `rustc --test`! this also reworks the "test1" exercise a bit closes #108
1 parent 17fa037 commit 02ceb0b

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

exercises/test1.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55

66
// Mary is buying apples. One apple usually costs 2 dollars, but if you buy
77
// more than 40 at once, each apple only costs 1! Write a function that calculates
8-
// the price of an order of apples given the order amount.
8+
// the price of an order of apples given the order amount. No hints this time!
99

10-
fn main() {
10+
// Put your function here!
11+
// fn ..... {
12+
13+
// Don't modify this function!
14+
#[test]
15+
fn verify_test() {
1116
let price1 = calculateprice(55);
1217
let price2 = calculateprice(40);
1318

14-
// Don't modify this!
15-
if price1 == 55 && price2 == 80 {
16-
println!("Good job!");
17-
} else {
18-
panic!("Uh oh! Wrong price!");
19-
}
19+
assert_eq!(price1, 55);
20+
assert_eq!(price2, 80);
2021
}

src/verify.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn verify() -> Result<(), ()> {
1414
compile_only("exercises/functions/functions3.rs")?;
1515
compile_only("exercises/functions/functions4.rs")?;
1616
compile_only("exercises/functions/functions5.rs")?;
17-
compile_only("exercises/test1.rs")?;
17+
test("exercises/test1.rs")?;
1818
compile_only("exercises/primitive_types/primitive_types1.rs")?;
1919
compile_only("exercises/primitive_types/primitive_types2.rs")?;
2020
compile_only("exercises/primitive_types/primitive_types3.rs")?;
@@ -87,19 +87,36 @@ pub fn test(filename: &str) -> Result<(), ()> {
8787
.args(&["--test", filename, "-o", "temp", "--color", "always"])
8888
.output()
8989
.expect("fail");
90-
bar.finish_and_clear();
9190
if testcmd.status.success() {
92-
let formatstr = format!("{} Successfully tested {}!", Emoji("✅", "✓"), filename);
93-
println!("{}", style(formatstr).green());
94-
clean();
95-
Ok(())
91+
bar.set_message(format!("Running {}...", filename).as_str());
92+
let runcmd = Command::new("./temp").output().expect("fail");
93+
bar.finish_and_clear();
94+
95+
if runcmd.status.success() {
96+
let formatstr = format!("{} Successfully tested {}!", Emoji("✅", "✓"), filename);
97+
println!("{}", style(formatstr).green());
98+
clean();
99+
Ok(())
100+
} else {
101+
let formatstr = format!(
102+
"{} Testing of {} failed! Please try again. Here's the output:",
103+
Emoji("⚠️ ", "!"),
104+
filename
105+
);
106+
println!("{}", style(formatstr).red());
107+
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
108+
clean();
109+
Err(())
110+
}
96111
} else {
112+
bar.finish_and_clear();
97113
let formatstr = format!(
98-
"{} Testing of {} failed! Please try again.",
114+
"{} Compiling of {} failed! Please try again. Here's the output:",
99115
Emoji("⚠️ ", "!"),
100116
filename
101117
);
102118
println!("{}", style(formatstr).red());
119+
println!("{}", String::from_utf8_lossy(&testcmd.stderr));
103120
clean();
104121
Err(())
105122
}

0 commit comments

Comments
 (0)