Skip to content

Commit 4bf0ddc

Browse files
committed
Check exercises unsolved
1 parent a365718 commit 4bf0ddc

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

rustlings-macros/info.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ https://github.com/rust-lang/rustlings/blob/main/CONTRIBUTING.md"""
3131
name = "intro1"
3232
dir = "00_intro"
3333
test = false
34+
skip_check_unsolved = true
3435
hint = """
3536
Enter `n` to move on to the next exercise.
3637
You might need to press ENTER after typing `n`."""

src/dev/check.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,46 @@ fn check_unexpected_files(
162162
Ok(())
163163
}
164164

165-
fn check_exercises(info_file: &InfoFile) -> Result<()> {
165+
fn check_exercises_unsolved(info_file: &InfoFile, target_dir: &Path) -> Result<()> {
166+
let error_occurred = AtomicBool::new(false);
167+
168+
println!(
169+
"Running all exercises to check that they aren't already solved. This may take a while…\n",
170+
);
171+
thread::scope(|s| {
172+
for exercise_info in &info_file.exercises {
173+
if exercise_info.skip_check_unsolved {
174+
continue;
175+
}
176+
177+
s.spawn(|| {
178+
let error = |e| {
179+
let mut stderr = io::stderr().lock();
180+
stderr.write_all(e).unwrap();
181+
stderr.write_all(b"\nProblem with the exercise ").unwrap();
182+
stderr.write_all(exercise_info.name.as_bytes()).unwrap();
183+
stderr.write_all(SEPARATOR).unwrap();
184+
error_occurred.store(true, atomic::Ordering::Relaxed);
185+
};
186+
187+
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
188+
match exercise_info.run_exercise(&mut output, target_dir) {
189+
Ok(true) => error(b"Already solved!"),
190+
Ok(false) => (),
191+
Err(e) => error(e.to_string().as_bytes()),
192+
}
193+
});
194+
}
195+
});
196+
197+
if error_occurred.load(atomic::Ordering::Relaxed) {
198+
bail!(CHECK_EXERCISES_UNSOLVED_ERR);
199+
}
200+
201+
Ok(())
202+
}
203+
204+
fn check_exercises(info_file: &InfoFile, target_dir: &Path) -> Result<()> {
166205
match info_file.format_version.cmp(&CURRENT_FORMAT_VERSION) {
167206
Ordering::Less => bail!("`format_version` < {CURRENT_FORMAT_VERSION} (supported version)\nPlease migrate to the latest format version"),
168207
Ordering::Greater => bail!("`format_version` > {CURRENT_FORMAT_VERSION} (supported version)\nTry updating the Rustlings program"),
@@ -172,15 +211,14 @@ fn check_exercises(info_file: &InfoFile) -> Result<()> {
172211
let info_file_paths = check_info_file_exercises(info_file)?;
173212
check_unexpected_files("exercises", &info_file_paths)?;
174213

175-
Ok(())
214+
check_exercises_unsolved(info_file, target_dir)
176215
}
177216

178-
fn check_solutions(require_solutions: bool, info_file: &InfoFile) -> Result<()> {
179-
let target_dir = parse_target_dir()?;
217+
fn check_solutions(require_solutions: bool, info_file: &InfoFile, target_dir: &Path) -> Result<()> {
180218
let paths = Mutex::new(hashbrown::HashSet::with_capacity(info_file.exercises.len()));
181219
let error_occurred = AtomicBool::new(false);
182220

183-
println!("Running all solutions. This may take a while...\n");
221+
println!("Running all solutions. This may take a while\n");
184222
thread::scope(|s| {
185223
for exercise_info in &info_file.exercises {
186224
s.spawn(|| {
@@ -206,7 +244,7 @@ fn check_solutions(require_solutions: bool, info_file: &InfoFile) -> Result<()>
206244
}
207245

208246
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
209-
match exercise_info.run_solution(&mut output, &target_dir) {
247+
match exercise_info.run_solution(&mut output, target_dir) {
210248
Ok(true) => {
211249
paths.lock().unwrap().insert(PathBuf::from(path));
212250
}
@@ -242,8 +280,9 @@ pub fn check(require_solutions: bool) -> Result<()> {
242280
check_cargo_toml(&info_file.exercises, &current_cargo_toml, b"")?;
243281
}
244282

245-
check_exercises(&info_file)?;
246-
check_solutions(require_solutions, &info_file)?;
283+
let target_dir = parse_target_dir()?;
284+
check_exercises(&info_file, &target_dir)?;
285+
check_solutions(require_solutions, &info_file, &target_dir)?;
247286

248287
println!("\nEverything looks fine!");
249288

@@ -252,3 +291,6 @@ pub fn check(require_solutions: bool) -> Result<()> {
252291

253292
const SEPARATOR: &[u8] =
254293
b"\n========================================================================================\n";
294+
295+
const CHECK_EXERCISES_UNSOLVED_ERR: &str = "At least one exercise is already solved or failed to run. See the output above.
296+
If this is an intro exercise that is intended to be already solved, add `skip_check_unsolved = true` to the exercise's metadata in the `info.toml` file.";

src/info_file.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ pub struct ExerciseInfo {
1111
pub name: String,
1212
/// Exercise's directory name inside the `exercises/` directory.
1313
pub dir: Option<String>,
14-
#[serde(default = "default_true")]
1514
/// Run `cargo test` on the exercise.
15+
#[serde(default = "default_true")]
1616
pub test: bool,
1717
/// Deny all Clippy warnings.
1818
#[serde(default)]
1919
pub strict_clippy: bool,
2020
/// The exercise's hint to be shown to the user on request.
2121
pub hint: String,
22+
/// The exercise is already solved. Ignore it when checking that all exercises are unsolved.
23+
#[serde(default)]
24+
pub skip_check_unsolved: bool,
2225
}
2326
#[inline(always)]
2427
const fn default_true() -> bool {

0 commit comments

Comments
 (0)