Skip to content

Commit

Permalink
use less brittle TFM logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarolf committed Nov 10, 2021
1 parent 8a97255 commit c404d64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
25 changes: 7 additions & 18 deletions src/Workspaces/MSBuildTest/DotNetSdkMSBuildInstalled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,44 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using Microsoft.Build.Locator;
using Roslyn.Test.Utilities;

namespace Microsoft.CodeAnalysis.MSBuild.UnitTests
{
internal partial class DotNetSdkMSBuildInstalled : ExecutionCondition
{
#if NETCOREAPP3_1_OR_GREATER
private static readonly VisualStudioInstance? s_instance;
private readonly Version _minimumVersion;

static DotNetSdkMSBuildInstalled()
{
s_instance = MSBuildLocator.QueryVisualStudioInstances()
.OrderByDescending(instances => instances.Version)
.FirstOrDefault();

if (s_instance != null && !MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterInstance(s_instance);
}
}

#if NET6_0
private readonly Version _minimumVersion;
=> s_instance = MSBuildLocator.RegisterDefaults();
#endif

public DotNetSdkMSBuildInstalled()
#if NET6_0
#if NETCOREAPP3_1_OR_GREATER
: this(new Version(6, 0))
#endif
{
}

#if NET6_0
#if NETCOREAPP3_1_OR_GREATER
internal DotNetSdkMSBuildInstalled(Version minimumVersion)
{
_minimumVersion = minimumVersion;
}
#endif

public override bool ShouldSkip
#if NET6_0
#if NETCOREAPP3_1_OR_GREATER
=> s_instance is null || s_instance.Version < _minimumVersion;
#else
=> true;
#endif

public override string SkipReason
#if NET6_0
#if NETCOREAPP3_1_OR_GREATER
=> $"Could not locate .NET SDK {_minimumVersion} or higher installed.";
#else
=> $"Test runs on .NET Core only.";
Expand Down
25 changes: 7 additions & 18 deletions src/Workspaces/MSBuildTest/VisualStudioMSBuildInstalled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,44 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using Microsoft.Build.Locator;
using Roslyn.Test.Utilities;

namespace Microsoft.CodeAnalysis.MSBuild.UnitTests
{
internal class VisualStudioMSBuildInstalled : ExecutionCondition
{
#if NET472_OR_GREATER
private static readonly VisualStudioInstance? s_instance;
private readonly Version _minimumVersion;

static VisualStudioMSBuildInstalled()
{
s_instance = MSBuildLocator.QueryVisualStudioInstances()
.OrderByDescending(instances => instances.Version)
.FirstOrDefault();

if (s_instance != null && !MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterInstance(s_instance);
}
}

#if !NET6_0
private readonly Version _minimumVersion;
=> s_instance = MSBuildLocator.RegisterDefaults();
#endif

public VisualStudioMSBuildInstalled()
#if !NET6_0
#if NET472_OR_GREATER
: this(new Version(16, 9))
#endif
{
}

#if !NET6_0
#if NET472_OR_GREATER
internal VisualStudioMSBuildInstalled(Version minimumVersion)
{
_minimumVersion = minimumVersion;
}
#endif

public override bool ShouldSkip
#if !NET6_0
#if NET472_OR_GREATER
=> s_instance is null || s_instance.Version < _minimumVersion;
#else
=> true;
#endif

public override string SkipReason
#if !NET6_0
#if NET472_OR_GREATER
=> $"Could not locate Visual Studio with MSBuild {_minimumVersion} or higher installed";
#else
=> $"Test runs on .NET Framework only.";
Expand Down

0 comments on commit c404d64

Please sign in to comment.