-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If target is None, use current directory as default
- Loading branch information
1 parent
5e26f4d
commit 985ec52
Showing
5 changed files
with
143 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
use walkdir::WalkDir; | ||
|
||
pub fn copy_dir_to(src: &Path, dst: &Path) -> std::io::Result<()> { | ||
if !dst.is_dir() { | ||
fs::create_dir_all(dst)?; | ||
} | ||
|
||
for entry in WalkDir::new(src) { | ||
let entry = entry?; | ||
let src_path = entry.path(); | ||
let relative_path = src_path | ||
.strip_prefix(src) | ||
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?; | ||
let dst_path = dst.join(relative_path); | ||
|
||
if src_path.is_dir() { | ||
fs::create_dir_all(&dst_path)?; | ||
} else { | ||
fs::copy(&src_path, &dst_path)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs::File; | ||
use std::io::prelude::*; | ||
use tempfile::tempdir; | ||
|
||
#[test] | ||
fn test_copy_dir_to() { | ||
// Create a temporary directory. | ||
let src_dir = tempdir().unwrap(); | ||
let dst_dir = tempdir().unwrap(); | ||
|
||
// Create a file in the source directory. | ||
let file_path = src_dir.path().join("test.txt"); | ||
let mut file = File::create(&file_path).unwrap(); | ||
writeln!(file, "Hello, world!").unwrap(); | ||
|
||
// Copy the source directory to the destination directory. | ||
copy_dir_to(src_dir.path(), dst_dir.path()).unwrap(); | ||
|
||
// Check if the file exists in the destination directory. | ||
let copied_file_path = dst_dir.path().join("test.txt"); | ||
assert!(copied_file_path.exists()); | ||
|
||
// Check the content of the copied file. | ||
let mut copied_file = File::open(copied_file_path).unwrap(); | ||
let mut contents = String::new(); | ||
copied_file.read_to_string(&mut contents).unwrap(); | ||
assert_eq!(contents, "Hello, world!\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod cache; | ||
pub mod encode; | ||
pub mod fs; | ||
pub mod git_utils; | ||
pub mod parser; | ||
pub mod process; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters