Skip to content

Commit

Permalink
net472 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Oct 17, 2024
1 parent 474b62c commit 13b5395
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 13 deletions.
2 changes: 0 additions & 2 deletions src/Basic.CompilerLog.UnitTests/BasicAnalyzerHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis.CSharp;



#if NET
using System.Runtime.Loader;
#endif
Expand Down
5 changes: 2 additions & 3 deletions src/Basic.CompilerLog.UnitTests/BinaryLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public void CreateStream2(BasicAnalyzerKind basicAnalyzerKind, bool leaveOpen)
}

[Theory]
[InlineData(BasicAnalyzerKind.InMemory)]
[InlineData(BasicAnalyzerKind.OnDisk)]
[MemberData(nameof(GetSupportedBasicAnalyzerKinds))]
public void VerifyBasicAnalyzerKind(BasicAnalyzerKind basicAnalyzerKind)
{
using var reader = BinaryLogReader.Create(Fixture.Console.Value.BinaryLogPath!, basicAnalyzerKind);
Expand All @@ -135,7 +134,7 @@ public void VerifyBasicAnalyzerKind(BasicAnalyzerKind basicAnalyzerKind)
}

[Theory]
[CombinatorialData]
[MemberData(nameof(GetSupportedBasicAnalyzerKinds))]
public void GetCompilationSimple(BasicAnalyzerKind basicAnalyzerKind)
{
using var reader = BinaryLogReader.Create(Fixture.Console.Value.BinaryLogPath!, basicAnalyzerKind);
Expand Down
13 changes: 13 additions & 0 deletions src/Basic.CompilerLog.UnitTests/CommonUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,17 @@ public void GetAssemlbyLoadContext()
}

#endif

[Fact]
public void Defines()
{
#if NET
Assert.True(DotnetUtil.IsNetCore);
Assert.False(DotnetUtil.IsNetFramework);
#elif NETFRAMEWORK
Assert.False(DotnetUtil.IsNetCore);
Assert.True(DotnetUtil.IsNetFramework);
#endif
}
}

8 changes: 5 additions & 3 deletions src/Basic.CompilerLog.UnitTests/CompilationDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ public void GetCompilationAfterGeneratorsDiagnostics()
{
using var reader = CompilerLogReader.Create(
Fixture.Console.Value.CompilerLogPath,
BasicAnalyzerKind.InMemory);
BasicAnalyzerHost.DefaultKind);
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);
BasicAnalyzerHost host = DotnetUtil.IsNetCore
? new BasicAnalyzerHostInMemory(reader, analyzers)
: new BasicAnalyzerHostOnDisk(reader, analyzers);
var data = (CSharpCompilationData)reader.ReadCompilationData(0);
data = new CSharpCompilationData(
data.CompilerCall,
Expand All @@ -126,7 +128,7 @@ public void GetCompilationAfterGeneratorsDiagnostics()
}

[Theory]
[CombinatorialData]
[MemberData(nameof(GetSupportedBasicAnalyzerKinds))]
public void GetGeneratedSyntaxTrees(BasicAnalyzerKind basicAnalyzerKind)
{
using var reader = CompilerLogReader.Create(Fixture.Console.Value.CompilerLogPath, basicAnalyzerKind);
Expand Down
1 change: 1 addition & 0 deletions src/Basic.CompilerLog.UnitTests/CompilerLogReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public void AnalyzerDiagnostics()
}
Assert.NotEmpty(host.GetDiagnostics());
}

#endif

[Fact]
Expand Down
2 changes: 2 additions & 0 deletions src/Basic.CompilerLog.UnitTests/ExportUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public void StrongNameKey()
TestExport(Fixture.ConsoleSigned.Value.CompilerLogPath, expectedCount: 1, runBuild: false);
}

#if NET
[Fact]
public void ExportAll()
{
Expand All @@ -297,6 +298,7 @@ public void ExportAllBadPath()
var exportUtil = new ExportUtil(reader, includeAnalyzers: false);
Assert.Throws<ArgumentException>(() => exportUtil.ExportAll(@"relative/path", SdkUtil.GetSdkDirectories()));
}
#endif

