Skip to content

Commit d1d5b83

Browse files
committed
chore: copy-to-container interface improvements
1 parent cf6f593 commit d1d5b83

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

testcontainers/src/core/copy.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::{
55

66
#[derive(Debug, Clone)]
77
pub struct CopyToContainer {
8-
pub target: String,
9-
pub source: CopyDataSource,
8+
target: String,
9+
source: CopyDataSource,
1010
}
1111

1212
#[derive(Debug, Clone)]
@@ -24,20 +24,28 @@ pub enum CopyToContaienrError {
2424
}
2525

2626
impl CopyToContainer {
27-
pub fn target_directory(&self) -> Result<String, CopyToContaienrError> {
28-
match path::Path::new(&self.target).parent() {
29-
Some(v) => Ok(v.display().to_string()),
30-
None => return Err(CopyToContaienrError::PathNameError(self.target.clone())),
27+
pub fn new(source: impl Into<CopyDataSource>, target: impl Into<String>) -> Self {
28+
Self {
29+
source: source.into(),
30+
target: target.into(),
3131
}
3232
}
3333

34-
pub async fn tar(&self) -> Result<bytes::Bytes, CopyToContaienrError> {
34+
pub(crate) fn target_directory(&self) -> Result<String, CopyToContaienrError> {
35+
path::Path::new(&self.target)
36+
.parent()
37+
.map(path::Path::display)
38+
.map(|dir| dir.to_string())
39+
.ok_or_else(|| CopyToContaienrError::PathNameError(self.target.clone()))
40+
}
41+
42+
pub(crate) async fn tar(&self) -> Result<bytes::Bytes, CopyToContaienrError> {
3543
self.source.tar(&self.target).await
3644
}
3745
}
3846

3947
impl CopyDataSource {
40-
pub async fn tar(
48+
pub(crate) async fn tar(
4149
&self,
4250
target_path: impl Into<String>,
4351
) -> Result<bytes::Bytes, CopyToContaienrError> {
@@ -46,10 +54,10 @@ impl CopyDataSource {
4654

4755
match self {
4856
CopyDataSource::File(file_path) => {
49-
let mut f = &mut tokio::fs::File::open(file_path)
57+
let f = &mut tokio::fs::File::open(file_path)
5058
.await
5159
.map_err(CopyToContaienrError::IoError)?;
52-
ar.append_file(&target_path, &mut f)
60+
ar.append_file(&target_path, f)
5361
.await
5462
.map_err(CopyToContaienrError::IoError)?;
5563
}

testcontainers/src/core/image/image_ext.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ pub trait ImageExt<I: Image> {
5959
fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I>;
6060

6161
/// Copies some source into the container as file
62-
fn with_copy_to(self, target: impl Into<String>, source: CopyDataSource)
63-
-> ContainerRequest<I>;
62+
fn with_copy_to(
63+
self,
64+
target: impl Into<String>,
65+
source: impl Into<CopyDataSource>,
66+
) -> ContainerRequest<I>;
6467

6568
/// Adds a port mapping to the container, mapping the host port to the container's internal port.
6669
///
@@ -90,6 +93,7 @@ pub trait ImageExt<I: Image> {
9093
/// cgroup namespace mode for the container. Possible values are:
9194
/// - [`CgroupnsMode::Private`]: the container runs in its own private cgroup namespace
9295
/// - [`CgroupnsMode::Host`]: use the host system's cgroup namespace
96+
///
9397
/// If not specified, the daemon default is used, which can either be `\"private\"` or `\"host\"`, depending on daemon version, kernel support and configuration.
9498
fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> ContainerRequest<I>;
9599

@@ -179,13 +183,13 @@ impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
179183
fn with_copy_to(
180184
self,
181185
target: impl Into<String>,
182-
source: CopyDataSource,
186+
source: impl Into<CopyDataSource>,
183187
) -> ContainerRequest<I> {
184188
let mut container_req = self.into();
185189
let target: String = target.into();
186190
container_req
187191
.copy_to_sources
188-
.push(CopyToContainer { target, source });
192+
.push(CopyToContainer::new(source, target));
189193
container_req
190194
}
191195

testcontainers/src/runners/async_runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ where
218218

219219
for copy_to_source in copy_to_sources {
220220
client
221-
.copy_to_container(&container_id, &copy_to_source)
221+
.copy_to_container(&container_id, copy_to_source)
222222
.await?;
223223
}
224224

testcontainers/tests/async_runner.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use testcontainers::{
88
CmdWaitFor, ExecCommand, WaitFor,
99
},
1010
runners::AsyncRunner,
11-
CopyDataSource, GenericImage, Image, ImageExt,
11+
GenericImage, Image, ImageExt,
1212
};
1313
use tokio::io::AsyncReadExt;
1414

@@ -204,10 +204,7 @@ async fn async_run_with_log_consumer() -> anyhow::Result<()> {
204204
async fn async_copy_files_to_container() -> anyhow::Result<()> {
205205
let container = GenericImage::new("alpine", "latest")
206206
.with_wait_for(WaitFor::seconds(2))
207-
.with_copy_to(
208-
"/tmp/somefile",
209-
CopyDataSource::from("foobar".to_string().into_bytes()),
210-
)
207+
.with_copy_to("/tmp/somefile", "foobar".to_string().into_bytes())
211208
.with_cmd(vec!["cat", "/tmp/somefile"])
212209
.start()
213210
.await?;

testcontainers/tests/sync_runner.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,7 @@ fn sync_copy_files_to_container() -> anyhow::Result<()> {
230230

231231
let container = GenericImage::new("alpine", "latest")
232232
.with_wait_for(WaitFor::seconds(2))
233-
.with_copy_to(
234-
"/tmp/somefile",
235-
CopyDataSource::Data("foobar".to_string().into_bytes()),
236-
)
233+
.with_copy_to("/tmp/somefile", "foobar".to_string().into_bytes())
237234
.with_cmd(vec!["cat", "/tmp/somefile"])
238235
.start()?;
239236

0 commit comments

Comments
 (0)