Skip to content

Commit

Permalink
Add debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
willibrandon committed Jan 19, 2025
1 parent cbee16b commit 7483a63
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
22 changes: 20 additions & 2 deletions rustscout/src/replace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,28 +398,37 @@ impl FileReplacementPlan {
/// Create a backup of the file if backup is enabled
fn create_backup(&self, config: &ReplacementConfig) -> SearchResult<Option<PathBuf>> {
if !config.backup_enabled {
println!("Debug: Backup not enabled");
return Ok(None);
}

// 1) Figure out the workspace root
let workspace_root = detect_workspace_root(&self.file_path)?;
println!("Debug: Workspace root = {}", workspace_root.display());

// 2) Determine the "backups" subdirectory
let backup_dir = if let Some(ref dir) = config.backup_dir {
println!("Debug: Using user-specified backup dir: {}", dir.display());
dir.clone()
} else {
workspace_root.join(".rustscout").join("backups")
let default_dir = workspace_root.join(".rustscout").join("backups");
println!("Debug: Using default backup dir: {}", default_dir.display());
default_dir
};
println!("Debug: Creating backup dir: {}", backup_dir.display());
fs::create_dir_all(&backup_dir)?;

// 3) Compute a unique backup filename from the *relative path*
let relative = self
.file_path
.strip_prefix(&workspace_root)
.unwrap_or(&self.file_path);
println!("Debug: Relative path for backup: {}", relative.display());
let relative_str = relative
.to_string_lossy()
.replace("\\", "_")
.replace("/", "_");
println!("Debug: Sanitized relative path: {}", relative_str);

let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -430,9 +439,18 @@ impl FileReplacementPlan {
// e.g. "crate_a_lib.rs.1737267859"
let backup_name = format!("{}.{}", relative_str, timestamp);
let backup_path = backup_dir.join(&backup_name);
println!("Debug: Final backup path: {}", backup_path.display());

// 5) Copy original file to the new backup path
fs::copy(&self.file_path, &backup_path).map_err(SearchError::IoError)?;
println!(
"Debug: Copying from {} to {}",
self.file_path.display(),
backup_path.display()
);
match fs::copy(&self.file_path, &backup_path) {
Ok(_) => println!("Debug: Successfully created backup"),
Err(e) => println!("Debug: Failed to create backup: {}", e),
}

if config.preserve_metadata {
if let Ok(metadata) = fs::metadata(&self.file_path) {
Expand Down
32 changes: 30 additions & 2 deletions rustscout/tests/cli_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,23 @@ fn test_replace_basic() -> Result<()> {
fn test_replace_with_backup() -> Result<()> {
let dir = tempdir()?;
let backup_dir = dir.path().join("backups");
println!("Debug: Test backup dir = {}", backup_dir.display());
fs::create_dir_all(&backup_dir)?;
println!(
"Debug: Created backup dir, exists = {}",
backup_dir.exists()
);

let undo_dir = dir.path().join(".rustscout").join("undo");
fs::create_dir_all(&undo_dir)?;

create_test_files(&dir, &[("test.txt", "Hello world")])?;
let test_file = dir.path().join("test.txt");
println!(
"Debug: Test file = {}, exists = {}",
test_file.display(),
test_file.exists()
);

let config = ReplacementConfig {
patterns: vec![ReplacementPattern {
Expand Down Expand Up @@ -109,8 +120,25 @@ fn test_replace_with_backup() -> Result<()> {
fs::read_to_string(dir.path().join("test.txt"))?,
"World world"
);
// Check that the backup directory is not empty
assert!(fs::read_dir(&backup_dir)?.next().is_some());

// Enhanced backup directory check with diagnostics
println!("Debug: Checking backup dir after replacement");
match fs::read_dir(&backup_dir) {
Ok(entries) => {
let entries: Vec<_> = entries.collect();
println!("Debug: Found {} entries in backup dir", entries.len());
for entry in &entries {
if let Ok(entry) = entry {
println!("Debug: Found backup file: {}", entry.path().display());
}
}
assert!(!entries.is_empty(), "Backup directory should not be empty");
}
Err(e) => {
println!("Debug: Failed to read backup dir: {}", e);
assert!(false, "Failed to read backup directory: {}", e);
}
}
Ok(())
}

Expand Down

0 comments on commit 7483a63

Please sign in to comment.