Skip to content

Commit

Permalink
Change command line format (#113)
Browse files Browse the repository at this point in the history
* Initial options refactoring code done

* Fixed it up

* More

* Fix tests

* Clean up API

* More
  • Loading branch information
jaredpar authored Feb 20, 2024
1 parent aa63ad6 commit 2506bc6
Show file tree
Hide file tree
Showing 26 changed files with 371 additions and 238 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"jaredpar",
"msbuild",
"Mvid",
"NETCOREAPP",
"Relogger",
"ruleset",
"Xunit"
Expand Down
8 changes: 8 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
unify the analyzer options on command line
- Move the CompilerLogState to FilterOptionSet, make that disposable
switch default to run analyzers
simplify the options, kind, etc ... for loading analyzers
- Move caching to the reader as it conttrols that
- Move ALC to the reader as it controls that
- Host options are just about what host is created, now how the host is managed
better test hooks to see what program actually ran
31 changes: 0 additions & 31 deletions src/Basic.CompilerLog.UnitTests/BasicAnalyzerHostOptionsTests.cs

This file was deleted.

8 changes: 3 additions & 5 deletions src/Basic.CompilerLog.UnitTests/BasicAnalyzerHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public sealed class BasicAnalyzerHostTests
[Fact]
public void Supported()
{
Assert.True(BasicAnalyzerHost.IsSupported(BasicAnalyzerKind.Default));
Assert.True(BasicAnalyzerHost.IsSupported(BasicAnalyzerKind.OnDisk));
Assert.True(BasicAnalyzerHost.IsSupported(BasicAnalyzerKind.None));
#if NETCOREAPP
Expand All @@ -31,24 +30,23 @@ public void Supported()
#endif

// To make sure this test is updated every time a new value is added
Assert.Equal(4, Enum.GetValues(typeof(BasicAnalyzerKind)).Length);
Assert.Equal(3, Enum.GetValues(typeof(BasicAnalyzerKind)).Length);
}

[Fact]
public void NoneDispose()
{
var host = new BasicAnalyzerHostNone(readGeneratedFiles: true, ImmutableArray<(SourceText, string)>.Empty, BasicAnalyzerHostOptions.None);
var host = new BasicAnalyzerHostNone(readGeneratedFiles: true, ImmutableArray<(SourceText, string)>.Empty);
host.Dispose();
Assert.Throws<ObjectDisposedException>(() => { _ = host.AnalyzerReferences; });
}

[Fact]
public void NoneProps()
{
var host = new BasicAnalyzerHostNone(readGeneratedFiles: true, ImmutableArray<(SourceText, string)>.Empty, BasicAnalyzerHostOptions.None);
var host = new BasicAnalyzerHostNone(readGeneratedFiles: true, ImmutableArray<(SourceText, string)>.Empty);
host.Dispose();
Assert.Equal(BasicAnalyzerKind.None, host.Kind);
Assert.Same(BasicAnalyzerHostOptions.None, host.Options);
Assert.Empty(host.GeneratedSourceTexts);
}
}
10 changes: 5 additions & 5 deletions src/Basic.CompilerLog.UnitTests/CompilationDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,37 +79,37 @@ public void GetAnalyzersNormal()
[Fact]
public void GetAnalyzersNoHosting()
{
using var reader = CompilerLogReader.Create(Fixture.ClassLibComplogPath.Value, BasicAnalyzerHostOptions.None);
using var reader = CompilerLogReader.Create(Fixture.ClassLibComplogPath.Value, CompilerLogReaderOptions.None);
var data = reader.ReadCompilationData(0);
Assert.Empty(data.GetAnalyzers());
}

[Fact]
public void GetDiagnostics()
{
using var reader = CompilerLogReader.Create(Fixture.ClassLibComplogPath.Value, BasicAnalyzerHostOptions.Default);
using var reader = CompilerLogReader.Create(Fixture.ClassLibComplogPath.Value, CompilerLogReaderOptions.Default);
var data = reader.ReadCompilationData(0);
Assert.NotEmpty(data.GetDiagnostics());
}

[Fact]
public async Task GetAllDiagnostics()
{
using var reader = CompilerLogReader.Create(Fixture.ClassLibComplogPath.Value, BasicAnalyzerHostOptions.Default);
using var reader = CompilerLogReader.Create(Fixture.ClassLibComplogPath.Value, CompilerLogReaderOptions.Default);
var data = reader.ReadCompilationData(0);
Assert.NotEmpty(await data.GetAllDiagnosticsAsync());
}

