Skip to content

Commit

Permalink
Update copy_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Jul 13, 2023
1 parent 9e18806 commit e56228e
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 40 deletions.
62 changes: 31 additions & 31 deletions crates/huak_ops/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,39 @@ use std::{
};

#[allow(dead_code)]
/// Copy contents from one directory into a new directory at a provided `to` full path.
/// If the `to` directory doesn't exist this function creates it.
pub fn copy_dir<T: AsRef<Path>>(from: T, to: T) -> HuakResult<()> {
let (from, to) = (from.as_ref(), to.as_ref());
let mut stack = Vec::new();
stack.push(PathBuf::from(from));
let target_root = to.to_path_buf();
let from_component_count = from.to_path_buf().components().count();
while let Some(working_path) = stack.pop() {
// Collects the trailing components of the path
let src: PathBuf = working_path
.components()
.skip(from_component_count)
.collect();
let dest = if src.components().count() == 0 {
target_root.clone()
} else {
target_root.join(&src)
};
if !dest.exists() {
fs::create_dir_all(&dest)?;
}
for entry in fs::read_dir(working_path)? {
let path = entry?.path();
if path.is_dir() {
stack.push(path);
} else if let Some(filename) = path.file_name() {
fs::copy(&path, dest.join(filename))?;
pub fn copy_dir<T: AsRef<Path>>(
from: T,
to: T,
options: &CopyDirOptions,
) -> Result<(), Error> {
let from = from.as_ref();
let to = to.as_ref();

if from.is_dir() {
for entry in fs::read_dir(from)?.filter_map(|e| e.ok()) {
let entry_path = entry.path();
if options.exclude.contains(&entry_path) {
continue;
}

let destination = to.join(entry.file_name());
if entry.file_type()?.is_dir() {
fs::create_dir_all(&destination)?;
copy_dir(entry.path(), destination, options)?;
} else {
fs::copy(entry.path(), &destination)?;
}
}
}

Ok(())
}

#[derive(Default)]
pub struct CopyDirOptions {
/// Exclude paths
pub exclude: Vec<PathBuf>,
}

/// Get an iterator over all paths found in each directory.
pub fn flatten_directories(
directories: impl IntoIterator<Item = PathBuf>,
Expand Down Expand Up @@ -125,7 +123,8 @@ mod tests {
fn test_copy_dir() {
let to = tempdir().unwrap().into_path();
let from = crate::test_resources_dir_path().join("mock-project");
copy_dir(from, to.join("mock-project")).unwrap();
copy_dir(from, to.join("mock-project"), &CopyDirOptions::default())
.unwrap();

assert!(to.join("mock-project").exists());
assert!(to.join("mock-project").join("pyproject.toml").exists());
Expand All @@ -135,7 +134,8 @@ mod tests {
fn test_find_root_file_bottom_up() {
let tmp = tempdir().unwrap().into_path();
let from = crate::test_resources_dir_path().join("mock-project");
copy_dir(&from, &tmp.join("mock-project")).unwrap();
copy_dir(&from, &tmp.join("mock-project"), &CopyDirOptions::default())
.unwrap();
let res = find_root_file_bottom_up(
"pyproject.toml",
tmp.join("mock-project").as_path(),
Expand Down
4 changes: 3 additions & 1 deletion crates/huak_ops/src/ops/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn add_project_optional_dependencies(
mod tests {
use super::*;
use crate::{
fs,
fs::{self, CopyDirOptions},
ops::{test_config, test_venv},
test_resources_dir_path, Verbosity,
};
Expand All @@ -144,6 +144,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down Expand Up @@ -172,6 +173,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let group = "dev";
Expand Down
3 changes: 2 additions & 1 deletion crates/huak_ops/src/ops/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn build_project(
mod tests {
use super::*;
use crate::{
fs,
fs::{self, CopyDirOptions},
ops::{test_config, test_venv},
test_resources_dir_path, Verbosity,
};
Expand All @@ -73,6 +73,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down
7 changes: 6 additions & 1 deletion crates/huak_ops/src/ops/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ pub fn clean_project(
#[cfg(test)]
mod tests {
use super::*;
use crate::{fs, ops::test_config, test_resources_dir_path, Verbosity};
use crate::{
fs::{self, CopyDirOptions},
ops::test_config,
test_resources_dir_path, Verbosity,
};
use tempfile::tempdir;

#[test]
Expand All @@ -63,6 +67,7 @@ mod tests {
fs::copy_dir(
test_resources_dir_path().join("mock-project"),
dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down
3 changes: 2 additions & 1 deletion crates/huak_ops/src/ops/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn format_project(
mod tests {
use super::*;
use crate::{
fs,
fs::{self, CopyDirOptions},
ops::{test_config, test_venv},
test_resources_dir_path, Verbosity,
};
Expand All @@ -107,6 +107,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down
4 changes: 3 additions & 1 deletion crates/huak_ops/src/ops/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn install_project_dependencies(
mod tests {
use super::*;
use crate::{
fs,
fs::{self, CopyDirOptions},
ops::{test_config, test_venv},
package::Package,
test_resources_dir_path, Verbosity,
Expand All @@ -79,6 +79,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand All @@ -103,6 +104,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down
3 changes: 3 additions & 0 deletions crates/huak_ops/src/ops/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub fn lint_project(config: &Config, options: &LintOptions) -> HuakResult<()> {
#[cfg(test)]
mod tests {
use super::*;
use crate::fs::CopyDirOptions;
use crate::ops::{test_config, test_venv};
use crate::{fs, test_resources_dir_path, Verbosity};
use tempfile::tempdir;
Expand All @@ -112,6 +113,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand All @@ -132,6 +134,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down
4 changes: 3 additions & 1 deletion crates/huak_ops/src/ops/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod tests {
use super::*;
use crate::{
dependency::Dependency,
fs,
fs::{self, CopyDirOptions},
ops::{test_config, test_venv},
package::Package,
test_resources_dir_path, Verbosity,
Expand All @@ -76,6 +76,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down Expand Up @@ -117,6 +118,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down
5 changes: 4 additions & 1 deletion crates/huak_ops/src/ops/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub fn run_command_str(command: &str, config: &Config) -> HuakResult<()> {
mod tests {
use super::*;
use crate::{
environment::env_path_string, fs, ops::test_config,
environment::env_path_string,
fs::{self, CopyDirOptions},
ops::test_config,
test_resources_dir_path, Verbosity,
};
use tempfile::tempdir;
Expand All @@ -31,6 +33,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down
3 changes: 2 additions & 1 deletion crates/huak_ops/src/ops/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn test_project(config: &Config, options: &TestOptions) -> HuakResult<()> {
mod tests {
use super::*;
use crate::{
fs,
fs::{self, CopyDirOptions},
ops::{test_config, test_venv},
test_resources_dir_path, Verbosity,
};
Expand All @@ -76,6 +76,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down
4 changes: 3 additions & 1 deletion crates/huak_ops/src/ops/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn update_project_dependencies(
mod tests {
use super::*;
use crate::{
fs,
fs::{self, CopyDirOptions},
ops::{test_config, test_venv},
test_resources_dir_path, Verbosity,
};
Expand All @@ -102,6 +102,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand All @@ -122,6 +123,7 @@ mod tests {
fs::copy_dir(
&test_resources_dir_path().join("mock-project"),
&dir.path().join("mock-project"),
&CopyDirOptions::default(),
)
.unwrap();
let root = dir.path().join("mock-project");
Expand Down

0 comments on commit e56228e

Please sign in to comment.