Skip to content

Commit 87b7505

Browse files
authored
fix: InvalidOperationException when adding future volume (#817)
1 parent 9a5531f commit 87b7505

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Testcontainers/Configurations/Volumes/VolumeMount.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace DotNet.Testcontainers.Configurations
55
/// <inheritdoc cref="IMount" />
66
internal readonly struct VolumeMount : IMount
77
{
8+
// Do not set the volume name immediately in the constructor. This may raise an InvalidOperationException too early.
9+
// Depending on when the developer passes the instance to VolumeMount it may not exist yet.
10+
private readonly IVolume volume;
11+
812
/// <summary>
913
/// Initializes a new instance of the <see cref="VolumeMount" /> struct.
1014
/// </summary>
@@ -13,8 +17,8 @@ namespace DotNet.Testcontainers.Configurations
1317
/// <param name="accessMode">The Docker volume access mode.</param>
1418
public VolumeMount(IVolume volume, string containerPath, AccessMode accessMode)
1519
{
20+
this.volume = volume;
1621
this.Type = MountType.Volume;
17-
this.Source = volume.Name;
1822
this.Target = containerPath;
1923
this.AccessMode = accessMode;
2024
}
@@ -26,7 +30,7 @@ public VolumeMount(IVolume volume, string containerPath, AccessMode accessMode)
2630
public AccessMode AccessMode { get; }
2731

2832
/// <inheritdoc />
29-
public string Source { get; }
33+
public string Source => this.volume.Name;
3034

3135
/// <inheritdoc />
3236
public string Target { get; }

0 commit comments

Comments
 (0)