Skip to content
40 changes: 37 additions & 3 deletions src/Testcontainers/Builders/ContainerBuilder`3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ public TBuilderEntity WithResourceMapping(byte[] resourceContent, string filePat
return WithResourceMapping(new BinaryResourceMapping(resourceContent, filePath, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(byte[] resourceContent, FileInfo filePath, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(new BinaryResourceMapping(resourceContent, filePath.FullName, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(string source, string target, UnixFileModes fileMode = Unix.FileMode644)
{
Expand All @@ -232,12 +238,24 @@ public TBuilderEntity WithResourceMapping(DirectoryInfo source, string target, U
return WithResourceMapping(new FileResourceMapping(source.FullName, target, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(DirectoryInfo source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(new FileResourceMapping(source.FullName, target.FullName, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(FileInfo source, string target, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(new FileResourceMapping(source.FullName, target, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(FileInfo source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(new FileResourceMapping(source.FullName, target.FullName, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(FileInfo source, FileInfo target, UnixFileModes fileMode = Unix.FileMode644)
{
Expand All @@ -253,15 +271,31 @@ public TBuilderEntity WithResourceMapping(FileInfo source, FileInfo target, Unix

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(Uri source, string target, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(source, new DirectoryInfo(target), fileMode);
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(Uri source, DirectoryInfo target,
UnixFileModes fileMode = Unix.FileMode644)
{
if (source.IsFile)
{
return WithResourceMapping(new FileResourceMapping(source.AbsolutePath, target, fileMode));
return WithResourceMapping(new FileResourceMapping(source.AbsolutePath, target.FullName, fileMode));
}
else

return WithResourceMapping(new UriResourceMapping(source, target.FullName, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(Uri source, FileInfo target, UnixFileModes fileMode = Unix.FileMode644)
{
if (source.IsFile)
{
return WithResourceMapping(new UriResourceMapping(source, target, fileMode));
return WithResourceMapping(new FileInfo(source.AbsolutePath), target, fileMode);
}

return WithResourceMapping(new UriResourceMapping(source, target.FullName, fileMode));
}

/// <inheritdoc />
Expand Down
73 changes: 73 additions & 0 deletions src/Testcontainers/Builders/IContainerBuilder`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,19 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
[Obsolete("Use WithResourceMapping(byte[], FileInfo, UnixFileModes) instead.")]
TBuilderEntity WithResourceMapping(byte[] resourceContent, string filePath, UnixFileModes fileMode = Unix.FileMode644);

/// <summary>
/// Copies the byte array content to the created container before it starts.
/// </summary>
/// <param name="resourceContent">The byte array content of the resource mapping.</param>
/// <param name="filePath">The target file path to copy the file to.</param>
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
TBuilderEntity WithResourceMapping(byte[] resourceContent, FileInfo filePath, UnixFileModes fileMode = Unix.FileMode644);

/// <summary>
/// Copies the contents of a URL, a test host directory or file to the container before it starts.
/// </summary>
Expand All @@ -246,6 +257,7 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
[Obsolete("Use one of the more specific WithResourceMapping(…) methods instead.")]
TBuilderEntity WithResourceMapping(string source, string target, UnixFileModes fileMode = Unix.FileMode644);

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

/// <summary>
/// Copies a test host directory or file to the container before it starts.
/// </summary>
/// <param name="source">The source directory to be copied.</param>
/// <param name="target">The target directory path to copy the files to.</param>
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
TBuilderEntity WithResourceMapping(DirectoryInfo source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644);

/// <summary>
/// Copies a test host directory or file to the given directory in the container before it starts.
/// </summary>
/// <param name="source">The source file to be copied.</param>
/// <param name="target">The target directory path to copy the file to.</param>
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
[Obsolete("Use WithResourceMapping(FileInfo, DirectoryInfo, UnixFileModes) instead.")]
TBuilderEntity WithResourceMapping(FileInfo source, string target, UnixFileModes fileMode = Unix.FileMode644);

/// <summary>
/// Copies a test host directory or file to the given directory in the container before it starts.
/// </summary>
/// <param name="source">The source file to be copied.</param>
/// <param name="target">The target directory path to copy the file to.</param>
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
TBuilderEntity WithResourceMapping(FileInfo source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644);

/// <summary>
/// Copies a test host file to the container before it starts.
/// </summary>
Expand Down Expand Up @@ -295,8 +329,47 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
/// <param name="target">The target directory or file path to copy the file to.</param>
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
[Obsolete("Use WithResourceMapping(Uri, FileInfo, UnixFileModes) or WithResourceMapping(Uri, DirectoryInfo, UnixFileModes) instead.")]
TBuilderEntity WithResourceMapping(Uri source, string target, UnixFileModes fileMode = Unix.FileMode644);

/// <summary>
/// Copies a file from a URL to the container before it starts.
/// </summary>
/// <remarks>
/// If the Uri scheme corresponds to a file, the content is copied to the target
/// directory path. If the Uri scheme corresponds to HTTP or HTTPS, the content is
/// copied to the target file path.
///
/// The Uri scheme must be either <c>http</c>, <c>https</c> or <c>file</c>.
///
/// If you prefer to copy a file to a specific target file path instead of a
/// directory, use: <see cref="WithResourceMapping(FileInfo, FileInfo, UnixFileModes)" />.
/// </remarks>
/// <param name="source">The source URL of the file to be copied.</param>
/// <param name="target">The target directory path to copy the file to.</param>
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
TBuilderEntity WithResourceMapping(Uri source, DirectoryInfo target, UnixFileModes fileMode = Unix.FileMode644);

/// <summary>
/// Copies a file from a URL to the container before it starts.
/// </summary>
/// <remarks>
/// If the Uri scheme corresponds to a file, the content is copied to the target
/// directory path. If the Uri scheme corresponds to HTTP or HTTPS, the content is
/// copied to the target file path.
///
/// The Uri scheme must be either <c>http</c>, <c>https</c> or <c>file</c>.
/// </remarks>
/// <param name="source">The source URL of the file to be copied.</param>
/// <param name="target">The target file path to copy the file to.</param>
/// <param name="fileMode">The POSIX file mode permission.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
TBuilderEntity WithResourceMapping(Uri source, FileInfo target, UnixFileModes fileMode = Unix.FileMode644);

/// <summary>
/// Assigns the mount configuration to manage data in the container.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;

namespace DotNet.Testcontainers.Tests.Fixtures
{
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Commons;
using DotNet.Testcontainers.Containers;
using JetBrains.Annotations;
using Xunit;

[UsedImplicitly]
public sealed class AlpineBuilderFixture : IAsyncLifetime
{
private readonly List<IContainer> _containers = [];

public IContainer Container(Func<ContainerBuilder, ContainerBuilder> builder)
{
var containerBuilder = builder(new ContainerBuilder());

var container = containerBuilder
.WithImage(CommonImages.Alpine)
.WithCommand(CommonCommands.SleepInfinity)
.Build();

_containers.Add(container);

return container;
}

public ValueTask InitializeAsync()
{
return ValueTask.CompletedTask;
}

public async ValueTask DisposeAsync()
{
foreach (var container in _containers)
{
await container.DisposeAsync();
}
}
}
}
Loading
Loading