Skip to content

Commit 61733f7

Browse files
committed
WithBindMount
1 parent 82ee25a commit 61733f7

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public sealed class WireMockContainerBuilder : ContainerBuilder<WireMockContaine
4040
});
4141

4242
private OSPlatform? _imageOS;
43+
private string? _staticMappingsPath;
4344

4445
/// <summary>
4546
/// Initializes a new instance of the <see cref="ContainerBuilder" /> class.
@@ -142,13 +143,9 @@ public WireMockContainerBuilder WithWatchStaticMappings(bool includeSubDirectori
142143
[PublicAPI]
143144
public WireMockContainerBuilder WithMappings(string path, bool includeSubDirectories = false)
144145
{
145-
Guard.NotNullOrEmpty(path);
146+
_staticMappingsPath = Guard.NotNullOrEmpty(path);
146147

147-
_imageOS ??= _getOSAsLazy.Value.GetAwaiter().GetResult();
148-
149-
return WithReadStaticMappings()
150-
.WithCommand($"--WatchStaticMappingsInSubdirectories {includeSubDirectories}")
151-
.WithBindMount(path, _info[_imageOS.Value].MappingsPath);
148+
return WithReadStaticMappings().WithCommand($"--WatchStaticMappingsInSubdirectories {includeSubDirectories}");
152149
}
153150

154151
private WireMockContainerBuilder(WireMockConfiguration dockerResourceConfiguration) : base(dockerResourceConfiguration)
@@ -178,11 +175,36 @@ protected override WireMockContainerBuilder Init()
178175
builder = builder.WithImage();
179176
}
180177

181-
var waitForContainerOS = _imageOS is null || _imageOS == OSPlatform.Windows ? Wait.ForWindowsContainer() : Wait.ForUnixContainer();
182-
return builder
178+
// In case the _imageOS is not set, determine it from the Image FullName.
179+
if (_imageOS == null)
180+
{
181+
if (builder.DockerResourceConfiguration.Image.FullName.IndexOf("wiremock.net", StringComparison.OrdinalIgnoreCase) < 0)
182+
{
183+
throw new InvalidOperationException();
184+
}
185+
186+
if (builder.DockerResourceConfiguration.Image.FullName.IndexOf("windows", StringComparison.OrdinalIgnoreCase) >= 0)
187+
{
188+
_imageOS = OSPlatform.Windows;
189+
}
190+
else
191+
{
192+
_imageOS = OSPlatform.Linux;
193+
}
194+
}
195+
196+
var waitForContainerOS = _imageOS == OSPlatform.Windows ? Wait.ForWindowsContainer() : Wait.ForUnixContainer();
197+
builder
183198
.WithPortBinding(WireMockContainer.ContainerPort, true)
184199
.WithCommand($"--WireMockLogger {DefaultLogger}")
185200
.WithWaitStrategy(waitForContainerOS.UntilMessageIsLogged("By Stef Heyenrath"));
201+
202+
if (!string.IsNullOrEmpty(_staticMappingsPath))
203+
{
204+
builder = builder.WithBindMount(_staticMappingsPath, _info[_imageOS.Value].MappingsPath);
205+
}
206+
207+
return builder;
186208
}
187209

188210
/// <inheritdoc />

0 commit comments

Comments
 (0)