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

Increase coverage and fix a few bugs #88

Merged
merged 3 commits into from
Dec 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<EmbeddedResource Include="Resources\MetadataVersion1\console.complog">
<LogicalName>MetadataVersion1.console.complog</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Resources\linux-console.complog">
<LogicalName>linux-console.complog</LogicalName>
</EmbeddedResource>
</ItemGroup>

</Project>
16 changes: 9 additions & 7 deletions src/Basic.CompilerLog.UnitTests/CompilerLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,15 @@ public void MetadataCompat(string resourceName)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using var stream = ResourceLoader.GetResourceStream(resourceName);
using var reader = CompilerLogReader.Create(stream, leaveOpen: true, BasicAnalyzerHostOptions.None);
foreach (var compilerCall in reader.ReadAllCompilerCalls())
{
var data = reader.ReadCompilationData(compilerCall);
var result = data.EmitToMemory();
Assert.True(result.Success);
}
Assert.Throws<ArgumentException>(() => CompilerLogReader.Create(stream, leaveOpen: true, BasicAnalyzerHostOptions.None));
}
}

[Fact]
public void Disposed()
{
var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value);
reader.Dispose();
Assert.Throws<ObjectDisposedException>(() => reader.ReadCompilationData(0));
}
}
29 changes: 29 additions & 0 deletions src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,22 @@ public void ResponseProjectFilter()
Assert.Contains("Program.cs", File.ReadAllLines(rsp));
}

[Fact]
public void ResponseOnCompilerLog()
{
var complogPath = Path.Combine(RootDirectory, "msbuild.complog");
Assert.Empty(CompilerLogUtil.ConvertBinaryLog(Fixture.SolutionBinaryLogPath, complogPath));
Assert.Equal(Constants.ExitSuccess, RunCompLog($"rsp {complogPath} -p console.csproj"));
}

[Fact]
public void ResponseOnInvalidFileType()
{
var (exitCode, output) = RunCompLogEx($"rsp data.txt -p console.csproj");
Assert.Equal(Constants.ExitFailure, exitCode);
Assert.Contains("Not a valid log", output);
}

[Fact]
public void ResponseAll()
{
Expand All @@ -283,6 +299,19 @@ public void ResponseBadOption()
Assert.Contains("complog rsp [OPTIONS]", output);
}

[Fact]
public void ResponseLinuxComplog()
{
var path = Path.Combine(RootDirectory, "console.complog");
File.WriteAllBytes(path, ResourceLoader.GetResourceBlob("linux-console.complog"));
var (exitCode, output) = RunCompLogEx($"rsp {path}");
Assert.Equal(Constants.ExitSuccess, exitCode);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Contains("generated on different operating system", output);
}
}

[Theory]
[InlineData("")]
[InlineData("--exclude-analyzers")]
Expand Down
Binary file not shown.
14 changes: 14 additions & 0 deletions src/Basic.CompilerLog.UnitTests/UsingAllCompilerLogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public async Task EmitToMemory()
}
}

