move Mono msbuild test hack to C# code

pull/23174/head
Jan Tattermusch 5 years ago
parent b19f287144
commit 47ae4cd5d9
  1. 32
      src/csharp/Grpc.Tools.Tests/Grpc.Tools.Tests.csproj
  2. 63
      src/csharp/Grpc.Tools.Tests/MsBuildAssemblyHelper.cs
  3. 9
      src/csharp/Grpc.Tools.Tests/NUnitMain.cs

@ -37,36 +37,4 @@
<PackageReference Include="Microsoft.Build.Framework; Microsoft.Build.Utilities.Core" Version="15.6.*" /> <PackageReference Include="Microsoft.Build.Framework; Microsoft.Build.Utilities.Core" Version="15.6.*" />
</ItemGroup> </ItemGroup>
<!-- Groups below is a hack to allow the test to run under Mono Framework build.
========================================================================== -->
<!-- Mono unfortunately comes with broken Microsoft.Build.* assemblies installed in
the GAC, but fortunately searches for runtime assemblies in a different order
than Windows CLR host: the GAC assemblies have the lowest search priority, (see
https://www.mono-project.com/docs/advanced/assemblies-and-the-gac/), not the
highest as is in Windows (documented at
https://docs.microsoft.com/dotnet/framework/deployment/how-the-runtime-locates-assemblies).
To run the tests under Mono, we need correct assemblies in the same directory as
the test executable. Correct versions are in the MSBuild directory under Mono. -->
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' and '$(OS)' != 'Windows_NT' ">
<None Include="$(_MSBuildAssemblyPath)/Microsoft.Build.Framework.dll;
$(_MSBuildAssemblyPath)/Microsoft.Build.Utilities.v4.0.dll;
$(_MSBuildAssemblyPath)/Microsoft.Build.Utilities.Core.dll"
CopyToOutputDirectory="Always" Visible="false" />
</ItemGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' and '$(OS)' != 'Windows_NT' ">
<!-- The None items are included into assembly candidate resolution by default, and
we do not want that, as they are not valid as reference assemblies (the version of
Microsoft.Build.Utilities.v4.0 is a pure facade for Microsoft.Build.Utilities.Core,
and does not define any types at all). Exclude them from assembly resolution. See
https://github.com/Microsoft/msbuild/blob/50639058f/documentation/wiki/ResolveAssemblyReference.md -->
<AssemblySearchPaths>{HintPathFromItem};{TargetFrameworkDirectory};{RawFileName}</AssemblySearchPaths>
<!-- Mono knows better where its MSBuild is. -->
<_MSBuildAssemblyPath Condition=" '$(MSBuildRuntimeType)' != 'Core' "
>$(MSBuildToolsPath)</_MSBuildAssemblyPath>
<!-- Under dotnet, make the best guess we can. -->
<_MSBuildAssemblyPath Condition=" '$(MSBuildRuntimeType)' == 'Core' "
>$(FrameworkPathOverride)/../msbuild/$(MSBuildToolsVersion)/bin</_MSBuildAssemblyPath>
</PropertyGroup>
</Project> </Project>

@ -0,0 +1,63 @@
#region Copyright notice and license
// Copyright 2018 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System.Reflection;
using NUnitLite;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Grpc.Tools.Tests
{
static class MsBuildAssemblyHelper
{
[DllImport("__Internal")]
extern static void mono_set_assemblies_path(string path);
public static void TweakAssemblyPathIfOnMono()
{
// Below is a hack to allow the tests to run under Mono Framework build.
// Mono unfortunately comes with broken Microsoft.Build.* assemblies installed in
// the GAC, so we need to tweak the assembly search path to make sure the right
// msbuild assemblies are loaded (and the tests work).
#if NET45
// only run this under .NET framework; under mono
bool isMono = Type.GetType("Mono.Runtime") != null;
if (isMono)
{
var mscorlibDir = Path.GetDirectoryName(typeof(Array).Assembly.Location);
// Construct the location of MsBuild assemblies from the location of mscorlib assembly.
var msbuildToolPath = Path.Combine(mscorlibDir, "..", "msbuild", "Current", "bin");
// To make sure we've constructed the right path, make sure the assemblies we're interested
// in are there.
foreach(var assemblyName in new [] {"Microsoft.Build.Framework.dll", "Microsoft.Build.Utilities.v4.0.dll", "Microsoft.Build.Utilities.Core.dll"})
{
if (!File.Exists(Path.Combine(msbuildToolPath, assemblyName)))
{
throw new InvalidOperationException($"Could not locate assembly {assemblyName} under {msbuildToolPath}");
}
}
// Normally the assembly search path can be changed by MONO_PATH environment variable, but it needs to be done
// before the process starts. The following internal method allows us to do the same thing.
mono_set_assemblies_path(msbuildToolPath);
}
#endif
}
}
}

@ -23,7 +23,10 @@ namespace Grpc.Tools.Tests
{ {
static class NUnitMain static class NUnitMain
{ {
public static int Main(string[] args) => public static int Main(string[] args)
new AutoRun(typeof(NUnitMain).GetTypeInfo().Assembly).Execute(args); {
}; MsBuildAssemblyHelper.TweakAssemblyPathIfOnMono();
return new AutoRun(typeof(NUnitMain).GetTypeInfo().Assembly).Execute(args);
}
}
} }

Loading…
Cancel
Save