Run rustfmt on solutions in dev check

This commit is contained in:
mo8it 2024-08-01 19:14:09 +02:00
parent e0f0944bff
commit 65a8f6bb4b
4 changed files with 23 additions and 8 deletions

View file

@ -24,8 +24,6 @@ jobs:
globs: "exercises/**/*.md" globs: "exercises/**/*.md"
- name: Run cargo fmt - name: Run cargo fmt
run: cargo fmt --all --check run: cargo fmt --all --check
- name: Run rustfmt on solutions
run: rustfmt --check --edition 2021 --color always solutions/**/*.rs
test: test:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
strategy: strategy:

View file

@ -4,6 +4,7 @@
- Run the final check of all exercises in parallel. - Run the final check of all exercises in parallel.
- Small exercise improvements. - Small exercise improvements.
- `dev check`: Check that all solutions are formatted with `rustfmt`.
<a name="6.1.0"></a> <a name="6.1.0"></a>

View file

@ -15,7 +15,7 @@ authors = [
] ]
repository = "https://github.com/rust-lang/rustlings" repository = "https://github.com/rust-lang/rustlings"
license = "MIT" license = "MIT"
edition = "2021" edition = "2021" # On Update: Update the edition of the `rustfmt` command that checks the solutions.
[workspace.dependencies] [workspace.dependencies]
serde = { version = "1.0.204", features = ["derive"] } serde = { version = "1.0.204", features = ["derive"] }

View file

@ -4,6 +4,7 @@ use std::{
fs::{self, read_dir, OpenOptions}, fs::{self, read_dir, OpenOptions},
io::{self, Read, Write}, io::{self, Read, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
process::{Command, Stdio},
sync::atomic::{self, AtomicBool}, sync::atomic::{self, AtomicBool},
thread, thread,
}; };
@ -224,7 +225,7 @@ fn check_solutions(
cmd_runner: &CmdRunner, cmd_runner: &CmdRunner,
) -> Result<()> { ) -> Result<()> {
println!("Running all solutions. This may take a while…\n"); println!("Running all solutions. This may take a while…\n");
let sol_paths = thread::scope(|s| { thread::scope(|s| {
let handles = info_file let handles = info_file
.exercises .exercises
.iter() .iter()
@ -250,6 +251,14 @@ fn check_solutions(
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut sol_paths = hashbrown::HashSet::with_capacity(info_file.exercises.len()); let mut sol_paths = hashbrown::HashSet::with_capacity(info_file.exercises.len());
let mut fmt_cmd = Command::new("rustfmt");
fmt_cmd
.arg("--check")
.arg("--edition")
.arg("2021")
.arg("--color")
.arg("--always")
.stdin(Stdio::null());
for (exercise_name, handle) in info_file for (exercise_name, handle) in info_file
.exercises .exercises
@ -259,6 +268,7 @@ fn check_solutions(
{ {
match handle.join() { match handle.join() {
Ok(SolutionCheck::Success { sol_path }) => { Ok(SolutionCheck::Success { sol_path }) => {
fmt_cmd.arg(&sol_path);
sol_paths.insert(PathBuf::from(sol_path)); sol_paths.insert(PathBuf::from(sol_path));
} }
Ok(SolutionCheck::MissingRequired) => { Ok(SolutionCheck::MissingRequired) => {
@ -276,12 +286,18 @@ fn check_solutions(
} }
} }
Ok(sol_paths) let handle = s.spawn(move || check_unexpected_files("solutions", &sol_paths));
})?;
check_unexpected_files("solutions", &sol_paths)?; if !fmt_cmd
.status()
.context("Failed to run `rustfmt` on all solution files")?
.success()
{
bail!("Some solutions aren't formatted. Run `rustfmt` on them");
}
Ok(()) handle.join().unwrap()
})
} }
pub fn check(require_solutions: bool) -> Result<()> { pub fn check(require_solutions: bool) -> Result<()> {