Skip to content

Commit 71d3125

Browse files
author
fmoko
authored
Merge pull request #278 from jrvidal/output-mode
feature: adds "output" mode, resolves #270
2 parents 7ce4294 + 3b6d5c3 commit 71d3125

File tree

3 files changed

+65
-34
lines changed

3 files changed

+65
-34
lines changed

exercises/primitive_types/primitive_types4.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,4 @@
88
fn slice_out_of_array() {
99
let a = [1, 2, 3, 4, 5];
1010

11-
let nice_slice = ???
12-
13-
assert_eq!([2, 3, 4], nice_slice)
1411
}

src/verify.rs

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::exercise::{Exercise, Mode, State};
1+
use crate::exercise::{CompiledExercise, Exercise, Mode, State};
22
use console::style;
33
use indicatif::ProgressBar;
44

55
pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<(), &'a Exercise> {
66
for exercise in start_at {
77
let compile_result = match exercise.mode {
88
Mode::Test => compile_and_test(&exercise, RunMode::Interactive),
9-
Mode::Compile => compile_only(&exercise),
9+
Mode::Compile => compile_and_run_interactively(&exercise),
1010
Mode::Clippy => compile_only(&exercise),
1111
};
1212
if !compile_result.unwrap_or(false) {
@@ -30,52 +30,53 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
3030
let progress_bar = ProgressBar::new_spinner();
3131
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
3232
progress_bar.enable_steady_tick(100);
33-
let compilation_result = exercise.compile();
33+
34+
let _ = compile(&exercise, &progress_bar)?;
3435
progress_bar.finish_and_clear();
3536

36-
match compilation_result {
37-
Ok(_) => {
38-
success!("Successfully compiled {}!", exercise);
39-
Ok(prompt_for_completion(&exercise))
40-
}
41-
Err(output) => {
42-
warn!(
43-
"Compilation of {} failed! Compiler error message:\n",
44-
exercise
45-
);
46-
println!("{}", output.stderr);
47-
Err(())
48-
}
49-
}
37+
success!("Successfully compiled {}!", exercise);
38+
Ok(prompt_for_completion(&exercise, None))
5039
}
5140

52-
fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()> {
41+
fn compile_and_run_interactively(exercise: &Exercise) -> Result<bool, ()> {
5342
let progress_bar = ProgressBar::new_spinner();
54-
progress_bar.set_message(format!("Testing {}...", exercise).as_str());
43+
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
5544
progress_bar.enable_steady_tick(100);
5645

57-
let compilation_result = exercise.compile();
46+
let compilation = compile(&exercise, &progress_bar)?;
5847

59-
let compilation = match compilation_result {
60-
Ok(compilation) => compilation,
48+
progress_bar.set_message(format!("Running {}...", exercise).as_str());
49+
let result = compilation.run();
50+
progress_bar.finish_and_clear();
51+
52+
let output = match result {
53+
Ok(output) => output,
6154
Err(output) => {
62-
progress_bar.finish_and_clear();
63-
warn!(
64-
"Compiling of {} failed! Please try again. Here's the output:",
65-
exercise
66-
);
67-
println!("{}", output.stderr);
55+
warn!("Ran {} with errors", exercise);
56+
println!("{}", output.stdout);
6857
return Err(());
6958
}
7059
};
7160

61+
success!("Successfully ran {}!", exercise);
62+
63+
Ok(prompt_for_completion(&exercise, Some(output.stdout)))
64+
}
65+
66+
fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()> {
67+
let progress_bar = ProgressBar::new_spinner();
68+
progress_bar.set_message(format!("Testing {}...", exercise).as_str());
69+
progress_bar.enable_steady_tick(100);
70+
71+
let compilation = compile(exercise, &progress_bar)?;
7272
let result = compilation.run();
7373
progress_bar.finish_and_clear();
7474

7575
match result {
7676
Ok(_) => {
77+
success!("Successfully tested {}", &exercise);
7778
if let RunMode::Interactive = run_mode {
78-
Ok(prompt_for_completion(&exercise))
79+
Ok(prompt_for_completion(&exercise, None))
7980
} else {
8081
Ok(true)
8182
}
@@ -91,7 +92,27 @@ fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()>
9192
}
9293
}
9394

94-
fn prompt_for_completion(exercise: &Exercise) -> bool {
95+
fn compile<'a, 'b>(
96+
exercise: &'a Exercise,
97+
progress_bar: &'b ProgressBar,
98+
) -> Result<CompiledExercise<'a>, ()> {
99+
let compilation_result = exercise.compile();
100+
101+
match compilation_result {
102+
Ok(compilation) => Ok(compilation),
103+
Err(output) => {
104+
progress_bar.finish_and_clear();
105+
warn!(
106+
"Compiling of {} failed! Please try again. Here's the output:",
107+
exercise
108+
);
109+
println!("{}", output.stderr);
110+
Err(())
111+
}
112+
}
113+
}
114+
115+
fn prompt_for_completion(exercise: &Exercise, prompt_output: Option<String>) -> bool {
95116
let context = match exercise.state() {
96117
State::Done => return true,
97118
State::Pending(context) => context,
@@ -106,6 +127,15 @@ fn prompt_for_completion(exercise: &Exercise) -> bool {
106127
println!("");
107128
println!("🎉 🎉 {} 🎉 🎉", success_msg);
108129
println!("");
130+
131+
if let Some(output) = prompt_output {
132+
println!("Output:");
133+
println!("{}", separator());
134+
println!("{}", output);
135+
println!("{}", separator());
136+
println!("");
137+
}
138+
109139
println!("You can keep working on this exercise,");
110140
println!(
111141
"or jump into the next one by removing the {} comment:",
@@ -129,3 +159,7 @@ fn prompt_for_completion(exercise: &Exercise) -> bool {
129159

130160
false
131161
}
162+
163+
fn separator() -> console::StyledObject<&'static str> {
164+
style("====================").bold()
165+
}

tests/integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn verify_all_success() {
3131
}
3232

3333
#[test]
34-
fn verify_all_failure() {
34+
fn verify_fails_if_some_fails() {
3535
Command::cargo_bin("rustlings")
3636
.unwrap()
3737
.arg("v")

0 commit comments

Comments
 (0)