@@ -210,6 +210,7 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func<string, Strea
210210 public void Close ( )
211211 {
212212 Stream ? stream = null ;
213+ Stream ? testStream = null ;
213214
214215 var methodClassOutputBuilders = new Dictionary < string , IOutputBuilder > ( ) ;
215216 var methodClassTestOutputBuilders = new Dictionary < string , IOutputBuilder > ( ) ;
@@ -237,11 +238,16 @@ public void Close()
237238 {
238239 var outputPath = _config . OutputLocation ;
239240 stream = _outputStreamFactory ( outputPath ) ;
241+
242+ var testOutputPath = _config . TestOutputLocation ;
243+ testStream = _outputStreamFactory ( testOutputPath ) ;
244+
240245 leaveStreamOpen = true ;
241246
242247 var usingDirectives = new SortedSet < string > ( StringComparer . Ordinal ) ;
243248 var staticUsingDirectives = new SortedSet < string > ( StringComparer . Ordinal ) ;
244249 var hasAnyContents = false ;
250+ var testHasAnyContents = false ;
245251
246252 foreach ( var outputBuilder in _outputBuilderFactory . OutputBuilders )
247253 {
@@ -257,11 +263,19 @@ public void Close()
257263 _ = staticUsingDirectives . Add ( staticUsingDirective ) ;
258264 }
259265
260- hasAnyContents = csharpOutputBuilder . Contents . Any ( ) ;
266+ if ( csharpOutputBuilder . IsTestOutput )
267+ {
268+ testHasAnyContents |= csharpOutputBuilder . Contents . Any ( ) ;
269+ }
270+ else
271+ {
272+ hasAnyContents |= csharpOutputBuilder . Contents . Any ( ) ;
273+ }
261274 }
262275 else if ( outputBuilder is XmlOutputBuilder xmlOutputBuilder )
263276 {
264- hasAnyContents = xmlOutputBuilder . Contents . Any ( ) ;
277+ Debug . Assert ( ! xmlOutputBuilder . IsTestOutput ) ;
278+ hasAnyContents |= xmlOutputBuilder . Contents . Any ( ) ;
265279 }
266280 }
267281
@@ -316,6 +330,36 @@ public void Close()
316330 }
317331 }
318332 }
333+
334+ if ( testHasAnyContents )
335+ {
336+ using var sw = new StreamWriter ( testStream , s_defaultStreamWriterEncoding , DefaultStreamWriterBufferSize , leaveStreamOpen ) ;
337+ sw . NewLine = "\n " ;
338+
339+ if ( _config . OutputMode == PInvokeGeneratorOutputMode . CSharp )
340+ {
341+ if ( ! string . IsNullOrEmpty ( _config . HeaderText ) )
342+ {
343+ sw . WriteLine ( _config . HeaderText ) ;
344+ }
345+
346+ if ( usingDirectives . Count != 0 )
347+ {
348+ foreach ( var usingDirective in usingDirectives )
349+ {
350+ sw . Write ( "using " ) ;
351+ sw . Write ( usingDirective ) ;
352+ sw . WriteLine ( ';' ) ;
353+ }
354+
355+ sw . WriteLine ( ) ;
356+ }
357+ }
358+ }
359+ else
360+ {
361+ testStream = null ;
362+ }
319363 }
320364
321365 foreach ( var outputBuilder in _outputBuilderFactory . OutputBuilders )
@@ -359,11 +403,18 @@ public void Close()
359403
360404 Debug . Assert ( stream is not null ) ;
361405 CloseOutputBuilder ( stream , outputBuilder , isMethodClass , leaveStreamOpen , emitNamespaceDeclaration ) ;
406+
407+ if ( testStream is not null )
408+ {
409+ CloseOutputBuilder ( testStream , outputBuilder , isMethodClass , leaveStreamOpen , emitNamespaceDeclaration ) ;
410+ }
411+
362412 emitNamespaceDeclaration = false ;
363413
364414 if ( _config . GenerateMultipleFiles )
365415 {
366416 stream = null ;
417+ Debug . Assert ( testStream is null ) ;
367418 }
368419 }
369420
@@ -400,7 +451,7 @@ public void Close()
400451
401452 foreach ( var entry in methodClassTestOutputBuilders )
402453 {
403- CloseOutputBuilder ( stream , entry . Value , isMethodClass : true , leaveStreamOpen , emitNamespaceDeclaration ) ;
454+ CloseOutputBuilder ( testStream ?? stream , entry . Value , isMethodClass : true , leaveStreamOpen , emitNamespaceDeclaration ) ;
404455 }
405456
406457 using var sw = new StreamWriter ( stream , s_defaultStreamWriterEncoding , DefaultStreamWriterBufferSize , leaveStreamOpen ) ;
@@ -415,6 +466,17 @@ public void Close()
415466 sw . WriteLine ( " </namespace>" ) ;
416467 sw . WriteLine ( "</bindings>" ) ;
417468 }
469+
470+ if ( testStream is not null )
471+ {
472+ using var tsw = new StreamWriter ( testStream , s_defaultStreamWriterEncoding , DefaultStreamWriterBufferSize , leaveStreamOpen ) ;
473+ tsw . NewLine = "\n " ;
474+
475+ if ( _config . OutputMode == PInvokeGeneratorOutputMode . CSharp )
476+ {
477+ tsw . WriteLine ( '}' ) ;
478+ }
479+ }
418480 }
419481
420482 _context . Clear ( ) ;
0 commit comments