Skip to content
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

coverage #170

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions src/Basic.CompilerLog.UnitTests/BinaryLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void GetCompilationSimple(BasicAnalyzerKind basicAnalyzerKind)
}

[Fact]
public void ReadDeletedPdb()
public void ReadGeneratedFilesDeletedPdb()
{
var dir = Root.NewDirectory();
RunDotNet($"new console --name example --output .", dir);
Expand All @@ -182,7 +182,7 @@ public void ReadDeletedPdb()
}

[Fact]
public void ReadGeneratedFiles()
public void ReadGeneratedFilesSimple()
{
using var reader = BinaryLogReader.Create(Fixture.Console.Value.BinaryLogPath!, BasicAnalyzerKind.None);
var compilerCall = reader.ReadAllCompilerCalls().Single();
Expand All @@ -191,4 +191,13 @@ public void ReadGeneratedFiles()
var tuple = generatedFiles.Single();
Assert.True(tuple.Stream.TryGetBuffer(out var _));
}

[WindowsFact]
public void ReadGeneratedFilesNativePdb()
{
Assert.NotNull(Fixture.ConsoleWithNativePdb);
using var reader = BinaryLogReader.Create(Fixture.ConsoleWithNativePdb.Value.BinaryLogPath!, BasicAnalyzerKind.None);
var compilerCall = reader.ReadAllCompilerCalls().Single();
Assert.Throws<InvalidOperationException>(() => _ = reader.ReadAllGeneratedFiles(compilerCall));
}
}
86 changes: 63 additions & 23 deletions src/Basic.CompilerLog.UnitTests/CompilerLogFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

namespace Basic.CompilerLog.UnitTests;

public readonly struct LogData(string compilerLogPath, string? binaryLogPath)
public readonly struct LogData(string compilerLogPath, string? binaryLogPath, bool supportsNoneHost = true)
{
public string CompilerLogPath { get; } = compilerLogPath;
public string? BinaryLogPath { get; } = binaryLogPath;
public bool SupportsNoneHost { get; } = supportsNoneHost;

public override string ToString() => $"{Path.GetFileName(CompilerLogPath)}";
}
Expand All @@ -40,14 +41,14 @@ public void Dispose()

public sealed class CompilerLogFixture : FixtureBase, IDisposable
{
private readonly ImmutableArray<Lazy<LogData>> _allLogs;
internal ImmutableArray<Lazy<LogData>> AllLogs { get; }

/// <summary>
/// Storage directory for all the generated artifacts and scatch directories
/// Storage directory for all the generated artifacts and scratch directories
/// </summary>
internal string StorageDirectory { get; }

internal string ScratchDirecectory { get; }
internal string ScratchDirectory { get; }

/// <summary>
/// Directory that holds the log files
Expand Down Expand Up @@ -89,6 +90,8 @@ public sealed class CompilerLogFixture : FixtureBase, IDisposable

internal Lazy<LogData>? WpfApp { get; }

internal Lazy<LogData>? ConsoleWithNativePdb { get; }

/// <summary>
/// Named complog value that makes intent of getting signed one clear
/// </summary>
Expand All @@ -103,9 +106,9 @@ public CompilerLogFixture(IMessageSink messageSink)
{
StorageDirectory = Path.Combine(Path.GetTempPath(), nameof(CompilerLogFixture), Guid.NewGuid().ToString("N"));
ComplogDirectory = Path.Combine(StorageDirectory, "logs");
ScratchDirecectory = Path.Combine(StorageDirectory, "scratch dir");
ScratchDirectory = Path.Combine(StorageDirectory, "scratch dir");
Directory.CreateDirectory(ComplogDirectory);
Directory.CreateDirectory(ScratchDirecectory);
Directory.CreateDirectory(ScratchDirectory);

var testArtifactsDir = Environment.GetEnvironmentVariable("TEST_ARTIFACTS_PATH");
if (testArtifactsDir is not null)
Expand Down Expand Up @@ -458,20 +461,51 @@ static void AddExternAlias(string projectFilePath, string aliasName)
RunDotnetCommand("new wpf --name wpfapp --output .", scratchPath);
RunDotnetCommand("build -bl -nr:false", scratchPath);
});

ConsoleWithNativePdb = WithBuild("console-with-nativepdb.complog", void (string scratchPath) =>
{
RunDotnetCommand($"new console --name console --output .", scratchPath);
var projectFileContent = """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<DebugType>full</DebugType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
""";
File.WriteAllText(Path.Combine(scratchPath, "console.csproj"), projectFileContent, TestBase.DefaultEncoding);
var program = """
using System;
using System.Text.RegularExpressions;
// This is an amazing resource
var r = Util.GetRegex();
Console.WriteLine(r);

partial class Util {
[GeneratedRegex("abc|def", RegexOptions.IgnoreCase, "en-US")]
internal static partial Regex GetRegex();
}
""";
File.WriteAllText(Path.Combine(scratchPath, "Program.cs"), program, TestBase.DefaultEncoding);
RunDotnetCommand("build -bl -nr:false", scratchPath);
}, supportsNoneHost: false);
}

WithResource("linux-console.complog");
WithResource("windows-console.complog");

