Skip to content

Commit

Permalink
Bug fixe and code coverage (#108)
Browse files Browse the repository at this point in the history
* Code coverage

* Code coverage

* Code coverage

* Code coverage

* Code coverage

* More

* Code coverage

* Code coverage

* Code coverage
  • Loading branch information
jaredpar authored Jan 29, 2024
1 parent 28731fa commit d071044
Show file tree
Hide file tree
Showing 36 changed files with 630 additions and 129 deletions.
7 changes: 4 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/artifacts/bin/Basic.CompilerLog/debug/Basic.CompilerLog.dll",
"args": ["replay", "C:\\Users\\jaredpar\\code\\roslyn\\artifacts\\log\\Debug\\Build.binlog", "-export", "-o", "c:/users/jaredpar/temp/export"],
"cwd": "${workspaceFolder}/src/Basic.CompilerLog",
"program": "${workspaceFolder}/artifacts/bin/Basic.CompilerLog/debug_net7.0/Basic.CompilerLog.dll",
"args": ["print", ".\\msbuild.binlog" ],
"cwd": "c:\\Users\\jaredpar\\temp\\console",
// "cwd": "${workspaceFolder}/src/Basic.CompilerLog",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="7.0.13" />
<PackageVersion Include="Microsoft.NET.Test.SDK" Version="17.1.0" />
<PackageVersion Include="Mono.Options" Version="6.12.0.148" />
<PackageVersion Include="MSBuild.StructuredLogger" Version="2.2.155" />
<PackageVersion Include="MSBuild.StructuredLogger" Version="2.2.169" />
<PackageVersion Include="System.Buffers" Version="4.5.1" />
<PackageVersion Include="System.IO.Compression" Version="4.3.0" />
<PackageVersion Include="xunit" Version="2.4.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<Compile Include="..\Shared\ProcessUtil.cs" Link="ProcessUtil.cs" />
<Compile Include="..\Shared\DotnetUtil.cs" Link="DotnetUtil.cs" />
<Compile Include="..\Shared\PathUtil.cs" Link="PathUtil.cs" />
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
<ProjectReference Include="..\Basic.CompilerLog.Util\Basic.CompilerLog.Util.csproj" />
<ProjectReference Include="..\Basic.CompilerLog\Basic.CompilerLog.csproj" Condition="'$(TargetFramework)' == 'net7.0'" />
Expand Down
31 changes: 31 additions & 0 deletions src/Basic.CompilerLog.UnitTests/BasicAnalyzerHostOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Basic.CompilerLog.Util;
using Basic.CompilerLog.Util.Impl;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

#if NETCOREAPP
using System.Runtime.Loader;
#endif

namespace Basic.CompilerLog.UnitTests;

public sealed class BasicAnalyzerHostOptionsTests
{
#if NETCOREAPP
[Fact]
public void CustomAssemblyLoadContext()
{
var alc = new AssemblyLoadContext("Custom", isCollectible: true);
var options = new BasicAnalyzerHostOptions(alc, BasicAnalyzerKind.Default, cacheable: true);
Assert.Same(alc, options.CompilerLoadContext);
alc.Unload();
}
#endif
}
5 changes: 4 additions & 1 deletion src/Basic.CompilerLog.UnitTests/BasicAnalyzerHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
using System.Threading.Tasks;
using Xunit;

#if NETCOREAPP
using System.Runtime.Loader;
#endif

namespace Basic.CompilerLog.UnitTests;

public sealed class BasicAnalyzerHostTests
Expand Down Expand Up @@ -47,5 +51,4 @@ public void NoneProps()
Assert.Same(BasicAnalyzerHostOptions.None, host.Options);
Assert.Empty(host.GeneratedSourceTexts);
}

}
76 changes: 76 additions & 0 deletions src/Basic.CompilerLog.UnitTests/BinaryLogUtilTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Basic.CompilerLog.Util;
using Basic.CompilerLog.Util.Impl;
using Microsoft.Build.Logging.StructuredLogger;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

#if NETCOREAPP
using System.Runtime.Loader;
#endif

namespace Basic.CompilerLog.UnitTests;

public sealed class BinaryLogUtilTests
{
[Theory]
[InlineData("dotnet exec csc.dll a.cs", "a.cs")]
[InlineData("dotnet not what we expect a.cs", "")]
[InlineData("csc.exe a.cs b.cs", "a.cs b.cs")]
public void SkipCompilerExecutableTests(string args, string expected)
{
var realArgs = BinaryLogUtil.SkipCompilerExecutable(ToArray(args), "csc.exe", "csc.dll");
Assert.Equal(ToArray(expected), realArgs);
static string[] ToArray(string arg) => arg.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
}
}

public sealed class MSBuildProjectDataTests
{
[Fact]
public void MSBuildProjectDataToString()
{
var data = new BinaryLogUtil.MSBuildProjectData(@"example.csproj");
Assert.NotEmpty(data.ToString());
}
}

