diff --git a/testcontainers/src/core/copy.rs b/testcontainers/src/core/copy.rs index d821b3d5..cc6f67b5 100644 --- a/testcontainers/src/core/copy.rs +++ b/testcontainers/src/core/copy.rs @@ -5,8 +5,8 @@ use std::{ #[derive(Debug, Clone)] pub struct CopyToContainer { - pub target: String, - pub source: CopyDataSource, + target: String, + source: CopyDataSource, } #[derive(Debug, Clone)] @@ -24,20 +24,28 @@ pub enum CopyToContaienrError { } impl CopyToContainer { - pub fn target_directory(&self) -> Result { - match path::Path::new(&self.target).parent() { - Some(v) => Ok(v.display().to_string()), - None => return Err(CopyToContaienrError::PathNameError(self.target.clone())), + pub fn new(source: impl Into, target: impl Into) -> Self { + Self { + source: source.into(), + target: target.into(), } } - pub async fn tar(&self) -> Result { + pub(crate) fn target_directory(&self) -> Result { + path::Path::new(&self.target) + .parent() + .map(path::Path::display) + .map(|dir| dir.to_string()) + .ok_or_else(|| CopyToContaienrError::PathNameError(self.target.clone())) + } + + pub(crate) async fn tar(&self) -> Result { self.source.tar(&self.target).await } } impl CopyDataSource { - pub async fn tar( + pub(crate) async fn tar( &self, target_path: impl Into, ) -> Result { @@ -46,10 +54,10 @@ impl CopyDataSource { match self { CopyDataSource::File(file_path) => { - let mut f = &mut tokio::fs::File::open(file_path) + let f = &mut tokio::fs::File::open(file_path) .await .map_err(CopyToContaienrError::IoError)?; - ar.append_file(&target_path, &mut f) + ar.append_file(&target_path, f) .await .map_err(CopyToContaienrError::IoError)?; } diff --git a/testcontainers/src/core/image/image_ext.rs b/testcontainers/src/core/image/image_ext.rs index c8228503..64aa5ddb 100644 --- a/testcontainers/src/core/image/image_ext.rs +++ b/testcontainers/src/core/image/image_ext.rs @@ -59,8 +59,11 @@ pub trait ImageExt { fn with_mount(self, mount: impl Into) -> ContainerRequest; /// Copies some source into the container as file - fn with_copy_to(self, target: impl Into, source: CopyDataSource) - -> ContainerRequest; + fn with_copy_to( + self, + target: impl Into, + source: impl Into, + ) -> ContainerRequest; /// Adds a port mapping to the container, mapping the host port to the container's internal port. /// @@ -90,6 +93,7 @@ pub trait ImageExt { /// cgroup namespace mode for the container. Possible values are: /// - [`CgroupnsMode::Private`]: the container runs in its own private cgroup namespace /// - [`CgroupnsMode::Host`]: use the host system's cgroup namespace + /// /// If not specified, the daemon default is used, which can either be `\"private\"` or `\"host\"`, depending on daemon version, kernel support and configuration. fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> ContainerRequest; @@ -179,13 +183,13 @@ impl>, I: Image> ImageExt for RI { fn with_copy_to( self, target: impl Into, - source: CopyDataSource, + source: impl Into, ) -> ContainerRequest { let mut container_req = self.into(); let target: String = target.into(); container_req .copy_to_sources - .push(CopyToContainer { target, source }); + .push(CopyToContainer::new(source, target)); container_req } diff --git a/testcontainers/src/runners/async_runner.rs b/testcontainers/src/runners/async_runner.rs index 13da3067..401aee08 100644 --- a/testcontainers/src/runners/async_runner.rs +++ b/testcontainers/src/runners/async_runner.rs @@ -218,7 +218,7 @@ where for copy_to_source in copy_to_sources { client - .copy_to_container(&container_id, ©_to_source) + .copy_to_container(&container_id, copy_to_source) .await?; } diff --git a/testcontainers/tests/async_runner.rs b/testcontainers/tests/async_runner.rs index 00b1dd60..8033438a 100644 --- a/testcontainers/tests/async_runner.rs +++ b/testcontainers/tests/async_runner.rs @@ -8,7 +8,7 @@ use testcontainers::{ CmdWaitFor, ExecCommand, WaitFor, }, runners::AsyncRunner, - CopyDataSource, GenericImage, Image, ImageExt, + GenericImage, Image, ImageExt, }; use tokio::io::AsyncReadExt; @@ -204,10 +204,7 @@ async fn async_run_with_log_consumer() -> anyhow::Result<()> { async fn async_copy_files_to_container() -> anyhow::Result<()> { let container = GenericImage::new("alpine", "latest") .with_wait_for(WaitFor::seconds(2)) - .with_copy_to( - "/tmp/somefile", - CopyDataSource::from("foobar".to_string().into_bytes()), - ) + .with_copy_to("/tmp/somefile", "foobar".to_string().into_bytes()) .with_cmd(vec!["cat", "/tmp/somefile"]) .start() .await?; diff --git a/testcontainers/tests/sync_runner.rs b/testcontainers/tests/sync_runner.rs index 791a6787..709f6f90 100644 --- a/testcontainers/tests/sync_runner.rs +++ b/testcontainers/tests/sync_runner.rs @@ -230,10 +230,7 @@ fn sync_copy_files_to_container() -> anyhow::Result<()> { let container = GenericImage::new("alpine", "latest") .with_wait_for(WaitFor::seconds(2)) - .with_copy_to( - "/tmp/somefile", - CopyDataSource::Data("foobar".to_string().into_bytes()), - ) + .with_copy_to("/tmp/somefile", "foobar".to_string().into_bytes()) .with_cmd(vec!["cat", "/tmp/somefile"]) .start()?;