Skip to content

Commit 3399e09

Browse files
committed
Improve WithResourceMapping API
1 parent 9eec936 commit 3399e09

File tree

4 files changed

+433
-3
lines changed

4 files changed

+433
-3
lines changed

src/Testcontainers/Builders/ContainerBuilder`3.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ public TBuilderEntity WithResourceMapping(byte[] resourceContent, string filePat
206206
return WithResourceMapping(new BinaryResourceMapping(resourceContent, filePath, fileMode));
207207
}
208208

209+
/// <inheritdoc />
210+
public TBuilderEntity WithResourceMapping(byte[] resourceContent, FileInfo filePath, UnixFileModes fileMode = Unix.FileMode644)
211+
{
212+
return WithResourceMapping(new BinaryResourceMapping(resourceContent, filePath.FullName, fileMode));
213+
}
214+
209215
/// <inheritdoc />
210216
public TBuilderEntity WithResourceMapping(string source, string target, UnixFileModes fileMode = Unix.FileMode644)
211217
{
@@ -232,12 +238,24 @@ public TBuilderEntity WithResourceMapping(DirectoryInfo source, string target, U
232238
return WithResourceMapping(new FileResourceMapping(source.FullName, target, fileMode));
233239
}
234240

241+
/// <inheritdoc />
242+
public TBuilderEntity WithResourceMapping(DirectoryInfo source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644)
243+
{
244+
return WithResourceMapping(new FileResourceMapping(source.FullName, target.FullName, fileMode));
245+
}
246+
235247
/// <inheritdoc />
236248
public TBuilderEntity WithResourceMapping(FileInfo source, string target, UnixFileModes fileMode = Unix.FileMode644)
237249
{
238250
return WithResourceMapping(new FileResourceMapping(source.FullName, target, fileMode));
239251
}
240252

253+
/// <inheritdoc />
254+
public TBuilderEntity WithResourceMapping(FileInfo source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644)
255+
{
256+
return WithResourceMapping(new FileResourceMapping(source.FullName, target.FullName, fileMode));
257+
}
258+
241259
/// <inheritdoc />
242260
public TBuilderEntity WithResourceMapping(FileInfo source, FileInfo target, UnixFileModes fileMode = Unix.FileMode644)
243261
{
@@ -253,15 +271,31 @@ public TBuilderEntity WithResourceMapping(FileInfo source, FileInfo target, Unix
253271

254272
/// <inheritdoc />
255273
public TBuilderEntity WithResourceMapping(Uri source, string target, UnixFileModes fileMode = Unix.FileMode644)
274+
{
275+
return WithResourceMapping(source, new DirectoryInfo(target), fileMode);
276+
}
277+
278+
/// <inheritdoc />
279+
public TBuilderEntity WithResourceMapping(Uri source, DirectoryInfo target,
280+
UnixFileModes fileMode = Unix.FileMode644)
256281
{
257282
if (source.IsFile)
258283
{
259-
return WithResourceMapping(new FileResourceMapping(source.AbsolutePath, target, fileMode));
284+
return WithResourceMapping(new FileResourceMapping(source.AbsolutePath, target.FullName, fileMode));
260285
}
261-
else
286+
287+
return WithResourceMapping(new UriResourceMapping(source, target.FullName, fileMode));
288+
}
289+
290+
/// <inheritdoc />
291+
public TBuilderEntity WithResourceMapping(Uri source, FileInfo target, UnixFileModes fileMode = Unix.FileMode644)
292+
{
293+
if (source.IsFile)
262294
{
263-
return WithResourceMapping(new UriResourceMapping(source, target, fileMode));
295+
return WithResourceMapping(new FileInfo(source.AbsolutePath), target, fileMode);
264296
}
297+
298+
return WithResourceMapping(new UriResourceMapping(source, target.FullName, fileMode));
265299
}
266300

267301
/// <inheritdoc />

src/Testcontainers/Builders/IContainerBuilder`2.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,19 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
228228
/// <param name="fileMode">The POSIX file mode permission.</param>
229229
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
230230
[PublicAPI]
231+
[Obsolete("Use WithResourceMapping(byte[], FileInfo, UnixFileModes) instead.")]
231232
TBuilderEntity WithResourceMapping(byte[] resourceContent, string filePath, UnixFileModes fileMode = Unix.FileMode644);
232233

234+
/// <summary>
235+
/// Copies the byte array content to the created container before it starts.
236+
/// </summary>
237+
/// <param name="resourceContent">The byte array content of the resource mapping.</param>
238+
/// <param name="filePath">The target file path to copy the file to.</param>
239+
/// <param name="fileMode">The POSIX file mode permission.</param>
240+
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
241+
[PublicAPI]
242+
TBuilderEntity WithResourceMapping(byte[] resourceContent, FileInfo filePath, UnixFileModes fileMode = Unix.FileMode644);
243+
233244
/// <summary>
234245
/// Copies the contents of a URL, a test host directory or file to the container before it starts.
235246
/// </summary>
@@ -246,6 +257,7 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
246257
/// <param name="fileMode">The POSIX file mode permission.</param>
247258
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
248259
[PublicAPI]
260+
[Obsolete("Use one of the more specific WithResourceMapping(…) methods instead.")]
249261
TBuilderEntity WithResourceMapping(string source, string target, UnixFileModes fileMode = Unix.FileMode644);
250262

251263
/// <summary>
@@ -256,18 +268,40 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
256268
/// <param name="fileMode">The POSIX file mode permission.</param>
257269
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
258270
[PublicAPI]
271+
[Obsolete("Use WithResourceMapping(DirectoryInfo, DirectoryInfo, UnixFileModes) instead.")]
259272
TBuilderEntity WithResourceMapping(DirectoryInfo source, string target, UnixFileModes fileMode = Unix.FileMode644);
260273

