rustlings/tests/dev_cargo_bins.rs

45 lines
1.4 KiB
Rust
Raw Normal View History

2024-03-31 20:11:52 -04:00
// Makes sure that `dev/Cargo.toml` is synced with `info.toml`.
// When this test fails, you just need to run `cargo run -p gen-dev-cargo-toml`.
2024-03-31 20:11:52 -04:00
use serde::Deserialize;
use std::fs;
#[derive(Deserialize)]
2024-04-13 21:13:33 -04:00
struct ExerciseInfo {
2024-03-31 20:11:52 -04:00
name: String,
2024-04-13 21:13:33 -04:00
dir: Option<String>,
2024-03-31 20:11:52 -04:00
}
#[derive(Deserialize)]
2024-04-13 21:13:33 -04:00
struct InfoFile {
exercises: Vec<ExerciseInfo>,
2024-03-31 20:11:52 -04:00
}
#[test]
fn dev_cargo_bins() {
2024-04-13 21:13:33 -04:00
let cargo_toml = fs::read_to_string("dev/Cargo.toml").unwrap();
2024-03-31 20:11:52 -04:00
2024-04-13 21:13:33 -04:00
let exercise_infos =
toml_edit::de::from_str::<InfoFile>(&fs::read_to_string("info.toml").unwrap())
.unwrap()
.exercises;
2024-03-31 20:11:52 -04:00
let mut start_ind = 0;
2024-04-13 21:13:33 -04:00
for exercise_info in exercise_infos {
let name_start = start_ind + cargo_toml[start_ind..].find('"').unwrap() + 1;
let name_end = name_start + cargo_toml[name_start..].find('"').unwrap();
assert_eq!(exercise_info.name, &cargo_toml[name_start..name_end]);
let path_start = name_end + cargo_toml[name_end + 1..].find('"').unwrap() + 2;
let path_end = path_start + cargo_toml[path_start..].find('"').unwrap();
let expected_path = if let Some(dir) = exercise_info.dir {
format!("../exercises/{dir}/{}.rs", exercise_info.name)
} else {
format!("../exercises/{}.rs", exercise_info.name)
};
assert_eq!(expected_path, &cargo_toml[path_start..path_end]);
2024-03-31 20:11:52 -04:00
start_ind = path_end + 1;
}
}