mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
test: Restore original config.yml after tests
Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
parent
fb8088c704
commit
4090fee0a6
1 changed files with 21 additions and 4 deletions
25
src/main.rs
25
src/main.rs
|
|
@ -196,18 +196,35 @@ mod tests {
|
|||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
/// RAII guard to ensure config file is cleaned up after test.
|
||||
struct ConfigGuard;
|
||||
/// RAII guard to manage config file during tests.
|
||||
/// It saves the original content of `config.yml` if it exists,
|
||||
/// and restores it when the guard goes out of scope.
|
||||
/// If the file didn't exist, it's removed.
|
||||
struct ConfigGuard {
|
||||
original_content: Option<String>,
|
||||
}
|
||||
|
||||
impl ConfigGuard {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
original_content: fs::read_to_string("config.yml").ok(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ConfigGuard {
|
||||
fn drop(&mut self) {
|
||||
let _ = fs::remove_file("config.yml");
|
||||
if let Some(content) = &self.original_content {
|
||||
fs::write("config.yml", content).expect("Failed to restore config.yml");
|
||||
} else {
|
||||
let _ = fs::remove_file("config.yml");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ensure_config() {
|
||||
let _guard = ConfigGuard; // Cleanup when _guard goes out of scope.
|
||||
let _guard = ConfigGuard::new(); // Cleanup when _guard goes out of scope.
|
||||
|
||||
// --- Test 1: File creation ---
|
||||
// Pre-condition: config.yml does not exist.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue