Skip to content

Commit

Permalink
Ensure we load the preview SDK during testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeRobich committed Apr 10, 2022
1 parent e9d6683 commit 7b62a5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
31 changes: 24 additions & 7 deletions tests/OmniSharp.MSBuild.Tests/AbstractMSBuildTestFixture.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,26 +12,42 @@

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)
: base(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<string, string>
{
["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<ExportDescriptorProvider> additionalExports = null,
protected OmniSharpTestHost CreateMSBuildTestHost(string path, IEnumerable<ExportDescriptorProvider> 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);
Expand Down
7 changes: 1 addition & 6 deletions tests/OmniSharp.MSBuild.Tests/ProjectLoadListenerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
{
["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);
}
Expand Down

0 comments on commit 7b62a5d

Please sign in to comment.