From 61733f7400a3c4eb691c6c49e600736fa38c4acf Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Thu, 26 Sep 2024 10:07:49 +0000 Subject: [PATCH] WithBindMount --- .../WireMockContainerBuilder.cs | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs b/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs index 6befac3d..d5962995 100644 --- a/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs +++ b/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs @@ -40,6 +40,7 @@ public sealed class WireMockContainerBuilder : ContainerBuilder /// Initializes a new instance of the class. @@ -142,13 +143,9 @@ public WireMockContainerBuilder WithWatchStaticMappings(bool includeSubDirectori [PublicAPI] public WireMockContainerBuilder WithMappings(string path, bool includeSubDirectories = false) { - Guard.NotNullOrEmpty(path); + _staticMappingsPath = Guard.NotNullOrEmpty(path); - _imageOS ??= _getOSAsLazy.Value.GetAwaiter().GetResult(); - - return WithReadStaticMappings() - .WithCommand($"--WatchStaticMappingsInSubdirectories {includeSubDirectories}") - .WithBindMount(path, _info[_imageOS.Value].MappingsPath); + return WithReadStaticMappings().WithCommand($"--WatchStaticMappingsInSubdirectories {includeSubDirectories}"); } private WireMockContainerBuilder(WireMockConfiguration dockerResourceConfiguration) : base(dockerResourceConfiguration) @@ -178,11 +175,36 @@ protected override WireMockContainerBuilder Init() builder = builder.WithImage(); } - var waitForContainerOS = _imageOS is null || _imageOS == OSPlatform.Windows ? Wait.ForWindowsContainer() : Wait.ForUnixContainer(); - return builder + // In case the _imageOS is not set, determine it from the Image FullName. + if (_imageOS == null) + { + if (builder.DockerResourceConfiguration.Image.FullName.IndexOf("wiremock.net", StringComparison.OrdinalIgnoreCase) < 0) + { + throw new InvalidOperationException(); + } + + if (builder.DockerResourceConfiguration.Image.FullName.IndexOf("windows", StringComparison.OrdinalIgnoreCase) >= 0) + { + _imageOS = OSPlatform.Windows; + } + else + { + _imageOS = OSPlatform.Linux; + } + } + + var waitForContainerOS = _imageOS == OSPlatform.Windows ? Wait.ForWindowsContainer() : Wait.ForUnixContainer(); + builder .WithPortBinding(WireMockContainer.ContainerPort, true) .WithCommand($"--WireMockLogger {DefaultLogger}") .WithWaitStrategy(waitForContainerOS.UntilMessageIsLogged("By Stef Heyenrath")); + + if (!string.IsNullOrEmpty(_staticMappingsPath)) + { + builder = builder.WithBindMount(_staticMappingsPath, _info[_imageOS.Value].MappingsPath); + } + + return builder; } ///