mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-11-08 09:09:17 +01:00
Check for unexpected files in the solutions dir
This commit is contained in:
parent
a3ada0eee8
commit
6ae4a979f4
@ -1,4 +1,4 @@
|
|||||||
use anyhow::{anyhow, bail, Context, Error, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use std::{
|
use std::{
|
||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
fs::{self, read_dir, OpenOptions},
|
fs::{self, read_dir, OpenOptions},
|
||||||
@ -99,14 +99,19 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<hashbrown::HashSet<
|
|||||||
Ok(paths)
|
Ok(paths)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the `exercises` directory for unexpected files.
|
// Check `dir` for unexpected files.
|
||||||
fn check_unexpected_files(info_file_paths: &hashbrown::HashSet<PathBuf>) -> Result<()> {
|
// Only Rust files in `allowed_rust_files` and `README.md` files are allowed.
|
||||||
fn unexpected_file(path: &Path) -> Error {
|
// Only one level of directory nesting is allowed.
|
||||||
anyhow!("Found the file `{}`. Only `README.md` and Rust files related to an exercise in `info.toml` are allowed in the `exercises` directory", path.display())
|
fn check_unexpected_files(
|
||||||
}
|
dir: &str,
|
||||||
|
allowed_rust_files: &hashbrown::HashSet<PathBuf>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let unexpected_file = |path: &Path| {
|
||||||
|
anyhow!("Found the file `{}`. Only `README.md` and Rust files related to an exercise in `info.toml` are allowed in the `{dir}` directory", path.display())
|
||||||
|
};
|
||||||
|
|
||||||
for entry in read_dir("exercises").context("Failed to open the `exercises` directory")? {
|
for entry in read_dir(dir).with_context(|| format!("Failed to open the `{dir}` directory"))? {
|
||||||
let entry = entry.context("Failed to read the `exercises` directory")?;
|
let entry = entry.with_context(|| format!("Failed to read the `{dir}` directory"))?;
|
||||||
|
|
||||||
if entry.file_type().unwrap().is_file() {
|
if entry.file_type().unwrap().is_file() {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
@ -115,7 +120,7 @@ fn check_unexpected_files(info_file_paths: &hashbrown::HashSet<PathBuf>) -> Resu
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !info_file_paths.contains(&path) {
|
if !allowed_rust_files.contains(&path) {
|
||||||
return Err(unexpected_file(&path));
|
return Err(unexpected_file(&path));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,7 +144,7 @@ fn check_unexpected_files(info_file_paths: &hashbrown::HashSet<PathBuf>) -> Resu
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !info_file_paths.contains(&path) {
|
if !allowed_rust_files.contains(&path) {
|
||||||
return Err(unexpected_file(&path));
|
return Err(unexpected_file(&path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -156,17 +161,19 @@ fn check_exercises(info_file: &InfoFile) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let info_file_paths = check_info_file_exercises(info_file)?;
|
let info_file_paths = check_info_file_exercises(info_file)?;
|
||||||
check_unexpected_files(&info_file_paths)?;
|
check_unexpected_files("exercises", &info_file_paths)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_solutions(info_file: &InfoFile) -> Result<()> {
|
fn check_solutions(info_file: &InfoFile) -> Result<()> {
|
||||||
|
let mut paths = hashbrown::HashSet::with_capacity(info_file.exercises.len());
|
||||||
let target_dir = parse_target_dir()?;
|
let target_dir = parse_target_dir()?;
|
||||||
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
|
let mut output = Vec::with_capacity(OUTPUT_CAPACITY);
|
||||||
|
|
||||||
for exercise_info in &info_file.exercises {
|
for exercise_info in &info_file.exercises {
|
||||||
if !Path::new(&exercise_info.sol_path()).exists() {
|
let path = exercise_info.sol_path();
|
||||||
|
if !Path::new(&path).exists() {
|
||||||
// No solution to check.
|
// No solution to check.
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -181,8 +188,12 @@ fn check_solutions(info_file: &InfoFile) -> Result<()> {
|
|||||||
exercise_info.name,
|
exercise_info.name,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
paths.insert(PathBuf::from(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
check_unexpected_files("solutions", &paths)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user