@@ -104,37 +104,76 @@ public sealed class EqualTest
104104 {
105105 [ Fact ]
106106 [ Trait ( nameof ( DockerCli . DockerPlatform ) , nameof ( DockerCli . DockerPlatform . Linux ) ) ]
107- public void ForSameConfigurationCreatedInDifferentOrder ( )
107+ public void ForKnownConfiguration ( )
108108 {
109- var env1 = new Dictionary < string , string >
110- {
111- [ "keyA" ] = "valueA" ,
112- [ "keyB" ] = "valueB" ,
113- } ;
114- var env2 = new Dictionary < string , string >
115- {
116- [ "keyB" ] = "valueB" ,
117- [ "keyA" ] = "valueA" ,
118- } ;
119- var hash1 = new ReuseHashContainerBuilder ( ) . WithEnvironment ( env1 ) . WithLabel ( "labelA" , "A" ) . WithLabel ( "labelB" , "B" ) . GetReuseHash ( ) ;
120- var hash2 = new ReuseHashContainerBuilder ( ) . WithEnvironment ( env2 ) . WithLabel ( "labelB" , "B" ) . WithLabel ( "labelA" , "A" ) . GetReuseHash ( ) ;
121- Assert . Equal ( hash1 , hash2 ) ;
109+ // Given
110+ var env = new Dictionary < string , string > ( ) ;
111+ env [ "keyA" ] = "valueA" ;
112+ env [ "keyB" ] = "valueB" ;
113+
114+ // When
115+ var hash = new ReuseHashContainerBuilder ( )
116+ . WithEnvironment ( env )
117+ . WithLabel ( "labelA" , "A" )
118+ . WithLabel ( "labelB" , "B" )
119+ . GetReuseHash ( ) ;
120+
121+ // Then
122+
123+ // The hash is calculated from the minified JSON. For readability, the JSON
124+ // shown below is formatted. `Dtj7Jx6NVlbDUnA3vmH1nNZw+o8=` is the
125+ // Base64-encoded SHA-1 hash for this JSON (minified):
126+ //
127+ // {
128+ // "Image": null,
129+ // "Name": null,
130+ // "Entrypoint": null,
131+ // "Command": [],
132+ // "Environments": {
133+ // "keyA": "valueA",
134+ // "keyB": "valueB"
135+ // },
136+ // "ExposedPorts": {},
137+ // "PortBindings": {},
138+ // "NetworkAliases": [],
139+ // "Labels": {
140+ // "labelA": "A",
141+ // "labelB": "B",
142+ // "org.testcontainers": "true",
143+ // "org.testcontainers.lang": "dotnet"
144+ // }
145+ // }
146+ Assert . Equal ( "Dtj7Jx6NVlbDUnA3vmH1nNZw+o8=" , hash ) ;
122147 }
123148
124149 [ Fact ]
125150 [ Trait ( nameof ( DockerCli . DockerPlatform ) , nameof ( DockerCli . DockerPlatform . Linux ) ) ]
126- public void ForGivenConfiguration ( )
151+ public void ForSameConfigurationInDifferentOrder ( )
127152 {
128- var env = new Dictionary < string , string >
129- {
130- [ "keyB" ] = "valueB" ,
131- [ "keyA" ] = "valueA" ,
132- } ;
133- var hash = new ReuseHashContainerBuilder ( ) . WithEnvironment ( env ) . WithLabel ( "labelB" , "B" ) . WithLabel ( "labelA" , "A" ) . GetReuseHash ( ) ;
134-
135- // 50MEP+vnxEkQFo5PrndJ7oKOfh8= is the base64 encoded SHA1 of this JSON:
136- // {"Image":null,"Name":null,"Entrypoint":null,"Command":[],"Environments":{"keyA":"valueA","keyB":"valueB"},"ExposedPorts":{},"PortBindings":{},"NetworkAliases":[],"ExtraHosts":[],"Labels":{"labelA":"A","labelB":"B","org.testcontainers":"true","org.testcontainers.lang":"dotnet"}}
137- Assert . Equal ( "50MEP+vnxEkQFo5PrndJ7oKOfh8=" , hash ) ;
153+ // Given
154+ var env1 = new Dictionary < string , string > ( ) ;
155+ env1 [ "keyA" ] = "valueA" ;
156+ env1 [ "keyB" ] = "valueB" ;
157+
158+ var env2 = new Dictionary < string , string > ( ) ;
159+ env2 [ "keyB" ] = "valueB" ;
160+ env2 [ "keyA" ] = "valueA" ;
161+
162+ // When
163+ var hash1 = new ReuseHashContainerBuilder ( )
164+ . WithEnvironment ( env1 )
165+ . WithLabel ( "labelA" , "A" )
166+ . WithLabel ( "labelB" , "B" )
167+ . GetReuseHash ( ) ;
168+
169+ var hash2 = new ReuseHashContainerBuilder ( )
170+ . WithEnvironment ( env2 )
171+ . WithLabel ( "labelB" , "B" )
172+ . WithLabel ( "labelA" , "A" )
173+ . GetReuseHash ( ) ;
174+
175+ // Then
176+ Assert . Equal ( hash1 , hash2 ) ;
138177 }
139178 }
140179
@@ -226,7 +265,12 @@ public void EnabledCleanUpThrowsException()
226265 private sealed class ReuseHashContainerBuilder : ContainerBuilder < ReuseHashContainerBuilder , DockerContainer , ContainerConfiguration >
227266 {
228267 public ReuseHashContainerBuilder ( ) : this ( new ContainerConfiguration ( ) )
229- => DockerResourceConfiguration = Init ( ) . DockerResourceConfiguration ;
268+ {
269+ // By default, the constructor calls `Init()`, which sets up the default builder
270+ // configurations, including ones for the port forwarding container if it's running.
271+ // To avoid applying those settings during tests, this class intentionally doesn't
272+ // call `Init()`.
273+ }
230274
231275 private ReuseHashContainerBuilder ( ContainerConfiguration configuration ) : base ( configuration )
232276 => DockerResourceConfiguration = configuration ;
0 commit comments