Skip to content

Commit 78552eb

Browse files
committed
Auto merge of #141 - cjpearce:fix/run-panics-on-compile-fail, r=komaeda
Stop run from panicking when compile fails Currently if you use the `rustlings run` command and your program fails to compile, rustlings will panic while trying to exit. First I've added a couple of integration tests to cover this case, which also meant moving a few tests so that the new fixtures didn't cause `verify_all_success` to fail. Then I noticed that the existing integration tests that test for failure pass even when rustlings panics, preventing the new tests from failing. I've updated the integration tests to distinguish between when rustlings has failed in the way that we want (exit code 1) rather than a panic (exit code 101). Finally I fixed the actual panic, which was just caused by unwrapping when rustlings should probably be exiting cleanly.
2 parents fbd0ccb + 0c7bd12 commit 78552eb

File tree

9 files changed

+53
-12
lines changed

9 files changed

+53
-12
lines changed

src/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,11 @@ fn main() {
5757
}
5858

5959
if let Some(matches) = matches.subcommand_matches("run") {
60-
run(matches.clone()).unwrap();
60+
run(matches.clone()).unwrap_or_else(|_| std::process::exit(1));
6161
}
6262

6363
if matches.subcommand_matches("verify").is_some() {
64-
match verify(None) {
65-
Ok(_) => {}
66-
Err(_) => std::process::exit(1),
67-
}
64+
verify(None).unwrap_or_else(|_| std::process::exit(1));
6865
}
6966

7067
if matches.subcommand_matches("watch").is_some() {

tests/fixture/failure/compFailure.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
let
3+
}

tests/fixture/failure/info.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[exercises]]
2+
path = "compFailure.rs"
3+
mode = "compile"
4+
5+
[[exercises]]
6+
path = "testFailure.rs"
7+
mode = "test"

tests/fixture/failure/testFailure.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn passing() {
3+
asset!(true);
4+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/integration_tests.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,85 @@ fn fails_when_in_wrong_dir() {
1313
.unwrap()
1414
.current_dir("tests/")
1515
.assert()
16-
.failure();
16+
.code(1);
1717
}
1818

1919
#[test]
2020
fn verify_all_success() {
2121
Command::cargo_bin("rustlings")
2222
.unwrap()
2323
.arg("v")
24-
.current_dir("tests/fixture/")
24+
.current_dir("tests/fixture/success")
2525
.assert()
2626
.success();
2727
}
2828

29+
#[test]
30+
fn verify_all_failure() {
31+
Command::cargo_bin("rustlings")
32+
.unwrap()
33+
.arg("v")
34+
.current_dir("tests/fixture/failure")
35+
.assert()
36+
.code(1);
37+
}
38+
2939
#[test]
3040
fn run_single_compile_success() {
3141
Command::cargo_bin("rustlings")
3242
.unwrap()
3343
.args(&["r", "compSuccess.rs"])
34-
.current_dir("tests/fixture/")
44+
.current_dir("tests/fixture/success/")
3545
.assert()
3646
.success();
3747
}
3848

49+
#[test]
50+
fn run_single_compile_failure() {
51+
Command::cargo_bin("rustlings")
52+
.unwrap()
53+
.args(&["r", "compFailure.rs"])
54+
.current_dir("tests/fixture/failure/")
55+
.assert()
56+
.code(1);
57+
}
58+
3959
#[test]
4060
fn run_single_test_success() {
4161
Command::cargo_bin("rustlings")
4262
.unwrap()
4363
.args(&["r", "testSuccess.rs"])
44-
.current_dir("tests/fixture/")
64+
.current_dir("tests/fixture/success/")
4565
.assert()
4666
.success();
4767
}
4868

69+
#[test]
70+
fn run_single_test_failure() {
71+
Command::cargo_bin("rustlings")
72+
.unwrap()
73+
.args(&["r", "testFailure.rs"])
74+
.current_dir("tests/fixture/failure/")
75+
.assert()
76+
.code(1);
77+
}
78+
4979
#[test]
5080
fn run_single_test_no_filename() {
5181
Command::cargo_bin("rustlings")
5282
.unwrap()
5383
.arg("r")
5484
.current_dir("tests/fixture/")
5585
.assert()
56-
.failure();
86+
.code(1);
5787
}
5888

5989
#[test]
6090
fn run_single_test_no_exercise() {
6191
Command::cargo_bin("rustlings")
6292
.unwrap()
6393
.args(&["r", "compNoExercise.rs"])
64-
.current_dir("tests/fixture/")
94+
.current_dir("tests/fixture/failure")
6595
.assert()
66-
.failure();
96+
.code(1);
6797
}

0 commit comments

Comments
 (0)