From 9a21aa38068b8c55ba96772bba9ff1730b3b1e3a Mon Sep 17 00:00:00 2001 From: Sandmeyer Date: Fri, 31 Jul 2026 20:38:42 +0800 Subject: [PATCH 1/2] Fix `dev check` rejecting exercises with CRLF line endings --- src/dev/check.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/dev/check.rs b/src/dev/check.rs index 16d04563ca..fb756bdbb8 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -118,7 +118,7 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result> { ); } - 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> { 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,20 @@ 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)); + } +} From 219f479f71b05a92e8cf92f0a94788fc15ebffe6 Mon Sep 17 00:00:00 2001 From: Sandmeyer Date: Fri, 31 Jul 2026 20:46:54 +0800 Subject: [PATCH 2/2] cargo fmt --- src/dev/check.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dev/check.rs b/src/dev/check.rs index fb756bdbb8..7d9b6e2403 100644 --- a/src/dev/check.rs +++ b/src/dev/check.rs @@ -416,7 +416,8 @@ mod tests { #[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"; + let content = + "fn main() {}\n\n#[cfg(test)]\nmod tests {\n #[test]\n fn it_works() {}\n}\n"; assert!(exercise_has_tests(content)); } }