public sealed class CompilationTaskDataTests
{
internal BinaryLogUtil.MSBuildProjectData ProjectData { get; } = new BinaryLogUtil.MSBuildProjectData(@"example.csproj");

[Fact]
public void TryCreateCompilerCallBadArguments()
{
var data = new BinaryLogUtil.CompilationTaskData(ProjectData, 1)
{
CommandLineArguments = "dotnet not a compiler call",
};

var diagnostics = new List<string>();
Assert.Null(data.TryCreateCompilerCall(diagnostics));
Assert.NotEmpty(diagnostics);
}

[Fact]
public void TryCreateCompilerNoArguments()
{
var data = new BinaryLogUtil.CompilationTaskData(ProjectData, 1)
{
CommandLineArguments = null,
};

var diagnostics = new List<string>();
Assert.Null(data.TryCreateCompilerCall(diagnostics));

// This is a normal non-compile case so no diagnostics are emitted
Assert.Empty(diagnostics);
}
}
15 changes: 15 additions & 0 deletions src/Basic.CompilerLog.UnitTests/CommonUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using Basic.CompilerLog.Util;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;
#if NETCOREAPP
using System.Runtime.Loader;
#endif

namespace Basic.CompilerLog.UnitTests;

Expand All @@ -24,4 +27,16 @@ public void GetAssemblyFileName(string commandLine, string expectedFileName)
var actualFileName = CommonUtil.GetAssemblyFileName(args);
Assert.Equal(expectedFileName, actualFileName);
}

#if NETCOREAPP

[Fact]
public void GetAssemlbyLoadContext()
{
var alc = new AssemblyLoadContext("Custom", isCollectible: true);
Assert.Same(alc, CommonUtil.GetAssemblyLoadContext(alc));
alc.Unload();
}

#endif
}
16 changes: 0 additions & 16 deletions src/Basic.CompilerLog.UnitTests/CompilerLogBuilderTests

This file was deleted.

59 changes: 59 additions & 0 deletions src/Basic.CompilerLog.UnitTests/CompilerLogBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Basic.CompilerLog.Util;
using Xunit;
using Xunit.Abstractions;

namespace Basic.CompilerLog.UnitTests;

[Collection(SolutionFixtureCollection.Name)]
public sealed class CompilerLogBuilderTests : TestBase
{
public SolutionFixture Fixture { get; }

public CompilerLogBuilderTests(ITestOutputHelper testOutputHelper, SolutionFixture fixture)
: base(testOutputHelper, nameof(CompilerLogBuilderTests))
{
Fixture = fixture;
}

[Fact]
public void AddMissingFile()
{
using var stream = new MemoryStream();
using var builder = new CompilerLogBuilder(stream, new());
using var binlogStream = new FileStream(Fixture.ConsoleWithDiagnosticsBinaryLogPath, FileMode.Open, FileAccess.Read, FileShare.Read);

var compilerCall = BinaryLogUtil.ReadAllCompilerCalls(binlogStream, new()).First(x => x.IsCSharp);
compilerCall = new CompilerCall(
compilerCall.ProjectFilePath,
CompilerCallKind.Regular,
compilerCall.TargetFramework,
isCSharp: true,
new Lazy<string[]>(() => ["/sourcelink:does-not-exist.txt"]),
null);
builder.Add(compilerCall);
}

[Fact]
public void PortablePdbMissing()
{
RunDotNet("new console -o .");
RunDotNet("build -bl:msbuild.binlog");

Directory
.EnumerateFiles(RootDirectory, "*.pdb", SearchOption.AllDirectories)
.ForEach(File.Delete);

using var complogStream = new MemoryStream();
using var binlogStream = new FileStream(Path.Combine(RootDirectory, "msbuild.binlog"), FileMode.Open, FileAccess.Read, FileShare.Read);
var diagnostics = CompilerLogUtil.ConvertBinaryLog(binlogStream, complogStream);
Assert.Contains(diagnostics, x => x.Contains("Can't find portable pdb"));
}

[Fact]
public void CloseTwice()
{
var builder = new CompilerLogBuilder(new MemoryStream(), []);
builder.Close();
Assert.Throws<InvalidOperationException>(() => builder.Close());
}
}
23 changes: 22 additions & 1 deletion src/Basic.CompilerLog.UnitTests/CompilerLogFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@ public void M() { }
ConsoleVisualBasicComplogPath = WithBuild("console-vb.complog", void (string scratchPath) =>
{
RunDotnetCommand($"new console --name console-vb --language VB --output .", scratchPath);
File.WriteAllText(Path.Combine(scratchPath, "Extra.vb"), """
Module M
#ExternalSource("line.txt", 0)
Sub G()
End Sub
#End ExternalSource
End Module
""", TestBase.DefaultEncoding);
File.WriteAllText(Path.Combine(scratchPath, "line.txt"), "this is content", TestBase.DefaultEncoding);
File.WriteAllText(Path.Combine(scratchPath, "console-vb.vbproj"), """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>vbconsole</RootNamespace>
<TargetFramework>net7.0</TargetFramework>
<EmbedAllSources>true</EmbedAllSources>
</PropertyGroup>
</Project>
""", TestBase.DefaultEncoding);
RunDotnetCommand("build -bl -nr:false", scratchPath);
});

Expand Down Expand Up @@ -199,7 +220,7 @@ public void M() { }
using System;
using System.Text.RegularExpressions;
// File that does not exsit
// File that does exsit
#line 42 "line.txt"
class C { }
""", TestBase.DefaultEncoding);
Expand Down
Loading

0 comments on commit d071044

Please sign in to comment.