Skip to content

Split the test output to its own stream in single-file mode #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public PInvokeGenerator(PInvokeGeneratorConfiguration config, Func<string, Strea
public void Close()
{
Stream? stream = null;
Stream? testStream = null;

var methodClassOutputBuilders = new Dictionary<string, IOutputBuilder>();
var methodClassTestOutputBuilders = new Dictionary<string, IOutputBuilder>();
Expand Down Expand Up @@ -237,11 +238,16 @@ public void Close()
{
var outputPath = _config.OutputLocation;
stream = _outputStreamFactory(outputPath);

var testOutputPath = _config.TestOutputLocation;
testStream = _outputStreamFactory(testOutputPath);

leaveStreamOpen = true;

var usingDirectives = new SortedSet<string>(StringComparer.Ordinal);
var staticUsingDirectives = new SortedSet<string>(StringComparer.Ordinal);
var hasAnyContents = false;
var testHasAnyContents = false;

foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders)
{
Expand All @@ -257,11 +263,19 @@ public void Close()
_ = staticUsingDirectives.Add(staticUsingDirective);
}

hasAnyContents = csharpOutputBuilder.Contents.Any();
if (csharpOutputBuilder.IsTestOutput)
{
testHasAnyContents |= csharpOutputBuilder.Contents.Any();
}
else
{
hasAnyContents |= csharpOutputBuilder.Contents.Any();
}
}
else if (outputBuilder is XmlOutputBuilder xmlOutputBuilder)
{
hasAnyContents = xmlOutputBuilder.Contents.Any();
Debug.Assert(!xmlOutputBuilder.IsTestOutput);
hasAnyContents |= xmlOutputBuilder.Contents.Any();
}
}

Expand Down Expand Up @@ -316,6 +330,36 @@ public void Close()
}
}
}

if (testHasAnyContents)
{
using var sw = new StreamWriter(testStream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
sw.NewLine = "\n";

if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
{
if (!string.IsNullOrEmpty(_config.HeaderText))
{
sw.WriteLine(_config.HeaderText);
}

if (usingDirectives.Count != 0)
{
foreach (var usingDirective in usingDirectives)
{
sw.Write("using ");
sw.Write(usingDirective);
sw.WriteLine(';');
}

sw.WriteLine();
}
}
}
else
{
testStream = null;
}
}

foreach (var outputBuilder in _outputBuilderFactory.OutputBuilders)
Expand Down Expand Up @@ -359,11 +403,18 @@ public void Close()

Debug.Assert(stream is not null);
CloseOutputBuilder(stream, outputBuilder, isMethodClass, leaveStreamOpen, emitNamespaceDeclaration);

if (testStream is not null)
{
CloseOutputBuilder(testStream, outputBuilder, isMethodClass, leaveStreamOpen, emitNamespaceDeclaration);
}

emitNamespaceDeclaration = false;

if (_config.GenerateMultipleFiles)
{
stream = null;
Debug.Assert(testStream is null);
}
}

Expand Down Expand Up @@ -400,7 +451,7 @@ public void Close()

foreach (var entry in methodClassTestOutputBuilders)
{
CloseOutputBuilder(stream, entry.Value, isMethodClass: true, leaveStreamOpen, emitNamespaceDeclaration);
CloseOutputBuilder(testStream ?? stream, entry.Value, isMethodClass: true, leaveStreamOpen, emitNamespaceDeclaration);
}

using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
Expand All @@ -415,6 +466,17 @@ public void Close()
sw.WriteLine(" </namespace>");
sw.WriteLine("</bindings>");
}

if (testStream is not null)
{
using var tsw = new StreamWriter(testStream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen);
tsw.NewLine = "\n";

if (_config.OutputMode == PInvokeGeneratorOutputMode.CSharp)
{
tsw.WriteLine('}');
}
}
}

_context.Clear();
Expand Down