[Fact]
public void GetCompilationAfterGeneratorsDiagnostics()
{
var options = new BasicAnalyzerHostOptions(BasicAnalyzerKind.InMemory, cacheable: true);
var options = new CompilerLogReaderOptions(BasicAnalyzerKind.InMemory, cacheAnalyzers: true);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, options);
var rawData = reader.ReadRawCompilationData(0).Item2;
var analyzers = rawData.Analyzers
.Where(x => x.FileName != "Microsoft.CodeAnalysis.NetAnalyzers.dll")
.ToList();
var host = new BasicAnalyzerHostInMemory(reader, analyzers, options);
var host = new BasicAnalyzerHostInMemory(reader, analyzers);
var data = (CSharpCompilationData)reader.ReadCompilationData(0);
data = new CSharpCompilationData(
data.CompilerCall,
Expand Down
6 changes: 3 additions & 3 deletions src/Basic.CompilerLog.UnitTests/CompilerLogReaderExTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public CompilerLogReaderExTests(ITestOutputHelper testOutputHelper, SolutionFixt
/// <summary>
/// Convert the console binary log and return a reader over it
/// </summary>
private CompilerLogReader ConvertConsole(Func<CompilerCall, CompilerCall> func, BasicAnalyzerHostOptions? options = null)
private CompilerLogReader ConvertConsole(Func<CompilerCall, CompilerCall> func, CompilerLogReaderOptions? options = null)
{
var diagnostics = new List<string>();

Expand All @@ -57,10 +57,10 @@ private CompilerLogReader ConvertConsole(Func<CompilerCall, CompilerCall> func,
builder.Add(compilerCall);
builder.Close();
stream.Position = 0;
return CompilerLogReader.Create(stream, leaveOpen: false, options, State);
return CompilerLogReader.Create(stream, options, State, leaveOpen: false);
}

private CompilerLogReader ConvertConsoleArgs(Func<string[], string[]> func, BasicAnalyzerHostOptions? options = null) =>
private CompilerLogReader ConvertConsoleArgs(Func<string[], string[]> func, CompilerLogReaderOptions? options = null) =>
ConvertConsole(x =>
{
var args = func(x.GetArguments());
Expand Down
37 changes: 24 additions & 13 deletions src/Basic.CompilerLog.UnitTests/CompilerLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,18 @@ public void CreateInvalidZipFile()
using var stream = new MemoryStream();
stream.Write([1, 2, 3, 4, 5], 0, 0);
stream.Position = 0;
Assert.Throws<CompilerLogException>(() => CompilerLogReader.Create(stream, leaveOpen: true, BasicAnalyzerHostOptions.None));
Assert.Throws<CompilerLogException>(() => CompilerLogReader.Create(stream, CompilerLogReaderOptions.None, leaveOpen: true));
}

[Fact]
public void CreateRespectLeaveOpen()
{
using var stream = new FileStream(Fixture.ConsoleComplexComplogPath.Value, FileMode.Open, FileAccess.Read, FileShare.Read);
var reader = CompilerLogReader.Create(stream, leaveOpen: true);
reader.Dispose();

// Throws if the underlying stream is disposed
stream.Seek(0, SeekOrigin.Begin);
}

[Fact]
Expand Down Expand Up @@ -114,7 +125,7 @@ public void KeyFileCustomState()
using var state = new CompilerLogState(tempDir.DirectoryPath);

var keyBytes = ResourceLoader.GetResourceBlob("Key.snk");
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplexComplogPath.Value, state: state);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplexComplogPath.Value, options: null, state: state);
var data = reader.ReadCompilationData(0);

Assert.NotNull(data.CompilationOptions.CryptoKeyFile);
Expand Down Expand Up @@ -158,7 +169,7 @@ public void AnalyzerLoadOptions()
}
any = true;

var options = new BasicAnalyzerHostOptions(kind);
var options = new CompilerLogReaderOptions(kind);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, options: options);
var data = reader.ReadCompilationData(0);
var compilation = data.GetCompilationAfterGenerators(out var diagnostics);
Expand Down Expand Up @@ -189,7 +200,7 @@ public void AnalyzerLoadCaching(BasicAnalyzerKind kind)
return;
}

var options = new BasicAnalyzerHostOptions(kind, cacheable: true);
var options = new CompilerLogReaderOptions(kind, cacheAnalyzers: true);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, options: options);
var data = reader.ReadRawCompilationData(0).Item2;

Expand All @@ -211,7 +222,7 @@ public void AnalyzerLoadDispose(BasicAnalyzerKind kind)
return;
}

