Skip to content

Commit 1918c78

Browse files
committed
Split the test output to its own stream in single-file mode
1 parent 124535b commit 1918c78

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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,32 @@ 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+
}
319359
}
320360

321361
foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders)
@@ -359,11 +399,18 @@ public void Close()
359399

360400
Debug.Assert(stream is not null);
361401
CloseOutputBuilder(stream, outputBuilder, isMethodClass, leaveStreamOpen, emitNamespaceDeclaration);
402+
403+
if (testStream is not null)
404+
{
405+
CloseOutputBuilder(testStream, outputBuilder, isMethodClass, leaveStreamOpen, emitNamespaceDeclaration);
406+
}
407+
362408
emitNamespaceDeclaration = false;
363409

364410
if (_config.GenerateMultipleFiles)
365411
{
366412
stream = null;
413+
Debug.Assert(testStream is null);
367414
}
368415
}
369416

@@ -400,7 +447,7 @@ public void Close()
400447

401448
foreach (var entry in methodClassTestOutputBuilders)
402449
{
403-
CloseOutputBuilder(stream, entry.Value, isMethodClass: true, leaveStreamOpen, emitNamespaceDeclaration);
450+
CloseOutputBuilder(testStream ?? stream, entry.Value, isMethodClass: true, leaveStreamOpen, emitNamespaceDeclaration);
404451
}
405452

406453
using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
@@ -415,6 +462,17 @@ public void Close()
415462
sw.WriteLine(" </namespace>");
416463
sw.WriteLine("</bindings>");
417464
}
465+
466+
if (testStream is not null)
467+
{
468+
using var tsw = new StreamWriter(testStream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
469+
tsw.NewLine = "\n";
470+
471+
if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
472+
{
473+
tsw.WriteLine('}');
474+
}
475+
}
418476
}
419477

420478
_context.Clear();

0 commit comments

Comments
 (0)