Skip to content

Commit

Permalink
Added tests for different binlog versions
Browse files Browse the repository at this point in the history
  • Loading branch information
daveaglick committed Aug 17, 2021
1 parent f9e50f7 commit 93e2dc0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
27 changes: 17 additions & 10 deletions src/Buildalyzer/Logging/EventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public EventProcessor(AnalyzerManager manager, ProjectAnalyzer analyzer, IEnumer
}

// In binlog 14 we need to gather properties and items during evaluation and "glue" them with the project event args
// But can never remove ProjectStarted: "even v14 will log them on ProjectStarted if any legacy loggers are present (for compat)"
// See https://twitter.com/KirillOsenkov/status/1427686459713019904
private void StatusEventRaised(object sender, BuildStatusEventArgs e)
{
// Needed to add an extern alias, see https://github.com/KirillOsenkov/MSBuildStructuredLog/issues/521
Expand Down Expand Up @@ -160,19 +162,24 @@ private void TargetFinished(object sender, TargetFinishedEventArgs e)

private void MessageRaised(object sender, BuildMessageEventArgs e)
{
// Process the command line arguments for the Fsc task
AnalyzerResult result = _currentResult.Count == 0 ? null : _currentResult.Peek();
if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true && !string.IsNullOrWhiteSpace(e.Message) && _targetStack.Any(x => x.TargetName == "CoreCompile") && _currentResult.Count != 0 && !result.HasFscArguments())
if (result is object)
{
result.ProcessFscCommandLine(e.Message);
}
// Process the command line arguments for the Fsc task
if (e.SenderName?.Equals("Fsc", StringComparison.OrdinalIgnoreCase) == true
&& !string.IsNullOrWhiteSpace(e.Message)
&& _targetStack.Any(x => x.TargetName == "CoreCompile")
&& !result.HasFscArguments())
{
result.ProcessFscCommandLine(e.Message);
}

// Process the command line arguments for the Csc task
if (result != null
&& e is TaskCommandLineEventArgs cmd
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase))
{
result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile"));
// Process the command line arguments for the Csc task
if (e is TaskCommandLineEventArgs cmd
&& string.Equals(cmd.TaskName, "Csc", StringComparison.OrdinalIgnoreCase))
{
result.ProcessCscCommandLine(cmd.CommandLine, _targetStack.Any(x => x.TargetName == "CoreCompile"));
}
}
}

Expand Down
78 changes: 40 additions & 38 deletions tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,55 +560,57 @@ public void BuildsLotsOfProjects()
analyzerResults.Count.ShouldBe(50);
}

[Test]
public void GetsSourceFilesFromVersion14BinLog()
// To produce different versions, create a global.json and then run `dotnet clean` and `dotnet build -bl:SdkNetCore31Project-vX.binlog` from the source project folder
[TestCase("SdkNetCore31Project-v9.binlog", 9)]
[TestCase("SdkNetCore31Project-v14.binlog", 14)]
public void GetsSourceFilesFromBinLogFile(string path, int expectedVersion)
{
// Given
StringWriter log = new StringWriter();
IProjectAnalyzer analyzer = GetProjectAnalyzer(
@"SdkNetCore31Project\SdkNetCore31Project.csproj",
log);
string binLogPath = Path.ChangeExtension(Path.GetTempFileName(), ".binlog");
// Verify this is the expected version
path = Path.GetFullPath(
Path.Combine(
Path.GetDirectoryName(typeof(SimpleProjectsFixture).Assembly.Location),
"..",
"..",
"..",
"..",
"binlogs",
path))
.Replace('\\', Path.DirectorySeparatorChar);
EnvironmentOptions options = new EnvironmentOptions();
options.Arguments.Add("/bl:" + binLogPath); // Tell MSBuild to produce the binlog so we use the latest internal logger

try
using (Stream stream = File.OpenRead(path))
{
// When
analyzer.Build(options);
using (Stream stream = File.OpenRead(binLogPath))
using (GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress))
{
using (GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress))
using (BinaryReader reader = new BinaryReader(gzip))
{
using (BinaryReader reader = new BinaryReader(gzip))
{
// Verify this produced a version 14 binlog
reader.ReadInt32().ShouldBe(14);
}
reader.ReadInt32().ShouldBe(expectedVersion);
}
}
IAnalyzerResults analyzerResults = analyzer.Manager.Analyze(binLogPath);
IReadOnlyList<string> sourceFiles = analyzerResults.First().SourceFiles;
}

// Then
sourceFiles.ShouldNotBeNull(log.ToString());
new[]
// Given
StringWriter log = new StringWriter();
AnalyzerManager analyzerManager = new AnalyzerManager(
new AnalyzerManagerOptions
{
LogWriter = log
});

// When
IAnalyzerResults analyzerResults = analyzerManager.Analyze(path);
IReadOnlyList<string> sourceFiles = analyzerResults.First().SourceFiles;

// Then
sourceFiles.ShouldNotBeNull(log.ToString());
new[]
{
#if Is_Windows
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
"AssemblyAttributes",
#endif
"Class1",
"AssemblyInfo"
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
}
finally
{
if (File.Exists(binLogPath))
{
File.Delete(binLogPath);
}
}
"Class1",
"AssemblyInfo"
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
}

private static IProjectAnalyzer GetProjectAnalyzer(string projectFile, StringWriter log)
Expand Down
Binary file added tests/binlogs/SdkNetCore31Project-v14.binlog
Binary file not shown.
Binary file added tests/binlogs/SdkNetCore31Project-v9.binlog
Binary file not shown.

0 comments on commit 93e2dc0

Please sign in to comment.