Fix SDK path issues

The command line tests are largely failing on CoreClr due to the
inability to implicitly find the SDK directory. On desktop it's possible
to use the directory containing the object assembly. On CoreClr there is
no real equivalent. Fixed this by just writing out mscorlib to a
directory on disk and making that directory explicitly the SDK
directory.
上级 dd617eb3
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.CSharp.CommandLine.UnitTests
{
public abstract class CommandLineTestBase : CSharpTestBase
{
public string WorkingDirectory { get; }
public string SdkDirectory { get; }
public string MscorlibFullPath { get; }
public CommandLineTestBase()
{
WorkingDirectory = TempRoot.Root;
SdkDirectory = GetSdkDirectory(Temp);
MscorlibFullPath = Path.Combine(SdkDirectory, "mscorlib.dll");
}
/// <summary>
/// This will return a directory which contains mscorlib for use in the compiler instances created for
/// this set of tests
/// </summary>
/// <returns></returns>
protected static string GetSdkDirectory(TempRoot temp)
{
if (ExecutionConditionUtil.IsCoreClr)
{
var dir = temp.CreateDirectory();
File.WriteAllBytes(Path.Combine(dir.Path, "mscorlib.dll"), TestResources.NetFX.net461.mscorlib);
return dir.Path;
}
return RuntimeEnvironment.GetRuntimeDirectory();
}
internal MockCSharpCompiler CreateCSharpCompiler(string[] args, ImmutableArray<DiagnosticAnalyzer> analyzers = default, AnalyzerAssemblyLoader loader = null)
{
return CreateCSharpCompiler(null, WorkingDirectory, args, analyzers, loader);
}
internal MockCSharpCompiler CreateCSharpCompiler(string responseFile, string workingDirectory, string[] args, ImmutableArray<DiagnosticAnalyzer> analyzers = default, AnalyzerAssemblyLoader loader = null)
{
var buildPaths = RuntimeUtilities.CreateBuildPaths(workingDirectory, sdkDirectory: SdkDirectory);
return new MockCSharpCompiler(responseFile, buildPaths, args, analyzers, loader);
}
}
}
因为 它太大了无法显示 source diff 。你可以改为 查看blob
......@@ -16,10 +16,8 @@
namespace Microsoft.CodeAnalysis.CSharp.CommandLine.UnitTests
{
public class ErrorLoggerTests : CSharpTestBase
public class ErrorLoggerTests : CommandLineTestBase
{
private readonly string _baseDirectory = TempRoot.Root;
[Fact]
public void NoDiagnostics()
{
......@@ -36,8 +34,7 @@ public static void Main(string[] args)
var errorLogDir = Temp.CreateDirectory();
var errorLogFile = Path.Combine(errorLogDir.Path, "ErrorLog.txt");
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[] { "/nologo", hello,
$"/errorlog:{errorLogFile}" });
var cmd = CreateCSharpCompiler(new[] { "/nologo", hello, $"/errorlog:{errorLogFile}" });
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var exitCode = cmd.Run(outWriter);
......@@ -73,7 +70,7 @@ public class C
var errorLogDir = Temp.CreateDirectory();
var errorLogFile = Path.Combine(errorLogDir.Path, "ErrorLog.txt");
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[] {
var cmd = CreateCSharpCompiler(null, WorkingDirectory, new[] {
"/nologo", sourceFile, "/preferreduilang:en", $"/errorlog:{errorLogFile}" });
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
......@@ -169,7 +166,7 @@ public class C
var errorLogDir = Temp.CreateDirectory();
var errorLogFile = Path.Combine(errorLogDir.Path, "ErrorLog.txt");
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[] {
var cmd = CreateCSharpCompiler(null, WorkingDirectory, new[] {
"/nologo", sourceFile, "/preferreduilang:en", $"/errorlog:{errorLogFile}" });
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
......@@ -267,7 +264,7 @@ public class C
var errorLogFile = Path.Combine(outputDir.Path, "ErrorLog.txt");
var outputFilePath = Path.Combine(outputDir.Path, "test.dll");
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[] {
var cmd = CreateCSharpCompiler(null, WorkingDirectory, new[] {
"/nologo", "/t:library", $"/out:{outputFilePath}", sourceFile, "/preferreduilang:en", $"/errorlog:{errorLogFile}" },
analyzers: ImmutableArray.Create<DiagnosticAnalyzer>(new AnalyzerForErrorLogTest()));
......
......@@ -6,7 +6,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis.CSharp.CommandLine.UnitTests</RootNamespace>
<TargetFrameworks>$(RoslynPortableTargetFrameworks)</TargetFrameworks>
<TargetFrameworks>netcoreapp2.1;net46</TargetFrameworks>
<RoslynProjectType>UnitTestPortable</RoslynProjectType>
<GenerateMicrosoftCodeAnalysisCommitHashAttribute>true</GenerateMicrosoftCodeAnalysisCommitHashAttribute>
</PropertyGroup>
......
......@@ -15,10 +15,9 @@
namespace Microsoft.CodeAnalysis.CSharp.CommandLine.UnitTests
{
public class TouchedFileLoggingTests : CSharpTestBase
public class TouchedFileLoggingTests : CommandLineTestBase
{
private static readonly string s_libDirectory = Environment.GetEnvironmentVariable("LIB");
private readonly string _baseDirectory = TempRoot.Root;
private const string helloWorldCS = @"using System;
class C
......@@ -36,7 +35,7 @@ public void TrivialSourceFileOnlyCsc()
var touchedDir = Temp.CreateDirectory();
var touchedBase = Path.Combine(touchedDir.Path, "touched");
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[] { "/nologo", hello,
var cmd = CreateCSharpCompiler(new[] { "/nologo", hello,
string.Format(@"/touchedfiles:""{0}""", touchedBase) });
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
......@@ -77,7 +76,7 @@ public void AppConfigCsc()
var net4_0dll = Temp.CreateFile().WriteAllBytes(TestResources.NetFX.v4_0_30319.System).Path;
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var cmd = new MockCSharpCompiler(null, _baseDirectory,
var cmd = CreateCSharpCompiler(
new[] { "/nologo",
"/r:" + silverlight,
"/r:" + net4_0dll,
......@@ -112,7 +111,7 @@ public void StrongNameKeyCsc()
var touchedBase = Path.Combine(touchedDir.Path, "touched");
var outWriter = new StringWriter(CultureInfo.InvariantCulture);
var cmd = new MockCSharpCompiler(null, _baseDirectory,
var cmd = CreateCSharpCompiler(
new[] { "/nologo",
"/touchedfiles:" + touchedBase,
"/keyfile:" + snkPath,
......@@ -150,7 +149,7 @@ public class C { }").Path;
var touchedDir = Temp.CreateDirectory();
var touchedBase = Path.Combine(touchedDir.Path, "touched");
var cmd = new MockCSharpCompiler(null, _baseDirectory, new[]
var cmd = CreateCSharpCompiler(new[]
{
"/nologo",
"/target:library",
......
......@@ -12,7 +12,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Test.Utilities
{
internal class MockCSharpCompiler : CSharpCompiler
{
protected readonly ImmutableArray<DiagnosticAnalyzer> _analyzers;
private readonly ImmutableArray<DiagnosticAnalyzer> _analyzers;
internal Compilation Compilation;
public MockCSharpCompiler(string responseFile, string workingDirectory, string[] args, ImmutableArray<DiagnosticAnalyzer> analyzers = default, AnalyzerAssemblyLoader loader = null)
......@@ -26,7 +26,7 @@ public MockCSharpCompiler(string responseFile, BuildPaths buildPaths, string[] a
_analyzers = !analyzers.IsDefault ? analyzers : ImmutableArray<DiagnosticAnalyzer>.Empty;
}
private static BuildPaths CreateBuildPaths(string workingDirectory) => RuntimeUtilities.CreateBuildPaths(workingDirectory);
private static BuildPaths CreateBuildPaths(string workingDirectory, string sdkDirectory = null) => RuntimeUtilities.CreateBuildPaths(workingDirectory, sdkDirectory);
protected override ImmutableArray<DiagnosticAnalyzer> ResolveAnalyzersFromArguments(
List<DiagnosticInfo> diagnostics,
......
......@@ -35,7 +35,7 @@ Friend Class MockVisualBasicCompiler
End Sub
Private Shared Function CreateBuildPaths(workingDirectory As String, tempDirectory As String) As BuildPaths
Return RuntimeUtilities.CreateBuildPaths(workingDirectory, tempDirectory)
Return RuntimeUtilities.CreateBuildPaths(workingDirectory, tempDirectory:=tempDirectory)
End Function
Protected Overrides Function ResolveAnalyzersFromArguments(
......
......@@ -13,20 +13,20 @@ namespace Microsoft.CodeAnalysis.Test.Utilities
/// </summary>
public static partial class RuntimeUtilities
{
internal static BuildPaths CreateBuildPaths(string workingDirectory, string tempDirectory = null)
internal static BuildPaths CreateBuildPaths(string workingDirectory, string sdkDirectory = null, string tempDirectory = null)
{
tempDirectory = tempDirectory ?? Path.GetTempPath();
#if NET46
return new BuildPaths(
clientDir: Path.GetDirectoryName(typeof(BuildPathsUtil).Assembly.Location),
workingDir: workingDirectory,
sdkDir: RuntimeEnvironment.GetRuntimeDirectory(),
sdkDir: sdkDirectory ?? RuntimeEnvironment.GetRuntimeDirectory(),
tempDir: tempDirectory);
#else
return new BuildPaths(
clientDir: AppContext.BaseDirectory,
workingDir: workingDirectory,
sdkDir: null,
sdkDir: sdkDirectory,
tempDir: tempDirectory);
#endif
}
......@@ -48,8 +48,12 @@ internal static AnalyzerAssemblyLoader CreateAnalyzerAssemblyLoader()
{
#if NET46
return new DesktopAnalyzerAssemblyLoader();
#else
#elif NETCOREAPP2_1
return new CoreClrAnalyzerAssemblyLoader();
#elif NETSTANDARD1_3
return new ThrowingAnalyzerAssemblyLoader();
#else
#error Unsupported configuration
#endif
}
......
......@@ -66,6 +66,7 @@
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.Workspaces.UnitTests" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\Compilers\Shared\CoreClrAnalyzerAssemblyLoader.cs" Link="CoreClrAnalyzerAssemblyLoader.cs" />
<Compile Include="..\..\..\Compilers\Shared\DesktopAnalyzerAssemblyLoader.cs">
<Link>DesktopAnalyzerAssemblyLoader.cs</Link>
</Compile>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册