Skip to content

Commit 69d91ed

Browse files
authored
chore: Set WithResourceMapping(string, string) obsolete and explain the new behavior (#934)
1 parent 705290b commit 69d91ed

File tree

6 files changed

+17
-16
lines changed

6 files changed

+17
-16
lines changed

src/Testcontainers.WebDriver/WebDriverBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public WebDriverBuilder WithBrowser(WebDriverBrowser webDriverBrowser)
6464
/// <returns>A configured instance of <see cref="WebDriverBuilder" />.</returns>
6565
public WebDriverBuilder WithConfigurationFromTomlFile(string configTomlFilePath)
6666
{
67-
return WithResourceMapping(File.ReadAllBytes(configTomlFilePath), "/opt/bin/config.toml");
67+
return WithResourceMapping(new FileInfo(configTomlFilePath), new FileInfo("/opt/bin/config.toml"));
6868
}
6969

7070
/// <summary>

src/Testcontainers/Builders/ContainerBuilder`3.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -195,32 +195,33 @@ public TBuilderEntity WithResourceMapping(byte[] resourceContent, string filePat
195195
}
196196

197197
/// <inheritdoc />
198+
[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.")]
198199
public TBuilderEntity WithResourceMapping(string source, string target, UnixFileModes fileMode = Unix.FileMode644)
199200
{
200-
return WithResourceMapping(new FileResourceMapping(source, target, fileMode));
201+
return WithResourceMapping(new FileInfo(source), new FileInfo(target), fileMode);
201202
}
202203

203204
/// <inheritdoc />
204205
public TBuilderEntity WithResourceMapping(DirectoryInfo source, string target, UnixFileModes fileMode = Unix.FileMode644)
205206
{
206-
return WithResourceMapping(source.FullName, target, fileMode);
207+
return WithResourceMapping(new FileResourceMapping(source.FullName, target, fileMode));
207208
}
208209

209210
/// <inheritdoc />
210211
public TBuilderEntity WithResourceMapping(FileInfo source, string target, UnixFileModes fileMode = Unix.FileMode644)
211212
{
212-
return WithResourceMapping(source.FullName, target, fileMode);
213+
return WithResourceMapping(new FileResourceMapping(source.FullName, target, fileMode));
213214
}
214215

215216
/// <inheritdoc />
216217
public TBuilderEntity WithResourceMapping(FileInfo source, FileInfo target, UnixFileModes fileMode = Unix.FileMode644)
217218
{
218-
using (var fileStream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
219+
using (var fileStream = source.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
219220
{
220221
using (var streamReader = new BinaryReader(fileStream))
221222
{
222223
var resourceContent = streamReader.ReadBytes((int)streamReader.BaseStream.Length);
223-
return WithResourceMapping(new BinaryResourceMapping(resourceContent, target.ToString(), fileMode));
224+
return WithResourceMapping(resourceContent, target.ToString(), fileMode);
224225
}
225226
}
226227
}
@@ -327,6 +328,7 @@ public TBuilderEntity WithPrivileged(bool privileged)
327328
}
328329

329330
/// <inheritdoc />
331+
[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.")]
330332
public TBuilderEntity WithOutputConsumer(IOutputConsumer outputConsumer)
331333
{
332334
return Clone(new ContainerConfiguration(outputConsumer: outputConsumer));

src/Testcontainers/Builders/IContainerBuilder`2.cs

-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ public interface IContainerBuilder<out TBuilderEntity, out TContainerEntity> : I
406406
/// <param name="outputConsumer">The output consumer.</param>
407407
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns>
408408
[PublicAPI]
409-
[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.")]
410409
TBuilderEntity WithOutputConsumer(IOutputConsumer outputConsumer);
411410

412411
/// <summary>

src/Testcontainers/Containers/TarOutputMemoryStream.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ await PutNextEntryAsync(tarEntry, ct)
5656
.ConfigureAwait(false);
5757

5858
#if NETSTANDARD2_1_OR_GREATER
59-
await WriteAsync(fileContent, ct)
60-
.ConfigureAwait(false);
59+
await WriteAsync(fileContent, ct)
60+
.ConfigureAwait(false);
6161
#else
6262
await WriteAsync(fileContent, 0, fileContent.Length, ct)
6363
.ConfigureAwait(false);

tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public abstract class TarOutputMemoryStreamTest
1010

1111
protected TarOutputMemoryStreamTest()
1212
{
13-
using var fileStream = _testFile.Create();
13+
using var fileStream = _testFile.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
1414
fileStream.WriteByte(13);
1515
}
1616

tests/Testcontainers.Tests/Unit/Containers/Unix/CopyResourceMappingContainerTest.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class CopyResourceMappingContainerTest : IAsyncLifetime, IDisposab
1515
{
1616
private const string ResourceMappingContent = "👋";
1717

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

2020
private readonly string _bytesTargetFilePath;
2121

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

30-
using var fileStream = _sourceFilePath.Create();
30+
using var fileStream = _testFile.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
3131
fileStream.Write(resourceContent);
3232

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

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

3737
_container = new ContainerBuilder()
3838
.WithImage(CommonImages.Alpine)
3939
.WithResourceMapping(resourceContent, _bytesTargetFilePath)
40-
.WithResourceMapping(_sourceFilePath, _fileTargetFilePath)
40+
.WithResourceMapping(_testFile, _fileTargetFilePath)
4141
.Build();
4242
}
4343

@@ -53,7 +53,7 @@ public Task DisposeAsync()
5353

5454
public void Dispose()
5555
{
56-
_sourceFilePath.Delete();
56+
_testFile.Delete();
5757
}
5858

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

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

0 commit comments

Comments
 (0)