Skip to content

Commit

Permalink
Use binlog reader in more places (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar authored Apr 28, 2024
1 parent 866649d commit 6426bc0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 39 deletions.
60 changes: 49 additions & 11 deletions src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public void ReplayWithArgs(string command, string arg, BasicAnalyzerKind? kind)
public void ReplayConsoleWithEmit(string arg)
{
using var emitDir = new TempDir();
Assert.Equal(Constants.ExitSuccess, RunCompLog($"replay {arg} -emit -o {emitDir.DirectoryPath} {Fixture.SolutionBinaryLogPath}"));
Assert.Equal(Constants.ExitSuccess, RunCompLog($"replay {arg} -o {emitDir.DirectoryPath} {Fixture.SolutionBinaryLogPath}"));

AssertOutput(@"console\emit\console.dll");
AssertOutput(@"console\emit\console.pdb");
Expand All @@ -487,6 +487,33 @@ void AssertOutput(string relativePath)
}
}

[Fact]
public void ReplayMissingOutput()
{
using var emitDir = new TempDir();
var (extiCode, output) = RunCompLogEx($"replay --out");
Assert.Equal(Constants.ExitFailure, extiCode);
Assert.Contains("Missing required value", output);
}

[Fact]
public void ReplayWithBadProject()
{
using var emitDir = new TempDir();
var (extiCode, output) = RunCompLogEx($"replay --severity Info --project console-with-diagnostics.csproj {Fixture.SolutionBinaryLogPath}");
Assert.Equal(Constants.ExitFailure, extiCode);
Assert.Contains("No compilations found", output);
}

[Fact]
public void ReplayWithDiagnostics()
{
using var emitDir = new TempDir();
var (exitCode, output) = RunCompLogEx($"replay --severity Info {Fixture.ConsoleWithDiagnosticsBinaryLogPath}");
Assert.Equal(Constants.ExitSuccess, exitCode);
Assert.Contains("CS0219", output);
}

[Fact]
public void ReplayHelp()
{
Expand Down Expand Up @@ -538,21 +565,32 @@ public void ReplayBadOption()
}

[Fact]
public void ReplayBadOptionCombination()
public void ReplayWithBothLogs()
{
var (exitCode, output) = RunCompLogEx($"replay -o example");
Assert.Equal(Constants.ExitFailure, exitCode);
Assert.StartsWith("Error: Specified a path", output);
RunWithBoth(void (string logFilePath) =>
{
var isBinlog = Path.GetExtension(logFilePath) == ".binlog";
AssertCompilerCallReader(void (ICompilerCallReader reader) =>
{
if (isBinlog)
{
Assert.IsType<BinaryLogReader>(reader);
}
else
{
Assert.IsType<CompilerLogReader>(reader);
}
});

RunCompLog($"replay {logFilePath}");
});
}

[Fact]
public void ReplayWithExport()
public void ReplayWithProject()
{
var (exitCode, output) = RunCompLogEx($"replay {Fixture.ConsoleWithDiagnosticsBinaryLogPath} -export -o {RootDirectory}");
Assert.Equal(Constants.ExitFailure, exitCode);
Assert.Contains("Exporting to", output);
Assert.True(File.Exists(Path.Combine(RootDirectory, "console-with-diagnostics", "export", "build.rsp")));
Assert.True(File.Exists(Path.Combine(RootDirectory, "console-with-diagnostics", "export", "ref", "netstandard.dll")));
AssertCompilerCallReader(void (ICompilerCallReader reader) => Assert.IsType<BinaryLogReader>(reader));
Assert.Equal(Constants.ExitSuccess, RunCompLog($"replay {Fixture.ConsoleProjectPath}"));
}

[Fact]
Expand Down
41 changes: 13 additions & 28 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Basic.CompilerLog;
using Basic.CompilerLog.Util;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Mono.Options;
using StructuredLogViewer;
using System.Diagnostics;
Expand Down Expand Up @@ -388,16 +389,12 @@ void PrintUsage()

int RunReplay(IEnumerable<string> args)
{
var baseOutputPath = "";
string? baseOutputPath = null;
var severity = DiagnosticSeverity.Warning;
var export = false;
var emit = false;
var options = new FilterOptionSet(analyzers: true)
{
{ "severity=", "minimum severity to display (default Warning)", (DiagnosticSeverity s) => severity = s },
{ "export", "export failed compilation", e => export = e is not null },
{ "emit", "emit the compilation(s) to disk", e => emit = e is not null },
{ "o|out=", "path to export to ", b => baseOutputPath = b },
{ "o|out=", "path to emit to ", void (string b) => baseOutputPath = b },
};

try
Expand All @@ -409,22 +406,20 @@ int RunReplay(IEnumerable<string> args)
return ExitSuccess;
}

if (!string.IsNullOrEmpty(baseOutputPath) && !(export || emit))
if (baseOutputPath is not null)
{
WriteLine("Error: Specified a path to export to but did not specify -export or -emit");
return ExitFailure;
baseOutputPath = GetBaseOutputPath(baseOutputPath);
WriteLine($"Outputting to {baseOutputPath}");
}

baseOutputPath = GetBaseOutputPath(baseOutputPath);
if (!string.IsNullOrEmpty(baseOutputPath))
using var reader = GetCompilerCallReader(extra, options.BasicAnalyzerKind, checkVersion: true);
var compilerCalls = reader.ReadAllCompilerCalls(options.FilterCompilerCalls);
if (compilerCalls.Count == 0)
{
WriteLine($"Outputting to {baseOutputPath}");
WriteLine("No compilations found");
return ExitFailure;
}

using var compilerLogStream = GetOrCreateCompilerLogStream(extra);
using var reader = GetCompilerLogReader(compilerLogStream, leaveOpen: true, options.BasicAnalyzerKind, checkVersion: true);
var compilerCalls = reader.ReadAllCompilerCalls(options.FilterCompilerCalls);
var exportUtil = new ExportUtil(reader, includeAnalyzers: options.IncludeAnalyzers);
var sdkDirs = SdkUtil.GetSdkDirectories();
var success = true;

Expand All @@ -438,7 +433,7 @@ int RunReplay(IEnumerable<string> args)
var compilation = compilationData.GetCompilationAfterGenerators();

IEmitResult emitResult;
if (emit)
if (baseOutputPath is not null)
{
var path = GetOutputPath(baseOutputPath, compilerCalls, i, "emit");
Directory.CreateDirectory(path);
Expand All @@ -454,19 +449,9 @@ int RunReplay(IEnumerable<string> args)
{
if (diagnostic.Severity >= severity)
{
Write(" ");
WriteLine(diagnostic.GetMessage());
WriteLine($" {diagnostic.Id}: {diagnostic.GetMessage()}");
}
}

if (!emitResult.Success && export)
{
var exportPath = GetOutputPath(baseOutputPath, compilerCalls, i, "export");
Directory.CreateDirectory(exportPath);
WriteLine($"Exporting to {exportPath}");
exportUtil.Export(compilationData.CompilerCall, exportPath, sdkDirs);
success = false;
}
}

return success ? ExitSuccess : ExitFailure;
Expand Down

0 comments on commit 6426bc0

Please sign in to comment.