var options = new BasicAnalyzerHostOptions(kind, cacheable: true);
var options = new CompilerLogReaderOptions(kind, cacheAnalyzers: true);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, options: options);
var data = reader.ReadCompilationData(0);
Assert.False(data.BasicAnalyzerHost.IsDisposed);
Expand All @@ -227,13 +238,13 @@ public void AnalyzerLoadDispose(BasicAnalyzerKind kind)
[Fact]
public void AnalyzerDiagnostics()
{
var options = new BasicAnalyzerHostOptions(BasicAnalyzerKind.InMemory, cacheable: true);
var options = new CompilerLogReaderOptions(BasicAnalyzerKind.InMemory, cacheAnalyzers: true);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, options);
var data = reader.ReadRawCompilationData(0).Item2;
var analyzers = data.Analyzers
.Where(x => x.FileName != "Microsoft.CodeAnalysis.NetAnalyzers.dll")
.ToList();
var host = new BasicAnalyzerHostInMemory(reader, analyzers, options);
var host = new BasicAnalyzerHostInMemory(reader, analyzers);
foreach (var analyzer in host.AnalyzerReferences)
{
analyzer.GetAnalyzersForAllLanguages();
Expand Down Expand Up @@ -264,15 +275,15 @@ public void ProjectMultiTarget()
[Fact]
public void NoneHostGeneratedFilesInRaw()
{
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, BasicAnalyzerHostOptions.None);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, CompilerLogReaderOptions.None);
var (_, data) = reader.ReadRawCompilationData(0);
Assert.Equal(1, data.Contents.Count(x => x.Kind == RawContentKind.GeneratedText));
}

[Fact]
public void NoneHostGeneratedFilesShouldBeLast()
{
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, BasicAnalyzerHostOptions.None);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, CompilerLogReaderOptions.None);
var data = reader.ReadCompilationData(0);
var tree = data.GetCompilationAfterGenerators().SyntaxTrees.Last();
var decls = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().ToList();
Expand All @@ -284,7 +295,7 @@ public void NoneHostGeneratedFilesShouldBeLast()
[Fact]
public void NoneHostHasNoGenerators()
{
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, BasicAnalyzerHostOptions.None);
using var reader = CompilerLogReader.Create(Fixture.ConsoleComplogPath.Value, CompilerLogReaderOptions.None);
var data = reader.ReadCompilationData(0);
var compilation1 = data.Compilation;
var compilation2 = data.GetCompilationAfterGenerators();
Expand All @@ -295,7 +306,7 @@ public void NoneHostHasNoGenerators()
[Fact]
public void NoneHostAddsNoGeneratorIfNoGeneratedSource()
{
using var reader = CompilerLogReader.Create(Fixture.ConsoleNoGeneratorComplogPath.Value, BasicAnalyzerHostOptions.None);
using var reader = CompilerLogReader.Create(Fixture.ConsoleNoGeneratorComplogPath.Value, CompilerLogReaderOptions.None);
var data = reader.ReadCompilationData(0);
var compilation1 = data.Compilation;
var compilation2 = data.GetCompilationAfterGenerators();
Expand Down Expand Up @@ -326,7 +337,7 @@ public void NoneHostNativePdb()
File.WriteAllText(Path.Combine(RootDirectory, "example.csproj"), projectFileContent, DefaultEncoding);
RunDotNet("build -bl -nr:false");

using var reader = CompilerLogReader.Create(Path.Combine(RootDirectory, "msbuild.binlog"), BasicAnalyzerHostOptions.None);
using var reader = CompilerLogReader.Create(Path.Combine(RootDirectory, "msbuild.binlog"), CompilerLogReaderOptions.None);
var rawData = reader.ReadRawCompilationData(0).Item2;
Assert.False(rawData.ReadGeneratedFiles);
var data = reader.ReadCompilationData(0);
Expand Down Expand Up @@ -407,7 +418,7 @@ public void MetadataCompat(string resourceName)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using var stream = ResourceLoader.GetResourceStream(resourceName);
Assert.Throws<CompilerLogException>(() => CompilerLogReader.Create(stream, leaveOpen: true, BasicAnalyzerHostOptions.None));
Assert.Throws<CompilerLogException>(() => CompilerLogReader.Create(stream, CompilerLogReaderOptions.None, leaveOpen: true));
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/Basic.CompilerLog.UnitTests/CompilerLogStateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using Xunit;
using Xunit.Abstractions;

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

namespace Basic.CompilerLog.UnitTests;

public class CompilerLogStateTests : TestBase
Expand Down Expand Up @@ -34,4 +38,14 @@ public void DisposeDirectoryLocked()
fileStream.Dispose();
}

#if NETCOREAPP
[Fact]
public void CustomAssemblyLoadContext()
{
var alc = new AssemblyLoadContext("Custom", isCollectible: true);
var options = new CompilerLogState(alc);
Assert.Same(alc, options.CompilerLoadContext);
alc.Unload();
}
#endif
}
18 changes: 18 additions & 0 deletions src/Basic.CompilerLog.UnitTests/FilterOptionSetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if NETCOREAPP
using Xunit;

namespace Basic.CompilerLog.UnitTests;

public sealed class FilterOptionSetTest
{
[Fact]
public void CheckForAnalyzers()
{
var options = new FilterOptionSet(analyzers: false);
Assert.Throws<InvalidOperationException>(() => options.IncludeAnalyzers);

options = new FilterOptionSet(analyzers: true);
Assert.True(options.IncludeAnalyzers);
}
}
#endif
Loading

0 comments on commit 2506bc6

Please sign in to comment.