From bdee2d593d97578a5ce95620b4138c90c4b46f1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlastimil=20Kalu=C5=BEa?= Date: Sat, 5 Nov 2022 18:23:58 +0100 Subject: [PATCH] fix(#640) Fixed locked file in CopyResourceMappingContainerTest --- .../Unix/CopyResourceMappingContainerTest.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/Testcontainers.Tests/Unit/Containers/Unix/CopyResourceMappingContainerTest.cs b/tests/Testcontainers.Tests/Unit/Containers/Unix/CopyResourceMappingContainerTest.cs index 308a361cc..b9dc7cf60 100644 --- a/tests/Testcontainers.Tests/Unit/Containers/Unix/CopyResourceMappingContainerTest.cs +++ b/tests/Testcontainers.Tests/Unit/Containers/Unix/CopyResourceMappingContainerTest.cs @@ -14,7 +14,7 @@ public sealed class CopyResourceMappingContainerTest : IAsyncLifetime, IDisposab { private const string ResourceMappingContent = "👋"; - private readonly FileStream fileStream = new(Path.Combine(Path.GetTempPath(), Path.GetTempFileName()), FileMode.Create, FileAccess.Write, FileShare.Read, ResourceMappingContent.Length, FileOptions.DeleteOnClose); + private readonly string filePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); private readonly string fileResourceMappingFilePath = Path.Combine("/tmp", Path.GetTempFileName()); @@ -27,22 +27,30 @@ public CopyResourceMappingContainerTest() this.container = new TestcontainersBuilder() .WithImage("alpine") .WithEntrypoint(KeepTestcontainersUpAndRunning.Command) - .WithResourceMapping(this.fileStream.Name, this.fileResourceMappingFilePath) + .WithResourceMapping(this.filePath, this.fileResourceMappingFilePath) .WithResourceMapping(Encoding.Default.GetBytes(ResourceMappingContent), this.binaryResourceMappingFilePath) .Build(); } public Task InitializeAsync() { - this.fileStream.Write(Encoding.Default.GetBytes(ResourceMappingContent)); - this.fileStream.Flush(); + using (var fileStream = File.Create(this.filePath)) + { + fileStream.Write(Encoding.Default.GetBytes(ResourceMappingContent)); + fileStream.Flush(); + } + return this.container.StartAsync(); } - public Task DisposeAsync() + public async Task DisposeAsync() { - this.fileStream.Dispose(); - return this.container.DisposeAsync().AsTask(); + await this.container.DisposeAsync(); + + if (File.Exists(this.filePath)) + { + File.Delete(this.filePath); + } } public void Dispose()