Skip to content

Commit

Permalink
chore: Set WithResourceMapping(string, string) obsolete and explain t…
Browse files Browse the repository at this point in the history
…he new behavior (#934)
  • Loading branch information
HofmeisterAn authored Jun 27, 2023
1 parent 705290b commit 69d91ed
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Testcontainers.WebDriver/WebDriverBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public WebDriverBuilder WithBrowser(WebDriverBrowser webDriverBrowser)
/// <returns>A configured instance of <see cref="WebDriverBuilder" />.</returns>
public WebDriverBuilder WithConfigurationFromTomlFile(string configTomlFilePath)
{
return WithResourceMapping(File.ReadAllBytes(configTomlFilePath), "/opt/bin/config.toml");
return WithResourceMapping(new FileInfo(configTomlFilePath), new FileInfo("/opt/bin/config.toml"));
}

/// <summary>
Expand Down
12 changes: 7 additions & 5 deletions src/Testcontainers/Builders/ContainerBuilder`3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,33 @@ public TBuilderEntity WithResourceMapping(byte[] resourceContent, string filePat
}

/// <inheritdoc />
[Obsolete("The next release will change how this member behaves. The target argument, which used to be a file path, will be a directory path where the file will be copied to, similar to WithResourceMapping(DirectoryInfo, string) and WithResourceMapping(FileInfo, string).\nTo retain the old behavior, use WithResourceMapping(FileInfo, FileInfo) instead.")]
public TBuilderEntity WithResourceMapping(string source, string target, UnixFileModes fileMode = Unix.FileMode644)
{
return WithResourceMapping(new FileResourceMapping(source, target, fileMode));
return WithResourceMapping(new FileInfo(source), new FileInfo(target), fileMode);
}

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

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

/// <inheritdoc />
public TBuilderEntity WithResourceMapping(FileInfo source, FileInfo target, UnixFileModes fileMode = Unix.FileMode644)
{
using (var fileStream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var fileStream = source.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var streamReader = new BinaryReader(fileStream))
{
var resourceContent = streamReader.ReadBytes((int)streamReader.BaseStream.Length);
return WithResourceMapping(new BinaryResourceMapping(resourceContent, target.ToString(), fileMode));
return WithResourceMapping(resourceContent, target.ToString(), fileMode);
}
}
}
Expand Down Expand Up @@ -327,6 +328,7 @@ public TBuilderEntity WithPrivileged(bool privileged)
}

/// <inheritdoc />
[Obsolete("It is no longer necessary to assign an output consumer to read the container's log messages.\nUse IContainer.GetLogsAsync(DateTime, DateTime, bool, CancellationToken) instead.")]
public TBuilderEntity WithOutputConsumer(IOutputConsumer outputConsumer)
{
return Clone(new ContainerConfiguration(outputConsumer: outputConsumer));
Expand Down
1 change: 0 additions & 1 deletion src/Testcontainers/Builders/IContainerBuilder`2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
/// <param name="outputConsumer">The output consumer.</param>
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
[PublicAPI]
[Obsolete("It is no longer necessary to assign an output consumer to read the container's log messages.\nUse IContainer.GetLogsAsync(DateTime, DateTime, bool, CancellationToken) instead.")]
TBuilderEntity WithOutputConsumer(IOutputConsumer outputConsumer);

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Testcontainers/Containers/TarOutputMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ await PutNextEntryAsync(tarEntry, ct)
.ConfigureAwait(false);

#if NETSTANDARD2_1_OR_GREATER
await WriteAsync(fileContent, ct)
.ConfigureAwait(false);
await WriteAsync(fileContent, ct)
.ConfigureAwait(false);
#else
await WriteAsync(fileContent, 0, fileContent.Length, ct)
.ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class TarOutputMemoryStreamTest

protected TarOutputMemoryStreamTest()
{
using var fileStream = _testFile.Create();
using var fileStream = _testFile.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
fileStream.WriteByte(13);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class CopyResourceMappingContainerTest : IAsyncLifetime, IDisposab
{
private const string ResourceMappingContent = "👋";

private readonly FileInfo _sourceFilePath = new FileInfo(Path.Combine(TestSession.TempDirectoryPath, Path.GetRandomFileName()));
private readonly FileInfo _testFile = new FileInfo(Path.Combine(TestSession.TempDirectoryPath, Path.GetRandomFileName()));

private readonly string _bytesTargetFilePath;

Expand All @@ -27,17 +27,17 @@ public CopyResourceMappingContainerTest()
{
var resourceContent = Encoding.Default.GetBytes(ResourceMappingContent);

using var fileStream = _sourceFilePath.Create();
using var fileStream = _testFile.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
fileStream.Write(resourceContent);

_bytesTargetFilePath = string.Join("/", string.Empty, "tmp", Guid.NewGuid(), _sourceFilePath.Name);
_bytesTargetFilePath = string.Join("/", string.Empty, "tmp", Guid.NewGuid(), _testFile.Name);

_fileTargetFilePath = string.Join("/", string.Empty, "tmp", Guid.NewGuid());

_container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithResourceMapping(resourceContent, _bytesTargetFilePath)
.WithResourceMapping(_sourceFilePath, _fileTargetFilePath)
.WithResourceMapping(_testFile, _fileTargetFilePath)
.Build();
}

Expand All @@ -53,7 +53,7 @@ public Task DisposeAsync()

public void Dispose()
{
_sourceFilePath.Delete();
_testFile.Delete();
}

[Fact]
Expand All @@ -62,7 +62,7 @@ public async Task ReadExistingFile()
// Given
IList<string> targetFilePaths = new List<string>();
targetFilePaths.Add(_bytesTargetFilePath);
targetFilePaths.Add(string.Join("/", _fileTargetFilePath, _sourceFilePath.Name));
targetFilePaths.Add(string.Join("/", _fileTargetFilePath, _testFile.Name));

// When
var resourceContents = await Task.WhenAll(targetFilePaths.Select(containerFilePath => _container.ReadFileAsync(containerFilePath)))
Expand Down

0 comments on commit 69d91ed

Please sign in to comment.