/// <summary>
/// Make sure that unix paths aren't confused as options when exporting the RSP file
Expand Down
2 changes: 1 addition & 1 deletion src/Basic.CompilerLog.UnitTests/SolutionReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private Solution GetSolution(string compilerLogFilePath, BasicAnalyzerKind basic
}

[Theory]
[CombinatorialData]
[MemberData(nameof(GetSupportedBasicAnalyzerKinds))]
public async Task DocumentsGeneratedDefaultHost(BasicAnalyzerKind basicAnalyzerKind)
{
var solution = GetSolution(Fixture.Console.Value.CompilerLogPath, basicAnalyzerKind);
Expand Down
11 changes: 11 additions & 0 deletions src/Basic.CompilerLog.UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public abstract class TestBase : IDisposable
internal Util.LogReaderState State { get; }
internal string RootDirectory => Root.DirectoryPath;

public static IEnumerable<object[]> GetSupportedBasicAnalyzerKinds()
{
yield return new object[] { BasicAnalyzerKind.None };
yield return new object[] { BasicAnalyzerKind.OnDisk };

if (DotnetUtil.IsNetCore)
{
yield return new object[] { BasicAnalyzerKind.InMemory };
}
}

protected TestBase(ITestOutputHelper testOutputHelper, string name)
{
TestOutputHelper = testOutputHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public async Task GeneratedFilePathsNoneHost()
}

[Theory]
[CombinatorialData]
[MemberData(nameof(GetSupportedBasicAnalyzerKinds))]
public async Task EmitToMemory(BasicAnalyzerKind basicAnalyzerKind)
{
TestOutputHelper.WriteLine($"BasicAnalyzerKind: {basicAnalyzerKind}");
Expand Down
47 changes: 44 additions & 3 deletions src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.VisualBasic;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;

#if NET
using System.Runtime.Loader;
#endif
Expand Down Expand Up @@ -33,7 +37,7 @@ internal BasicAnalyzerHostOnDisk(IBasicAnalyzerHostDataProvider provider, List<R
AnalyzerDirectory = Path.Combine(provider.LogReaderState.AnalyzerDirectory, dirName);
Directory.CreateDirectory(AnalyzerDirectory);

Loader = new OnDiskLoader(name, AnalyzerDirectory, provider.LogReaderState);
Loader = new OnDiskLoader(name, AnalyzerDirectory, provider.LogReaderState, AddDiagnostic);

// Now create the AnalyzerFileReference. This won't actually pull on any assembly loading
// until later so it can be done at the same time we're building up the files.
Expand Down Expand Up @@ -71,7 +75,7 @@ internal sealed class OnDiskLoader : AssemblyLoadContext, IAnalyzerAssemblyLoade
internal AssemblyLoadContext CompilerLoadContext { get; set; }
internal string AnalyzerDirectory { get; }

internal OnDiskLoader(string name, string analyzerDirectory, LogReaderState state)
internal OnDiskLoader(string name, string analyzerDirectory, LogReaderState state, Action<Diagnostic> _)
: base(name, isCollectible: true)
{
CompilerLoadContext = state.CompilerLoadContext;
Expand Down Expand Up @@ -125,15 +129,44 @@ public void AddDependencyLocation(string fullPath)

internal sealed class OnDiskLoader : IAnalyzerAssemblyLoader, IDisposable
{
public static readonly DiagnosticDescriptor CannotFindAssembly =
new DiagnosticDescriptor(
"BCLA0002",

Check warning on line 134 in src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs

View workflow job for this annotation

GitHub Actions / Build and Test windows-latest

Diagnostic Id 'BCLA0002' is already used by analyzer 'BasicAnalyzerReference'. Please use a different diagnostic ID.

Check warning on line 134 in src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs

View workflow job for this annotation

GitHub Actions / Build and Test windows-latest

Diagnostic Id 'BCLA0002' is already used by analyzer 'BasicAnalyzerReference'. Please use a different diagnostic ID.

Check warning on line 134 in src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs

View workflow job for this annotation

GitHub Actions / Build and Test windows-latest

Diagnostic Id 'BCLA0002' is already used by analyzer 'BasicAnalyzerReference'. Please use a different diagnostic ID.

Check warning on line 134 in src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs

View workflow job for this annotation

GitHub Actions / Build and Test windows-latest

Diagnostic Id 'BCLA0002' is already used by analyzer 'BasicAnalyzerReference'. Please use a different diagnostic ID.

Check warning on line 134 in src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs

View workflow job for this annotation

GitHub Actions / Build and Test ubuntu-latest

Diagnostic Id 'BCLA0002' is already used by analyzer 'BasicAnalyzerReference'. Please use a different diagnostic ID.

Check warning on line 134 in src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs

View workflow job for this annotation

GitHub Actions / Build and Test ubuntu-latest

Diagnostic Id 'BCLA0002' is already used by analyzer 'BasicAnalyzerReference'. Please use a different diagnostic ID.

Check warning on line 134 in src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs

View workflow job for this annotation

GitHub Actions / Build and Test ubuntu-latest

Diagnostic Id 'BCLA0002' is already used by analyzer 'BasicAnalyzerReference'. Please use a different diagnostic ID.

Check warning on line 134 in src/Basic.CompilerLog.Util/Impl/BasicAnalyzerHostOnDisk.cs

View workflow job for this annotation

GitHub Actions / Build and Test ubuntu-latest

Diagnostic Id 'BCLA0002' is already used by analyzer 'BasicAnalyzerReference'. Please use a different diagnostic ID.
"Cannot find assembly",
"Cannot find assembly {0}",
"BasicCompilerLog",
DiagnosticSeverity.Error,
isEnabledByDefault: true);

internal string Name { get; }
internal string AnalyzerDirectory { get; }
internal Action<Diagnostic> OnDiagnostic { get; }
internal Dictionary<string, Assembly> AssemblyMap { get; } = new(StringComparer.OrdinalIgnoreCase);

internal OnDiskLoader(string name, string analyzerDirectory, LogReaderState state)
internal OnDiskLoader(string name, string analyzerDirectory, LogReaderState state, Action<Diagnostic> onDiagnostic)
{
Name = name;
AnalyzerDirectory = analyzerDirectory;
OnDiagnostic = onDiagnostic;

AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;

// When running as a library there is no guarantee that we have a app.config with correct
// binding redirects. To account for this we will map the core compiler assemblies by
// simple name and resolve them in assembly resolve.
Assembly[] platformAssemblies =
[
typeof(Compilation).Assembly,
typeof(CSharpSyntaxNode).Assembly,
typeof(VisualBasicSyntaxNode).Assembly,
typeof(ImmutableArray).Assembly,
typeof(PEReader).Assembly,
];

foreach (var assembly in platformAssemblies)
{
AssemblyMap[assembly.GetName().Name] = assembly;
}
}

public void Dispose()
Expand All @@ -143,12 +176,20 @@ public void Dispose()

private Assembly? OnAssemblyResolve(object sender, ResolveEventArgs e)
{
var assemblyName = new AssemblyName(e.Name);
if (AssemblyMap.TryGetValue(assemblyName.Name, out var assembly))
{
return assembly;
}

var name = Path.Combine(AnalyzerDirectory, $"{e.Name}.dll");
if (File.Exists(name))
{
return Assembly.LoadFrom(name);
}

var diagnostic = Diagnostic.Create(CannotFindAssembly, Location.None, assemblyName);
OnDiagnostic(diagnostic);
return null;
}

Expand Down
8 changes: 8 additions & 0 deletions src/Shared/DotnetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ internal static class DotnetUtil
{
private static readonly Lazy<Dictionary<string, string>> _lazyDotnetEnvironmentVariables = new(CreateDotnetEnvironmentVariables);

#if NET
internal static bool IsNetCore => true;
internal static bool IsNetFramework => false;
#elif NETFRAMEWORK
internal static bool IsNetCore => false;
internal static bool IsNetFramework => true;
#endif

private static Dictionary<string, string> CreateDotnetEnvironmentVariables()
{
// The CLI, particularly when run from dotnet test, will set the MSBuildSDKsPath environment variable
Expand Down

0 comments on commit 13b5395

Please sign in to comment.