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

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(byte[] resourceContent, FileInfo target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(resourceContent, FilePath.Of(target.ToString()), uid, gid, fileMode);
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(byte[] resourceContent, FilePath target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(new BinaryResourceMapping(resourceContent, target.Value, uid, gid, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(string source, string target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
Expand All @@ -239,35 +251,91 @@ public TBuilderEntity WithResourceMapping(DirectoryInfo source, string target, u
return WithResourceMapping(new FileResourceMapping(source.FullName, target, uid, gid, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(DirectoryInfo source, DirectoryInfo target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(DirectoryPath.Of(source.FullName), DirectoryPath.Of(target.ToString()), uid, gid, fileMode);
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(DirectoryPath source, DirectoryPath target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(new FileResourceMapping(source.Value, target.Value, uid, gid, fileMode));
}

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

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(FileInfo source, DirectoryInfo target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(FilePath.Of(source.FullName), DirectoryPath.Of(target.ToString()), uid, gid, fileMode);
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(FilePath source, DirectoryPath target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(new FileResourceMapping(source.Value, target.Value, uid, gid, fileMode));
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(FileInfo source, FileInfo target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
using (var fileStream = source.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
return WithResourceMapping(FilePath.Of(source.FullName), FilePath.Of(target.ToString()), uid, gid, fileMode);
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(FilePath source, FilePath target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
using (var fileStream = new FileInfo(source.Value).Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var streamReader = new BinaryReader(fileStream))
{
var resourceContent = streamReader.ReadBytes((int)streamReader.BaseStream.Length);
return WithResourceMapping(resourceContent, target.ToString(), uid, gid, fileMode);
return WithResourceMapping(resourceContent, target, uid, gid, fileMode);
}
}
}

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

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(Uri source, DirectoryInfo target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(source, DirectoryPath.Of(target.ToString()), uid, gid, fileMode);
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(Uri source, DirectoryPath target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
var fileName = Path.GetFileName(source.LocalPath);
var filePath = FilePath.Of(Path.Combine(target.Value, fileName));
return WithResourceMapping(source, filePath, uid, gid, fileMode);
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(Uri source, FileInfo target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(source, FilePath.Of(target.ToString()), uid, gid, fileMode);
}

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(Uri source, FilePath target, uint uid = 0, uint gid = 0, UnixFileModes fileMode = Unix.FileMode644)
{
if (source.IsFile)
{
return WithResourceMapping(new FileResourceMapping(source.AbsolutePath, target, uid, gid, fileMode));
return WithResourceMapping(FilePath.Of(source.LocalPath), target, uid, gid, fileMode);
}
else
{
return WithResourceMapping(new UriResourceMapping(source, target, uid, gid, fileMode));
return WithResourceMapping(new UriResourceMapping(source, target.Value, uid, gid, fileMode));
}
}

Expand Down
55 changes: 55 additions & 0 deletions src/Testcontainers/Builders/DirectoryPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace DotNet.Testcontainers.Builders
{
using System;
using DotNet.Testcontainers.Configurations;
using JetBrains.Annotations;

/// <summary>
/// Represents a container or host directory path.
/// </summary>
[PublicAPI]
public readonly record struct DirectoryPath
{
/// <summary>
/// Initializes a new instance of the <see cref="DirectoryPath" /> struct.
/// </summary>
/// <param name="value">The directory path value.</param>
private DirectoryPath(string value)
{
Value = value;
}

/// <summary>
/// Gets the normalized directory path value.
/// </summary>
[PublicAPI]
public string Value { get; }

/// <summary>
/// Creates a new <see cref="DirectoryPath" /> from the specified path.
/// </summary>
/// <param name="path">The directory path.</param>
/// <returns>The normalized <see cref="DirectoryPath" />.</returns>
[PublicAPI]
public static DirectoryPath Of(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}

if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException("The directory path cannot be empty.", nameof(path));
}

return new DirectoryPath(Unix.Instance.NormalizePath(path));
}

/// <inheritdoc />
public override string ToString()
{
return Value;
}
}
}
55 changes: 55 additions & 0 deletions src/Testcontainers/Builders/FilePath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace DotNet.Testcontainers.Builders
{
using System;
using DotNet.Testcontainers.Configurations;
using JetBrains.Annotations;

/// <summary>
/// Represents a container or host file path.
/// </summary>
[PublicAPI]
public readonly record struct FilePath
{
/// <summary>
/// Initializes a new instance of the <see cref="FilePath" /> struct.
/// </summary>
/// <param name="value">The file path value.</param>
private FilePath(string value)
{
Value = value;
}

/// <summary>
/// Gets the normalized file path value.
/// </summary>
[PublicAPI]
public string Value { get; }

/// <summary>
/// Creates a new <see cref="FilePath" /> from the specified path.
/// </summary>
/// <param name="path">The file path.</param>
/// <returns>The normalized <see cref="FilePath" />.</returns>
[PublicAPI]
public static FilePath Of(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}

if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentException("The file path cannot be empty.", nameof(path));
}

return new FilePath(Unix.Instance.NormalizePath(path));
}

/// <inheritdoc />
public override string ToString()
{
return Value;
}
}
}
Loading
Loading