Skip to content

Commit

Permalink
Cleaned up hosting a bit (#100)
Browse files Browse the repository at this point in the history
* Cleaned up hosting code a bit

* Removed dead code

* more
  • Loading branch information
jaredpar authored Dec 23, 2023
1 parent 76c9fd3 commit d9654ba
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 150 deletions.
16 changes: 16 additions & 0 deletions src/Basic.CompilerLog.UnitTests/AssertEx.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

using Basic.CompilerLog.Util;
using Xunit;
using Xunit.Abstractions;

namespace Basic.CompilerLog.UnitTests;

Expand All @@ -10,4 +12,18 @@ internal static void HasData(MemoryStream? stream)
Assert.NotNull(stream);
Assert.True(stream.Length > 0);
}

internal static void Success<T>(ITestOutputHelper testOutputHelper, T emitResult)
where T : struct, IEmitResult
{
if (!emitResult.Success)
{
foreach (var diagnostic in emitResult.Diagnostics)
{
testOutputHelper.WriteLine(diagnostic.ToString());
}
}

Assert.True(emitResult.Success);
}
}
5 changes: 3 additions & 2 deletions src/Basic.CompilerLog.UnitTests/BasicAnalyzerHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,21 @@ public void Supported()
}

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

[Fact]
public void Props()
public void NoneProps()
{
var host = new BasicAnalyzerHostNone(readGeneratedFiles: true, ImmutableArray<(SourceText, string)>.Empty, BasicAnalyzerHostOptions.None);
host.Dispose();
Assert.Equal(BasicAnalyzerKind.None, host.Kind);
Assert.Same(BasicAnalyzerHostOptions.None, host.Options);
Assert.Empty(host.GeneratedSourceTexts);
}

}
4 changes: 2 additions & 2 deletions src/Basic.CompilerLog.UnitTests/CodeAnalysisExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public void EmitToMemory()
var data = GetCompilationData(Fixture.ClassLibComplogPath.Value);
var compilation = data.GetCompilationAfterGenerators();
var result = compilation.EmitToMemory(EmitFlags.Default);
Assert.True(result.Success);
AssertEx.Success(TestOutputHelper, result);
AssertEx.HasData(result.AssemblyStream);
Assert.Null(result.PdbStream);
Assert.Null(result.XmlStream);
Assert.Null(result.MetadataStream);

