Merge pull request #2425 from SandmeyerX/main

Fix `dev check` rejecting exercises with CRLF line endings
This commit is contained in:
Remo Senekowitsch 2026-07-31 19:13:47 +02:00 committed by GitHub
commit 2f99667f39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -118,7 +118,7 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
);
}
let contains_tests = file_buf.contains("#[test]\n");
let contains_tests = exercise_has_tests(&file_buf);
if exercise_info.test {
if !contains_tests {
bail!(
@ -139,6 +139,13 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
Ok(paths)
}
// Check if the exercise contains `#[test]`-annotated tests.
// CRLF line endings are normalized first so that the check doesn't fail
// on files with Windows line endings (e.g. `#[test]\r\n`).
fn exercise_has_tests(file_content: &str) -> bool {
file_content.replace("\r\n", "\n").contains("#[test]\n")
}
// Check `dir` for unexpected files.
// Only Rust files in `allowed_rust_files` and `README.md` files are allowed.
// Only one level of directory nesting is allowed.
@ -396,3 +403,21 @@ pub fn check(require_solutions: bool) -> Result<()> {
}
const SKIP_CHECK_UNSOLVED_HINT: &str = "If this is an introduction exercise that is intended to be already solved, add `skip_check_unsolved = true` to the exercise's metadata in the `info.toml` file";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_tests_in_files_with_crlf_line_endings() {
let content = "fn main() {}\r\n\r\n#[cfg(test)]\r\nmod tests {\r\n #[test]\r\n fn it_works() {}\r\n}\r\n";
assert!(exercise_has_tests(content));
}
#[test]
fn detects_tests_in_files_with_lf_line_endings() {
let content =
"fn main() {}\n\n#[cfg(test)]\nmod tests {\n #[test]\n fn it_works() {}\n}\n";
assert!(exercise_has_tests(content));
}
}