From e106404ae22113ec2561658ce31cbadc139c537b Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Thu, 27 Dec 2018 00:05:01 -0300 Subject: [PATCH] Switch to Microsoft.Build.Locator and AssemblyAttribute metadata --- src/Moq/Moq.Sdk.Tests/Moq.Sdk.Tests.csproj | 2 - src/Packages.props | 3 +- .../Stunts.Internal/DocumentExtensions.cs | 3 + src/Testing/MockBuildEngine.cs | 2 +- src/Testing/ModuleInitializer.cs | 29 +++++++++ src/Testing/Moq.Testing.projitems | 1 + src/Testing/TestHelpers.cs | 14 +---- src/Testing/TestOutputLogger.cs | 2 +- src/build/PackageReferences.targets | 6 +- src/build/Settings.Tests.props | 1 - src/build/Settings.Tests.targets | 59 +++++-------------- 11 files changed, 58 insertions(+), 64 deletions(-) create mode 100644 src/Testing/ModuleInitializer.cs diff --git a/src/Moq/Moq.Sdk.Tests/Moq.Sdk.Tests.csproj b/src/Moq/Moq.Sdk.Tests/Moq.Sdk.Tests.csproj index e5287085..d9e5162b 100644 --- a/src/Moq/Moq.Sdk.Tests/Moq.Sdk.Tests.csproj +++ b/src/Moq/Moq.Sdk.Tests/Moq.Sdk.Tests.csproj @@ -8,8 +8,6 @@ false - - false diff --git a/src/Packages.props b/src/Packages.props index e9e4dd8f..afa2b8c2 100644 --- a/src/Packages.props +++ b/src/Packages.props @@ -4,7 +4,7 @@ - + @@ -13,6 +13,7 @@ + diff --git a/src/Stunts/Stunts.Internal/DocumentExtensions.cs b/src/Stunts/Stunts.Internal/DocumentExtensions.cs index 3e6504f1..c7b0cdf5 100644 --- a/src/Stunts/Stunts.Internal/DocumentExtensions.cs +++ b/src/Stunts/Stunts.Internal/DocumentExtensions.cs @@ -46,6 +46,9 @@ public static async Task ApplyCodeFixAsync(this Document document, str ApplyChangesOperation operation; if ((operation = operations.OfType().FirstOrDefault()) != null) { + // According to https://github.com/DotNetAnalyzers/StyleCopAnalyzers/pull/935 and + // https://github.com/dotnet/roslyn-sdk/issues/140, Sam Harwell mentioned that we should + // be forcing a re-parse of the document syntax tree at this point. document = await operation.ChangedSolution.GetDocument(document.Id).RecreateDocumentAsync(cancellationToken); // Retrieve the codefixes for the updated doc again. codeFixes = await GetCodeFixes(document, codeFixName, analyzers, cancellationToken).ConfigureAwait(false); diff --git a/src/Testing/MockBuildEngine.cs b/src/Testing/MockBuildEngine.cs index 0c6628d2..7ea2c844 100644 --- a/src/Testing/MockBuildEngine.cs +++ b/src/Testing/MockBuildEngine.cs @@ -9,7 +9,7 @@ /// Fake build engine for unit testing Tasks without spinning up /// MSBuild. /// -public class MockBuildEngine : IBuildEngine +class MockBuildEngine : IBuildEngine { bool trace = false; ITestOutputHelper output; diff --git a/src/Testing/ModuleInitializer.cs b/src/Testing/ModuleInitializer.cs new file mode 100644 index 00000000..482fc474 --- /dev/null +++ b/src/Testing/ModuleInitializer.cs @@ -0,0 +1,29 @@ +using System; +using System.Linq; +/// +/// Used by the InjectModuleInitializer. All code inside the Run method is ran as soon as the assembly is loaded. +/// +internal static partial class ModuleInitializer +{ + /// + /// Initializes the module. + /// + internal static void Run() + { + if (!AppDomain.CurrentDomain.GetAssemblies().Any(x => x.GetName().Name == "Microsoft.Build")) + { +#pragma warning disable 0436 + Microsoft.Build.Locator.MSBuildLocator.RegisterMSBuildPath(ThisAssembly.Metadata.MSBuildBinPath); + Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", ThisAssembly.Metadata.MSBuildBinPath, EnvironmentVariableTarget.Process); +#pragma warning restore 0436 + } + + OnRun(); + } + + /// + /// Declare in another partial class to extend what is run when the assembly is + /// loaded. + /// + static partial void OnRun(); +} \ No newline at end of file diff --git a/src/Testing/Moq.Testing.projitems b/src/Testing/Moq.Testing.projitems index 96e11ff5..2633d067 100644 --- a/src/Testing/Moq.Testing.projitems +++ b/src/Testing/Moq.Testing.projitems @@ -11,6 +11,7 @@ + diff --git a/src/Testing/TestHelpers.cs b/src/Testing/TestHelpers.cs index e1b8893d..7eaa7610 100644 --- a/src/Testing/TestHelpers.cs +++ b/src/Testing/TestHelpers.cs @@ -34,7 +34,6 @@ public static ProjectInfo CreateProjectInfo(string language, string assemblyName (ParseOptions)new VisualBasicParseOptions(Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Latest); //The location of the .NET assemblies - var frameworkPath = Path.GetDirectoryName(typeof(object).Assembly.Location); var netstandardPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), @".nuget\packages\NETStandard.Library\2.0.0\build\netstandard2.0\ref"); if (!Directory.Exists(netstandardPath)) netstandardPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"dotnet\sdk\NuGetFallbackFolder\netstandard.library\2.0.0\build\netstandard2.0\ref"); @@ -45,17 +44,10 @@ public static ProjectInfo CreateProjectInfo(string language, string assemblyName throw new InvalidOperationException("Failed to find location of .NETStandard 2.0 reference assemblies"); var referencePaths = - //new[] - //{ - // typeof(object).Assembly.Location, - // typeof(Enumerable).Assembly.Location, - // typeof(CSharpCompilation).Assembly.Location, - // typeof(Compilation).Assembly.Location, - //} Directory.EnumerateFiles(netstandardPath, "*.dll") - .Concat(ReferencePaths.Paths) - //ReferencePaths.Paths - //.Concat(Directory.EnumerateFiles(netstandardPath, "*.dll")) +#pragma warning disable CS0436 // Type conflicts with imported type + .Concat(ThisAssembly.Metadata.ReferencePaths.Split('|')) +#pragma warning restore CS0436 // Type conflicts with imported type .Where(path => !string.IsNullOrEmpty(path) && File.Exists(path)) .Distinct(FileNameEqualityComparer.Default); diff --git a/src/Testing/TestOutputLogger.cs b/src/Testing/TestOutputLogger.cs index 5f71071d..6fd4dad7 100644 --- a/src/Testing/TestOutputLogger.cs +++ b/src/Testing/TestOutputLogger.cs @@ -5,7 +5,7 @@ /// /// xunit logger > MSBuild logger /// -public class TestOutputLogger : ILogger +class TestOutputLogger : ILogger { ITestOutputHelper output; diff --git a/src/build/PackageReferences.targets b/src/build/PackageReferences.targets index aa913fb9..0d32cbdb 100644 --- a/src/build/PackageReferences.targets +++ b/src/build/PackageReferences.targets @@ -20,10 +20,8 @@ - - - - + + diff --git a/src/build/Settings.Tests.props b/src/build/Settings.Tests.props index e44fbc0f..dc71ea9a 100644 --- a/src/build/Settings.Tests.props +++ b/src/build/Settings.Tests.props @@ -1,7 +1,6 @@ - $(OutputPath)UnitTests\$(MSBuildProjectName)\ 1701;1702;1705;0436 true diff --git a/src/build/Settings.Tests.targets b/src/build/Settings.Tests.targets index 5b77fc16..2c8f46ef 100644 --- a/src/build/Settings.Tests.targets +++ b/src/build/Settings.Tests.targets @@ -1,11 +1,4 @@ - - $(IntermediateOutputPath)ReferencePaths.g.cs - - $(CoreCompileDependsOn); - GenerateReferencePaths; - - @@ -14,6 +7,13 @@ + + + <_Parameter1>MSBuildBinPath + <_Parameter2>$(MSBuildBinPath) + + + @@ -28,49 +28,22 @@ - - - $(CoreCompileDependsOn); - GenerateReferencePaths - - - - + - - - - - - - + + <_FilteredReferencePath Include="@(ReferencePath)" Condition="'%(ReferencePath.FrameworkFile)' == 'true'" /> + <_FilteredReferencePath Include="@(TargetPathWithTargetPlatformMoniker)" /> + - - + + <_Parameter1>ReferencePaths + <_Parameter2>@(_FilteredReferencePath -> '%(FullPath)', '|') +