261274
/// <summary>
262275
/// Copies a test host directory or file to the container before it starts.
263276
/// </summary>
277+
/// <param name="source">The source directory to be copied.</param>
278+
/// <param name="target">The target directory path to copy the files to.</param>
279+
/// <param name="fileMode">The POSIX file mode permission.</param>
280+
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
281+
[PublicAPI]
282+
TBuilderEntity WithResourceMapping(DirectoryInfo source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644);
283+
284+
/// <summary>
285+
/// Copies a test host directory or file to the given directory in the container before it starts.
286+
/// </summary>
264287
/// <param name="source">The source file to be copied.</param>
265288
/// <param name="target">The target directory path to copy the file to.</param>
266289
/// <param name="fileMode">The POSIX file mode permission.</param>
267290
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
268291
[PublicAPI]
292+
[Obsolete("Use WithResourceMapping(FileInfo, DirectoryInfo, UnixFileModes) instead.")]
269293
TBuilderEntity WithResourceMapping(FileInfo source, string target, UnixFileModes fileMode = Unix.FileMode644);
270294

295+
/// <summary>
296+
/// Copies a test host directory or file to the given directory in the container before it starts.
297+
/// </summary>
298+
/// <param name="source">The source file to be copied.</param>
299+
/// <param name="target">The target directory path to copy the file to.</param>
300+
/// <param name="fileMode">The POSIX file mode permission.</param>
301+
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
302+
[PublicAPI]
303+
TBuilderEntity WithResourceMapping(FileInfo source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644);
304+
271305
/// <summary>
272306
/// Copies a test host file to the container before it starts.
273307
/// </summary>
@@ -295,8 +329,47 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
295329
/// <param name="target">The target directory or file path to copy the file to.</param>
296330
/// <param name="fileMode">The POSIX file mode permission.</param>
297331
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
332+
[PublicAPI]
333+
[Obsolete("Use WithResourceMapping(Uri, FileInfo, UnixFileModes) or WithResourceMapping(Uri, DirectoryInfo, UnixFileModes) instead.")]
298334
TBuilderEntity WithResourceMapping(Uri source, string target, UnixFileModes fileMode = Unix.FileMode644);
299335

336+
/// <summary>
337+
/// Copies a file from a URL to the container before it starts.
338+
/// </summary>
339+
/// <remarks>
340+
/// If the Uri scheme corresponds to a file, the content is copied to the target
341+
/// directory path. If the Uri scheme corresponds to HTTP or HTTPS, the content is
342+
/// copied to the target file path.
343+
///
344+
/// The Uri scheme must be either <c>http</c>, <c>https</c> or <c>file</c>.
345+
///
346+
/// If you prefer to copy a file to a specific target file path instead of a
347+
/// directory, use: <see cref="WithResourceMapping(FileInfo, FileInfo, UnixFileModes)" />.
348+
/// </remarks>
349+
/// <param name="source">The source URL of the file to be copied.</param>
350+
/// <param name="target">The target directory path to copy the file to.</param>
351+
/// <param name="fileMode">The POSIX file mode permission.</param>
352+
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
353+
[PublicAPI]
354+
TBuilderEntity WithResourceMapping(Uri source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644);
355+
356+
/// <summary>
357+
/// Copies a file from a URL to the container before it starts.
358+
/// </summary>
359+
/// <remarks>
360+
/// If the Uri scheme corresponds to a file, the content is copied to the target
361+
/// directory path. If the Uri scheme corresponds to HTTP or HTTPS, the content is
362+
/// copied to the target file path.
363+
///
364+
/// The Uri scheme must be either <c>http</c>, <c>https</c> or <c>file</c>.
365+
/// </remarks>
366+
/// <param name="source">The source URL of the file to be copied.</param>
367+
/// <param name="target">The target file path to copy the file to.</param>
368+
/// <param name="fileMode">The POSIX file mode permission.</param>
369+
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
370+
[PublicAPI]
371+
TBuilderEntity WithResourceMapping(Uri source, FileInfo target, UnixFileModes fileMode = Unix.FileMode644);
372+
300373
/// <summary>
301374
/// Assigns the mount configuration to manage data in the container.
302375
/// </summary>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
3+
namespace DotNet.Testcontainers.Tests.Fixtures
4+
{
5+
using System;
6+
using System.Threading.Tasks;
7+
using DotNet.Testcontainers.Builders;
8+
using DotNet.Testcontainers.Commons;
9+
using DotNet.Testcontainers.Containers;
10+
using JetBrains.Annotations;
11+
using Xunit;
12+
13+
[UsedImplicitly]
14+
public sealed class AlpineBuilderFixture : IAsyncLifetime
15+
{
16+
private readonly List<IContainer> _containers = [];
17+
18+
public IContainer Container(Func<ContainerBuilder, ContainerBuilder> builder)
19+
{
20+
var containerBuilder = builder(new ContainerBuilder());
21+
22+
var container = containerBuilder
23+
.WithImage(CommonImages.Alpine)
24+
.WithCommand(CommonCommands.SleepInfinity)
25+
.Build();
26+
27+
_containers.Add(container);
28+
29+
return container;
30+
}
31+
32+
public ValueTask InitializeAsync()
33+
{
34+
return ValueTask.CompletedTask;
35+
}
36+
37+
public async ValueTask DisposeAsync()
38+
{
39+
foreach (var container in _containers)
40+
{
41+
await container.DisposeAsync();
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)