[Fact]
public async Task CommandLineArguments()
{
await foreach (var complogPath in Fixture.GetAllCompilerLogs(TestOutputHelper))
{
TestOutputHelper.WriteLine(complogPath);
using var reader = CompilerLogReader.Create(complogPath, options: BasicAnalyzerHostOptions.None);
foreach (var data in reader.ReadAllCompilerCalls())
{
Assert.NotEmpty(data.GetArguments());
}
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down
127 changes: 108 additions & 19 deletions src/Basic.CompilerLog.Util/CompilerLogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,34 @@

namespace Basic.CompilerLog.Util;

public abstract class CompilerLogReader : IDisposable
public sealed class CompilerLogReader : IDisposable
{
public static int LatestMetadataVersion => Metadata.LatestMetadataVersion;

/// <summary>
/// Stores the underlying archive this reader is using. Do not use directly. Instead
/// use <see cref="ZipArchive"/> which will throw if the reader is disposed
/// </summary>
private ZipArchive _zipArchiveCore;

private readonly Dictionary<Guid, MetadataReference> _refMap = new();
private readonly Dictionary<Guid, (string FileName, AssemblyName AssemblyName)> _mvidToRefInfoMap = new();
private readonly Dictionary<string, BasicAnalyzerHost> _analyzersMap = new();
private readonly bool _ownsCompilerLogState;
private readonly Dictionary<int, object> _compilationInfoMap = new();
private readonly Dictionary<int, CompilationInfoPack> _compilationInfoMap = new();

public BasicAnalyzerHostOptions BasicAnalyzerHostOptions { get; }
internal CompilerLogState CompilerLogState { get; }
internal ZipArchive ZipArchive { get; private set; }
internal Metadata Metadata { get; }
internal int Count => Metadata.Count;
public int MetadataVersion => Metadata.MetadataVersion;
public bool IsWindowsLog => Metadata.IsWindows == true;
public bool IsDisposed => _zipArchiveCore is null;
internal ZipArchive ZipArchive => !IsDisposed ? _zipArchiveCore : throw new ObjectDisposedException(nameof(CompilerLogReader));

internal CompilerLogReader(ZipArchive zipArchive, Metadata metadata, BasicAnalyzerHostOptions basicAnalyzersOptions, CompilerLogState? state)
private CompilerLogReader(ZipArchive zipArchive, Metadata metadata, BasicAnalyzerHostOptions basicAnalyzersOptions, CompilerLogState? state)
{
ZipArchive = zipArchive;
_zipArchiveCore = zipArchive;
CompilerLogState = state ?? new CompilerLogState();
_ownsCompilerLogState = state is null;
BasicAnalyzerHostOptions = basicAnalyzersOptions;
Expand Down Expand Up @@ -77,8 +84,8 @@
var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen);
var metadata = ReadMetadata();
return metadata.MetadataVersion switch {
1 => new CompilerLogReaderVersion1(zipArchive, metadata, options, state),
2 => new CompilerLogReaderVersion2(zipArchive, metadata, options, state),
1 => throw new ArgumentException("Version 1 compiler logs are no longer supported"),

Check warning on line 87 in src/Basic.CompilerLog.Util/CompilerLogReader.cs

View check run for this annotation

Codecov / codecov/patch

src/Basic.CompilerLog.Util/CompilerLogReader.cs#L87

Added line #L87 was not covered by tests
2 => new CompilerLogReader(zipArchive, metadata, options, state),
_ => throw GetInvalidCompilerLogFileException(),
};

Expand Down Expand Up @@ -108,15 +115,73 @@
return Create(stream, leaveOpen: false, options, state);
}

private protected abstract object ReadCompilationInfo(int index);
private CompilationInfoPack ReadCompilationInfo(int index)
{
using var stream = ZipArchive.OpenEntryOrThrow(GetCompilerEntryName(index));
return MessagePackSerializer.Deserialize<CompilationInfoPack>(stream);
}

private protected abstract CompilerCall ReadCompilerCallCore(int index, object rawInfo);
private CompilerCall ReadCompilerCallCore(int index, CompilationInfoPack pack)
{
return new CompilerCall(
pack.ProjectFilePath,
pack.CompilerCallKind,
pack.TargetFramework,
pack.IsCSharp,
new Lazy<string[]>(() => GetContentPack<string[]>(pack.CommandLineArgsHash)),
index);
}

private protected abstract RawCompilationData ReadRawCompilationDataCore(int index, object rawInfo);
private RawCompilationData ReadRawCompilationDataCore(int index, CompilationInfoPack pack)
{
var dataPack = GetContentPack<CompilationDataPack>(pack.CompilationDataPackHash);

var references = dataPack
.References
.Select(x => new RawReferenceData(x.Mvid, x.Aliases, x.EmbedInteropTypes))
.ToList();
var analyzers = dataPack
.Analyzers
.Select(x => new RawAnalyzerData(x.Mvid, x.FilePath))
.ToList();
var contents = dataPack
.ContentList
.Select(x => new RawContent(x.Item2.FilePath, x.Item2.ContentHash, (RawContentKind)x.Item1))
.ToList();
var resources = dataPack
.Resources
.Select(x => new RawResourceData(x.ContentHash, CreateResourceDescription(this, x)))
.ToList();

return new RawCompilationData(
index,
compilationName: dataPack.ValueMap["compilationName"],
assemblyFileName: dataPack.ValueMap["assemblyFileName"]!,
xmlFilePath: dataPack.ValueMap["xmlFilePath"],
outputDirectory: dataPack.ValueMap["outputDirectory"],
dataPack.ChecksumAlgorithm,
references,
analyzers,
contents,
resources,
pack.IsCSharp,
dataPack.IncludesGeneratedText);

static ResourceDescription CreateResourceDescription(CompilerLogReader reader, ResourcePack pack)
{
var dataProvider = () =>
{
var bytes = reader.GetContentBytes(pack.ContentHash);
return new MemoryStream(bytes);
};

private protected abstract (EmitOptions, ParseOptions, CompilationOptions) ReadCompilerOptionsCore(int index, object rawInfo);
return string.IsNullOrEmpty(pack.FileName)
? new ResourceDescription(pack.Name, dataProvider, pack.IsPublic)
: new ResourceDescription(pack.Name, pack.FileName, dataProvider, pack.IsPublic);
}
}

private object GetOrReadCompilationInfo(int index)
private CompilationInfoPack GetOrReadCompilationInfo(int index)
{
if (!_compilationInfoMap.TryGetValue(index, out var info))
{
Expand Down Expand Up @@ -154,20 +219,44 @@
public (EmitOptions EmitOptions, ParseOptions ParseOptions, CompilationOptions CompilationOptions) ReadCompilerOptions(CompilerCall compilerCall)
{
var index = GetIndex(compilerCall);
var info = GetOrReadCompilationInfo(index);
return ReadCompilerOptionsCore(index, info);
var pack = GetOrReadCompilationInfo(index);
return ReadCompilerOptions(pack);
}

private (EmitOptions EmitOptions, ParseOptions ParseOptions, CompilationOptions CompilationOptions) ReadCompilerOptions(CompilationInfoPack pack)
{
var emitOptions = MessagePackUtil.CreateEmitOptions(GetContentPack<EmitOptionsPack>(pack.EmitOptionsHash));
ParseOptions parseOptions;
CompilationOptions compilationOptions;
if (pack.IsCSharp)
{
var parseTuple = GetContentPack<(ParseOptionsPack, CSharpParseOptionsPack)>(pack.ParseOptionsHash);
parseOptions = MessagePackUtil.CreateCSharpParseOptions(parseTuple.Item1, parseTuple.Item2);

var optionsTuple = GetContentPack<(CompilationOptionsPack, CSharpCompilationOptionsPack)>(pack.CompilationOptionsHash);
compilationOptions = MessagePackUtil.CreateCSharpCompilationOptions(optionsTuple.Item1, optionsTuple.Item2);
}
else
{
var parseTuple = GetContentPack<(ParseOptionsPack, VisualBasicParseOptionsPack)>(pack.ParseOptionsHash);
parseOptions = MessagePackUtil.CreateVisualBasicParseOptions(parseTuple.Item1, parseTuple.Item2);

var optionsTuple = GetContentPack<(CompilationOptionsPack, VisualBasicCompilationOptionsPack, ParseOptionsPack, VisualBasicParseOptionsPack)>(pack.CompilationOptionsHash);
compilationOptions = MessagePackUtil.CreateVisualBasicCompilationOptions(optionsTuple.Item1, optionsTuple.Item2, optionsTuple.Item3, optionsTuple.Item4);
}

return (emitOptions, parseOptions, compilationOptions);
}

public CompilationData ReadCompilationData(int index) =>
ReadCompilationData(ReadCompilerCall(index));

public CompilationData ReadCompilationData(CompilerCall compilerCall)
{
var index = GetIndex(compilerCall);
var info = GetOrReadCompilationInfo(index);
var pack = GetOrReadCompilationInfo(GetIndex(compilerCall));
var rawCompilationData = ReadRawCompilationData(compilerCall);
var referenceList = GetMetadataReferences(rawCompilationData.References);
var (emitOptions, rawParseOptions, compilationOptions) = ReadCompilerOptionsCore(index, info);
var (emitOptions, rawParseOptions, compilationOptions) = ReadCompilerOptions(pack);

var hashAlgorithm = rawCompilationData.ChecksumAlgorithm;
var sourceTextList = new List<(SourceText SourceText, string Path)>();
Expand Down Expand Up @@ -606,13 +695,13 @@

public void Dispose()
{
if (ZipArchive is null)
if (IsDisposed)
{
return;
}

ZipArchive.Dispose();
ZipArchive = null!;
_zipArchiveCore = null!;

if (_ownsCompilerLogState)
{
Expand Down
Loading
Loading