result = compilation.EmitToMemory(EmitFlags.IncludePdbStream);
Assert.True(result.Success);
AssertEx.Success(TestOutputHelper, result);
AssertEx.HasData(result.AssemblyStream);
AssertEx.HasData(result.PdbStream);
Assert.Null(result.XmlStream);
Expand Down
22 changes: 22 additions & 0 deletions src/Basic.CompilerLog.UnitTests/CompilerLogFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,28 @@ This is an awesome resource
}
}
""");
File.WriteAllText(Path.Combine(scratchPath, ".editorconfig"), """
# This file is the top-most EditorConfig file
root = true
# All Files
[*]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
[*.{cs,vb,}]
# Default Severity for all .NET Code Style rules below
dotnet_analyzer_diagnostic.severity = warning
dotnet_style_qualification_for_field = true:warning
dotnet_style_qualification_for_property = true:warning
dotnet_style_qualification_for_method = true:warning
dotnet_style_qualification_for_event = true:warning
""");
RunDotnetCommand("build -bl -nr:false", scratchPath);
});

Expand Down
37 changes: 37 additions & 0 deletions src/Basic.CompilerLog.UnitTests/CompilerLogStateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

using Basic.CompilerLog.Util;
using Xunit;
using Xunit.Abstractions;

namespace Basic.CompilerLog.UnitTests;

public class CompilerLogStateTests : TestBase
{
public CompilerLogStateTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper, nameof(CompilerLogStateTests))
{
}

[Fact]
public void DisposeCleansUpDirectories()
{
var state = new CompilerLogState(baseDir: Root.NewDirectory());
Directory.CreateDirectory(state.AnalyzerDirectory);
state.Dispose();
Assert.False(Directory.Exists(state.BaseDirectory));
}

/// <summary>
/// Don't throw if state can't clean up the directories because they are locked.
/// </summary>
[Fact]
public void DisposeDirectoryLocked()
{
var state = new CompilerLogState(baseDir: Root.NewDirectory());
Directory.CreateDirectory(state.AnalyzerDirectory);
var fileStream = new FileStream(Path.Combine(state.AnalyzerDirectory, "example.txt"), FileMode.Create, FileAccess.ReadWrite, FileShare.None);
state.Dispose();
fileStream.Dispose();
}

}
2 changes: 1 addition & 1 deletion src/Basic.CompilerLog.UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected TestBase(ITestOutputHelper testOutputHelper, string name)
{
TestOutputHelper = testOutputHelper;
Root = new TempDir(name);
State = new CompilerLogState(Root.NewDirectory("crypto-keys"));
State = new CompilerLogState(Root.NewDirectory("state"));
}

public void Dispose()
Expand Down
8 changes: 4 additions & 4 deletions src/Basic.CompilerLog.UnitTests/UsingAllCompilerLogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task EmitToDisk()
using var testDir = new TempDir();
TestOutputHelper.WriteLine($"{Path.GetFileName(complogPath)}: {data.CompilerCall.ProjectFileName} ({data.CompilerCall.TargetFramework})");
var emitResult = data.EmitToDisk(testDir.DirectoryPath);
Assert.True(emitResult.Success);
AssertEx.Success(TestOutputHelper, emitResult);
}
});

Expand All @@ -56,7 +56,7 @@ public async Task EmitToMemory()
{
TestOutputHelper.WriteLine($"\t{data.CompilerCall.ProjectFileName} ({data.CompilerCall.TargetFramework})");
var emitResult = data.EmitToMemory();
Assert.True(emitResult.Success);
AssertEx.Success(TestOutputHelper, emitResult);
}
}
}
Expand All @@ -67,12 +67,12 @@ public async Task EmitToMemoryWithSeparateState()
await foreach (var complogPath in Fixture.GetAllCompilerLogs(TestOutputHelper))
{
TestOutputHelper.WriteLine(complogPath);
using var state = new CompilerLogState(cryptoKeyFileDirectoryBase: Root.NewDirectory());
using var state = new CompilerLogState(baseDir: Root.NewDirectory());
foreach (var data in ReadAll(complogPath, state))
{
TestOutputHelper.WriteLine($"\t{data.CompilerCall.ProjectFileName} ({data.CompilerCall.TargetFramework})");
var emitResult = data.EmitToMemory();
Assert.True(emitResult.Success);
AssertEx.Success(TestOutputHelper, emitResult);
}
}

Expand Down
31 changes: 14 additions & 17 deletions src/Basic.CompilerLog.Util/Basic.CompilerLog.Util.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions src/Basic.CompilerLog.Util/BasicAnalyzerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ public static BasicAnalyzerKind RuntimeDefaultKind

public BasicAnalyzerKind Kind { get; }

/// <summary>
/// In the case analyzers are realized on disk for evaluation this is the base directory they should
/// be in.
/// </summary>
public string? AnalyzerDirectory { get; }

/// <summary>
/// When true requests for the exact same set of analyzers will return
/// the same <see cref="BasicAnalyzerHost"/> instance.
Expand All @@ -65,11 +59,9 @@ public static BasicAnalyzerKind RuntimeDefaultKind
public BasicAnalyzerHostOptions(
AssemblyLoadContext compilerLoadContext,
BasicAnalyzerKind kind,
string? analyzerDirectory = null,
bool cacheable = true)
{
Kind = kind;
AnalyzerDirectory = analyzerDirectory;
CompilerLoadContext = compilerLoadContext;
Cacheable = cacheable;
}
Expand All @@ -82,19 +74,12 @@ public BasicAnalyzerHostOptions(
bool cacheable = true)
{
Kind = kind;
AnalyzerDirectory = analyzerDirectory;
Cacheable = cacheable;

#if NETCOREAPP
CompilerLoadContext = CommonUtil.GetAssemblyLoadContext(null);
#endif
}

public string GetAnalyzerDirectory(string name)
{
var basePath = AnalyzerDirectory ?? Path.Combine(Path.GetTempPath(), "Basic.CompilerLog");
return Path.Combine(basePath, name);
}
}

/// <summary>
Expand Down
36 changes: 29 additions & 7 deletions src/Basic.CompilerLog.Util/CompilerLogState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Basic.CompilerLog.Util;

Expand All @@ -19,31 +20,52 @@ namespace Basic.CompilerLog.Util;
/// </summary>
public sealed class CompilerLogState : IDisposable
{
internal string BaseDirectory { get; }

/// <summary>
/// The compiler supports strong named keys that exist on disk. In order for compilation to succeed at the
/// Emit section, even for some binding purposes, that file must continue to exist on disk when the project
/// is re-hydrated.
/// </summary>
public string CryptoKeyFileDirectory { get; }

/// <summary>
/// In the case analyzers are realized on disk for evaluation this is the base directory they should
/// be in.
/// </summary>
public string AnalyzerDirectory { get; }

internal List<BasicAnalyzerHost> BasicAnalyzerHosts { get; } = new();

public CompilerLogState(string? cryptoKeyFileDirectoryBase = null)
/// <summary>
/// Create a new instance of the compiler log state
/// </summary>
/// <param name="baseDir">The base path that should be used to create <see cref="CryptoKeyFileDirectory"/>
/// and <see cref="AnalyzerDirectory"/> paths</param>
public CompilerLogState(string? baseDir = null)
{
cryptoKeyFileDirectoryBase ??= Path.GetTempPath();
CryptoKeyFileDirectory = Path.Combine(cryptoKeyFileDirectoryBase, "Basic.CompilerLog", Guid.NewGuid().ToString());
BaseDirectory = baseDir ?? Path.Combine(Path.GetTempPath(), "Basic.CompilerLog", Guid.NewGuid().ToString("N"));
CryptoKeyFileDirectory = Path.Combine(BaseDirectory, "CryptoKeys");
AnalyzerDirectory = Path.Combine(BaseDirectory, "Analyzers");
}

public void Dispose()
{
if (Directory.Exists(CryptoKeyFileDirectory))
foreach (var host in BasicAnalyzerHosts)
{
Directory.Delete(CryptoKeyFileDirectory, recursive: true);
host.Dispose();
}

foreach (var host in BasicAnalyzerHosts)
try
{
host.Dispose();
if (Directory.Exists(BaseDirectory))
{
Directory.Delete(BaseDirectory, recursive: true);
}
}
catch (Exception)
{
// Nothing to do if we can't delete the directories
}
}
}
26 changes: 0 additions & 26 deletions src/Basic.CompilerLog.Util/Impl/BasicAnalyzerConfigOptions.cs

This file was deleted.

Loading

0 comments on commit d9654ba

Please sign in to comment.