diff --git a/tests/OmniSharp.MSBuild.Tests/AbstractMSBuildTestFixture.cs b/tests/OmniSharp.MSBuild.Tests/AbstractMSBuildTestFixture.cs index 944daf3338..37c1cdc6d2 100644 --- a/tests/OmniSharp.MSBuild.Tests/AbstractMSBuildTestFixture.cs +++ b/tests/OmniSharp.MSBuild.Tests/AbstractMSBuildTestFixture.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Composition.Hosting.Core; using Microsoft.CodeAnalysis; using Microsoft.Extensions.Configuration; @@ -11,9 +12,10 @@ namespace OmniSharp.MSBuild.Tests { - public abstract class AbstractMSBuildTestFixture : AbstractTestFixture + public abstract class AbstractMSBuildTestFixture : AbstractTestFixture, IDisposable { private readonly IAssemblyLoader _assemblyLoader; + private readonly IMSBuildLocator _msbuildLocator; private readonly IAnalyzerAssemblyLoader _analyzerAssemblyLoader; public AbstractMSBuildTestFixture(ITestOutputHelper output) @@ -21,16 +23,31 @@ public AbstractMSBuildTestFixture(ITestOutputHelper output) { _assemblyLoader = new AssemblyLoader(this.LoggerFactory); _analyzerAssemblyLoader = ShadowCopyAnalyzerAssemblyLoader.Instance; + + // Since we can only load MSBuild once into our process we need to include + // prerelease version so that our .NET 7 tests will pass. + var configuration = new Dictionary + { + ["sdk:IncludePrereleases"] = bool.TrueString + }.ToConfiguration(); + + _msbuildLocator = MSBuildLocator.CreateDefault(this.LoggerFactory, _assemblyLoader, configuration); + + // Some tests require MSBuild to be discovered early + // to ensure that the Microsoft.Build.* assemblies can be located + _msbuildLocator.RegisterDefaultInstance(this.LoggerFactory.CreateLogger("MSBuildTests"), dotNetInfo: null); + } + + public void Dispose() + { + (_msbuildLocator as IDisposable)?.Dispose(); } - protected OmniSharpTestHost CreateMSBuildTestHost( - string path, - IEnumerable additionalExports = null, + protected OmniSharpTestHost CreateMSBuildTestHost(string path, IEnumerable additionalExports = null, IConfiguration configurationData = null) { var environment = new OmniSharpEnvironment(path, logLevel: LogLevel.Trace); - using var msbuildLocator = MSBuildLocator.CreateDefault(this.LoggerFactory, _assemblyLoader, configurationData); - var serviceProvider = TestServiceProvider.Create(this.TestOutput, environment, this.LoggerFactory, _assemblyLoader, _analyzerAssemblyLoader, msbuildLocator, + var serviceProvider = TestServiceProvider.Create(this.TestOutput, environment, this.LoggerFactory, _assemblyLoader, _analyzerAssemblyLoader, _msbuildLocator, configurationData); return OmniSharpTestHost.Create(serviceProvider, additionalExports); diff --git a/tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs b/tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs index 5736d6921d..3682f954c9 100644 --- a/tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs +++ b/tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs @@ -248,16 +248,11 @@ public async Task The_correct_sdk_version_is_emitted_NET6() [ConditionalFact(typeof(NonMonoRuntimeOnly))] public async Task The_correct_sdk_version_is_emitted_NET7() { - var configuration = new Dictionary - { - ["sdk:IncludePrereleases"] = bool.TrueString - }.ToConfiguration(); - // Arrange var emitter = new ProjectLoadTestEventEmitter(); using var testProject = await TestAssets.Instance.GetTestProjectAsync("Net70Project"); - using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory), configuration); + using var host = CreateMSBuildTestHost(testProject.Directory, emitter.AsExportDescriptionProvider(LoggerFactory)); Assert.Single(emitter.ReceivedMessages); Assert.Equal(GetHashedFileExtension("7.0.100-preview.2.22153.17"), emitter.ReceivedMessages[0].SdkVersion); }