22using System . Runtime . CompilerServices ;
33using System . Text ;
44using Microsoft . CodeAnalysis ;
5- using ChilliCream . Testing ;
65using HotChocolate ;
76using HotChocolate . Language ;
8- using Snapshooter ;
9- using Snapshooter . Xunit ;
107using StrawberryShake . CodeGeneration . Analyzers ;
118using StrawberryShake . CodeGeneration . Analyzers . Models ;
129using StrawberryShake . CodeGeneration . Utilities ;
13- using Snapshot = Snapshooter . Xunit . Snapshot ;
1410using RequestStrategyGen = StrawberryShake . Tools . Configuration . RequestStrategy ;
1511using static StrawberryShake . CodeGeneration . CSharp . CSharpGenerator ;
1612
@@ -140,11 +136,7 @@ public static void AssertResult(
140136
141137 if ( settings . SnapshotFile is not null )
142138 {
143- documents . ToString ( )
144- . MatchSnapshot (
145- new SnapshotFullName (
146- settings . SnapshotFile ,
147- Snapshot . FullName ( ) . FolderPath ) ) ;
139+ MatchSnapshotAtPath ( documents . ToString ( ) , settings . SnapshotFile ) ;
148140 }
149141 else
150142 {
@@ -200,12 +192,17 @@ public static AssertSettings CreateIntegrationTest(
200192 TransportProfile [ ] ? profiles = null ,
201193 AccessModifier accessModifier = AccessModifier . Public ,
202194 bool noStore = false ,
203- [ CallerMemberName ] string ? testName = null )
195+ [ CallerMemberName ] string ? testName = null ,
196+ [ CallerFilePath ] string ? callerFilePath = null )
204197 {
205- var snapshotFullName = Snapshot . FullName ( ) ;
206- var testFile = System . IO . Path . Combine (
207- snapshotFullName . FolderPath ,
208- testName + "Test.cs" ) ;
198+ ArgumentException . ThrowIfNullOrEmpty ( testName ) ;
199+ ArgumentException . ThrowIfNullOrEmpty ( callerFilePath ) ;
200+
201+ var folder = System . IO . Path . GetDirectoryName ( callerFilePath )
202+ ?? throw new ArgumentException (
203+ $ "Could not determine directory from caller file path '{ callerFilePath } '.",
204+ nameof ( callerFilePath ) ) ;
205+ var testFile = System . IO . Path . Combine ( folder , testName + "Test.cs" ) ;
209206 var ns = "StrawberryShake.CodeGeneration.CSharp.Integration." + testName ;
210207
211208 if ( ! File . Exists ( testFile ) )
@@ -219,13 +216,11 @@ public static AssertSettings CreateIntegrationTest(
219216
220217 return new AssertSettings
221218 {
222- ClientName = testName ! + "Client" ,
219+ ClientName = testName + "Client" ,
223220 Namespace = ns ,
224221 AccessModifier = accessModifier ,
225222 StrictValidation = true ,
226- SnapshotFile = System . IO . Path . Combine (
227- snapshotFullName . FolderPath ,
228- testName + "Test.Client.cs" ) ,
223+ SnapshotFile = System . IO . Path . Combine ( folder , testName + "Test.Client.cs" ) ,
229224 RequestStrategy = requestStrategy ,
230225 NoStore = noStore ,
231226 Profiles = ( profiles ??
@@ -235,6 +230,46 @@ public static AssertSettings CreateIntegrationTest(
235230 } ;
236231 }
237232
233+ private static void MatchSnapshotAtPath ( string content , string snapshotFile )
234+ {
235+ content = content . Replace ( "\r \n " , "\n " ) ;
236+
237+ if ( ! File . Exists ( snapshotFile ) )
238+ {
239+ CheckStrictMode ( ) ;
240+ File . WriteAllText ( snapshotFile , content ) ;
241+ return ;
242+ }
243+
244+ var existing = File . ReadAllText ( snapshotFile ) . Replace ( "\r \n " , "\n " ) ;
245+ if ( string . Equals ( existing , content , StringComparison . Ordinal ) )
246+ {
247+ return ;
248+ }
249+
250+ var folder = System . IO . Path . GetDirectoryName ( snapshotFile ) ! ;
251+ var mismatchDir = System . IO . Path . Combine ( folder , "__snapshots__" , "__mismatch__" ) ;
252+ Directory . CreateDirectory ( mismatchDir ) ;
253+ var mismatchFile = System . IO . Path . Combine ( mismatchDir , System . IO . Path . GetFileName ( snapshotFile ) ) ;
254+ File . WriteAllText ( mismatchFile , content ) ;
255+
256+ Assert . Fail ( $ "Snapshot mismatch. Mismatch file written to { mismatchFile } ") ;
257+ }
258+
259+ private static void CheckStrictMode ( )
260+ {
261+ var value = Environment . GetEnvironmentVariable ( "COOKIE_CRUMBLE_STRICT_MODE" ) ;
262+
263+ if ( string . Equals ( value , "on" , StringComparison . Ordinal )
264+ || ( bool . TryParse ( value , out var b ) && b ) )
265+ {
266+ Assert . Fail (
267+ "Strict mode is enabled and no snapshot has been found "
268+ + "for the current test. Create a new snapshot locally and "
269+ + "rerun your tests." ) ;
270+ }
271+ }
272+
238273 private static ClientModel CreateClientModel (
239274 string [ ] sourceText ,
240275 bool strictValidation ,
0 commit comments