_allLogs = builder.ToImmutable();
Lazy<LogData> WithBuild(string name, Action<string> action, bool expectDiagnosticMessages = false)
AllLogs = builder.ToImmutable();
Lazy<LogData> WithBuild(string name, Action<string> action, bool expectDiagnosticMessages = false, bool supportsNoneHost = true)
{
var lazy = new Lazy<LogData>(() =>
{
var start = DateTime.UtcNow;
try
{
var scratchPath = Path.Combine(ScratchDirecectory, Guid.NewGuid().ToString("N"));
var scratchPath = Path.Combine(ScratchDirectory, Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(scratchPath);
messageSink.OnDiagnosticMessage($"Starting {name} in {scratchPath}");
RunDotnetCommand("new globaljson --sdk-version 8.0.400", scratchPath);
Expand All @@ -496,7 +530,7 @@ Lazy<LogData> WithBuild(string name, Action<string> action, bool expectDiagnosti
Assert.Empty(diagnostics);
}

return new LogData(complogFilePath, binlogFilePath);
return new LogData(complogFilePath, binlogFilePath, supportsNoneHost);
}
catch (Exception ex)
{
Expand All @@ -520,15 +554,16 @@ void WithResource(string name)
}
}

public async IAsyncEnumerable<LogData> GetAllLogDatas(ITestOutputHelper testOutputHelper)
public async IAsyncEnumerable<LogData> GetAllLogData(ITestOutputHelper testOutputHelper, BasicAnalyzerKind? kind = null)
{
var start = DateTime.UtcNow;
foreach (var logData in _allLogs)
foreach (var lazyLogData in AllLogs)
{
if (logData.IsValueCreated)
LogData logData;
if (lazyLogData.IsValueCreated)
{
testOutputHelper.WriteLine($"Using cached value");
yield return logData.Value;
logData = lazyLogData.Value;
}
else
{
Expand All @@ -538,7 +573,7 @@ await Task.Factory.StartNew(() =>
try
{
testOutputHelper.WriteLine($"Starting {nameof(GetAllCompilerLogs)}");
tcs.SetResult(logData.Value);
tcs.SetResult(lazyLogData.Value);
testOutputHelper.WriteLine($"Finished {nameof(GetAllCompilerLogs)} {(DateTime.UtcNow - start).TotalSeconds:F2}s");
}
catch (Exception ex)
Expand All @@ -547,7 +582,12 @@ await Task.Factory.StartNew(() =>
}
}, TaskCreationOptions.LongRunning);

yield return await tcs.Task;
logData = await tcs.Task;
}

if (kind is not BasicAnalyzerKind.None || logData.SupportsNoneHost)
{
yield return logData;
}
}
}
Expand All @@ -560,7 +600,7 @@ await Task.Factory.StartNew(() =>
public FileLockHold LockScratchDirectory()
{
var list = new List<Stream>();
foreach (var filePath in Directory.EnumerateFiles(ScratchDirecectory, "*", SearchOption.AllDirectories))
foreach (var filePath in Directory.EnumerateFiles(ScratchDirectory, "*", SearchOption.AllDirectories))
{
// Don't lock the binlog or complogs as that is what the code is actually going to be reading
if (Path.GetExtension(filePath) is ".binlog" or ".complog")
Expand All @@ -574,9 +614,9 @@ public FileLockHold LockScratchDirectory()
return new FileLockHold(list);
}

public async IAsyncEnumerable<string> GetAllLogs(ITestOutputHelper testOutputHelper)
public async IAsyncEnumerable<string> GetAllLogs(ITestOutputHelper testOutputHelper, BasicAnalyzerKind? kind = null)
{
await foreach (var logData in GetAllLogDatas(testOutputHelper))
await foreach (var logData in GetAllLogData(testOutputHelper, kind))
{
yield return logData.CompilerLogPath;
if (logData.BinaryLogPath is { } binaryLogPath)
Expand All @@ -586,17 +626,17 @@ public async IAsyncEnumerable<string> GetAllLogs(ITestOutputHelper testOutputHel
}
}

public async IAsyncEnumerable<string> GetAllCompilerLogs(ITestOutputHelper testOutputHelper)
public async IAsyncEnumerable<string> GetAllCompilerLogs(ITestOutputHelper testOutputHelper, BasicAnalyzerKind? kind = null)
{
await foreach (var logData in GetAllLogDatas(testOutputHelper))
await foreach (var logData in GetAllLogData(testOutputHelper, kind))
{
yield return logData.CompilerLogPath;
}
}

public async IAsyncEnumerable<string> GetAllBinaryLogs(ITestOutputHelper testOutputHelper)
public async IAsyncEnumerable<string> GetAllBinaryLogs(ITestOutputHelper testOutputHelper, BasicAnalyzerKind? kind = null)
{
await foreach (var logData in GetAllLogDatas(testOutputHelper))
await foreach (var logData in GetAllLogData(testOutputHelper, kind))
{
if (logData.BinaryLogPath is { } binaryLog)
{
Expand Down
14 changes: 14 additions & 0 deletions src/Basic.CompilerLog.UnitTests/CompilerLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,20 @@ public void NoneHostAddsNoGeneratorIfNoGeneratedSource()
Assert.Single(data.AnalyzerReferences);
}

[WindowsFact]
public void HasAllGeneratedFileContent()
{
Run(Fixture.Console.Value.CompilerLogPath, true);
Run(Fixture.ConsoleWithNativePdb!.Value.CompilerLogPath, false);

void Run(string complogFilePath, bool expected)
{
using var reader = CompilerLogReader.Create(complogFilePath, BasicAnalyzerKind.None);
var compilerCall = reader.ReadCompilerCall(0);
Assert.Equal(expected, reader.HasAllGeneratedFileContent(compilerCall));
}
}

[Fact]
public void NoneHostNativePdb()
{
Expand Down
Loading
Loading