Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 91 additions & 88 deletions src/Shared/UnitTests/TestAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,130 +19,133 @@
[assembly: AssemblyFixture(typeof(MSBuildTestEnvironmentFixture), LifetimeScope = AssemblyFixtureAttribute.Scope.Class)]
[assembly: AssemblyFixture(typeof(MSBuildTestEnvironmentFixture), LifetimeScope = AssemblyFixtureAttribute.Scope.Method)]

public class MSBuildTestAssemblyFixture : IDisposable
namespace Microsoft.Build.UnitTests
{
bool _disposed;
private TestEnvironment _testEnvironment;

public MSBuildTestAssemblyFixture()
public class MSBuildTestAssemblyFixture : IDisposable
{
// Set field to indicate tests are running in the TestInfo class in Microsoft.Build.Framework.
// See the comments on the TestInfo class for an explanation of why it works this way.
var frameworkAssembly = typeof(Microsoft.Build.Framework.ITask).Assembly;
var testInfoType = frameworkAssembly.GetType("Microsoft.Build.Framework.TestInfo");
var runningTestsField = testInfoType.GetField("s_runningTests", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
runningTestsField.SetValue(null, true);
bool _disposed;
private TestEnvironment _testEnvironment;

_testEnvironment = TestEnvironment.Create();
public MSBuildTestAssemblyFixture()
{
// Set field to indicate tests are running in the TestInfo class in Microsoft.Build.Framework.
// See the comments on the TestInfo class for an explanation of why it works this way.
var frameworkAssembly = typeof(Microsoft.Build.Framework.ITask).Assembly;
var testInfoType = frameworkAssembly.GetType("Microsoft.Build.Framework.TestInfo");
var runningTestsField = testInfoType.GetField("s_runningTests", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
runningTestsField.SetValue(null, true);

_testEnvironment.DoNotLaunchDebugger();
_testEnvironment = TestEnvironment.Create();

// Reset the VisualStudioVersion environment variable. This will be set if tests are run from a VS command prompt. However,
// if the environment variable is set, it will interfere with tests which set the SubToolsetVersion
// (VerifySubToolsetVersionSetByConstructorOverridable), as the environment variable would take precedence.
_testEnvironment.SetEnvironmentVariable("VisualStudioVersion", string.Empty);
_testEnvironment.DoNotLaunchDebugger();

// Prevent test assemblies from logging any performance info.
// https://github.com/dotnet/msbuild/pull/6274
_testEnvironment.SetEnvironmentVariable("DOTNET_PERFLOG_DIR", string.Empty);
// Reset the VisualStudioVersion environment variable. This will be set if tests are run from a VS command prompt. However,
// if the environment variable is set, it will interfere with tests which set the SubToolsetVersion
// (VerifySubToolsetVersionSetByConstructorOverridable), as the environment variable would take precedence.
_testEnvironment.SetEnvironmentVariable("VisualStudioVersion", string.Empty);

SetDotnetHostPath(_testEnvironment);
// Prevent test assemblies from logging any performance info.
// https://github.com/dotnet/msbuild/pull/6274
_testEnvironment.SetEnvironmentVariable("DOTNET_PERFLOG_DIR", string.Empty);

// Use a project-specific temporary path
// This is so multiple test projects can be run in parallel without sharing the same temp directory
var subdirectory = Path.GetRandomFileName();
SetDotnetHostPath(_testEnvironment);

string newTempPath = Path.Combine(Path.GetTempPath(), subdirectory);
var assemblyTempFolder = _testEnvironment.CreateFolder(newTempPath);
// Use a project-specific temporary path
// This is so multiple test projects can be run in parallel without sharing the same temp directory
var subdirectory = Path.GetRandomFileName();

_testEnvironment.SetTempPath(assemblyTempFolder.Path);
string newTempPath = Path.Combine(Path.GetTempPath(), subdirectory);
var assemblyTempFolder = _testEnvironment.CreateFolder(newTempPath);

_testEnvironment.CreateFile(
transientTestFolder: assemblyTempFolder,
fileName: "MSBuild_Tests.txt",
contents: $"Temporary test folder for tests from {AppContext.BaseDirectory}");
_testEnvironment.SetTempPath(assemblyTempFolder.Path);

// Ensure that we stop looking for a D.B.rsp at the root of the test temp
_testEnvironment.CreateFile(
transientTestFolder: assemblyTempFolder,
fileName: "Directory.Build.rsp",
contents: string.Empty);
_testEnvironment.CreateFile(
transientTestFolder: assemblyTempFolder,
fileName: "MSBuild_Tests.txt",
contents: $"Temporary test folder for tests from {AppContext.BaseDirectory}");

_testEnvironment.CreateFile(
transientTestFolder: assemblyTempFolder,
fileName: "Directory.Build.props",
contents: "<Project />");
// Ensure that we stop looking for a D.B.rsp at the root of the test temp
_testEnvironment.CreateFile(
transientTestFolder: assemblyTempFolder,
fileName: "Directory.Build.rsp",
contents: string.Empty);

_testEnvironment.CreateFile(
transientTestFolder: assemblyTempFolder,
fileName: "Directory.Build.targets",
contents: "<Project />");
}
_testEnvironment.CreateFile(
transientTestFolder: assemblyTempFolder,
fileName: "Directory.Build.props",
contents: "<Project />");

/// <summary>
/// Find correct version of "dotnet", and set DOTNET_HOST_PATH so that the Roslyn tasks will use the right host
/// </summary>
/// <param name="testEnvironment"></param>
private static void SetDotnetHostPath(TestEnvironment testEnvironment)
{
var currentFolder = AppContext.BaseDirectory;
_testEnvironment.CreateFile(
transientTestFolder: assemblyTempFolder,
fileName: "Directory.Build.targets",
contents: "<Project />");
}

while (currentFolder != null)
/// <summary>
/// Find correct version of "dotnet", and set DOTNET_HOST_PATH so that the Roslyn tasks will use the right host
/// </summary>
/// <param name="testEnvironment"></param>
private static void SetDotnetHostPath(TestEnvironment testEnvironment)
{
string potentialVersionsPropsPath = Path.Combine(currentFolder, "build", "Versions.props");
if (FileSystems.Default.FileExists(potentialVersionsPropsPath))
var currentFolder = AppContext.BaseDirectory;

while (currentFolder != null)
{
var doc = XDocument.Load(potentialVersionsPropsPath);
var ns = doc.Root.Name.Namespace;
var cliVersionElement = doc.Root.Elements(ns + "PropertyGroup").Elements(ns + "DotNetCliVersion").FirstOrDefault();
if (cliVersionElement != null)
string potentialVersionsPropsPath = Path.Combine(currentFolder, "build", "Versions.props");
if (FileSystems.Default.FileExists(potentialVersionsPropsPath))
{
string cliVersion = cliVersionElement.Value;
string dotnetPath = Path.Combine(currentFolder, "artifacts", ".dotnet", cliVersion, "dotnet");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
var doc = XDocument.Load(potentialVersionsPropsPath);
var ns = doc.Root.Name.Namespace;
var cliVersionElement = doc.Root.Elements(ns + "PropertyGroup").Elements(ns + "DotNetCliVersion").FirstOrDefault();
if (cliVersionElement != null)
{
dotnetPath += ".exe";
string cliVersion = cliVersionElement.Value;
string dotnetPath = Path.Combine(currentFolder, "artifacts", ".dotnet", cliVersion, "dotnet");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
dotnetPath += ".exe";
}

testEnvironment.SetEnvironmentVariable("DOTNET_HOST_PATH", dotnetPath);
}

testEnvironment.SetEnvironmentVariable("DOTNET_HOST_PATH", dotnetPath);
break;
}

break;
currentFolder = Directory.GetParent(currentFolder)?.FullName;
}

currentFolder = Directory.GetParent(currentFolder)?.FullName;
}
}

public void Dispose()
{
if (!_disposed)
public void Dispose()
{
_testEnvironment.Dispose();
if (!_disposed)
{
_testEnvironment.Dispose();

_disposed = true;
_disposed = true;
}
}
}
}

public class MSBuildTestEnvironmentFixture : IDisposable
{
bool _disposed;
private TestEnvironment _testEnvironment;

public MSBuildTestEnvironmentFixture()
public class MSBuildTestEnvironmentFixture : IDisposable
{
_testEnvironment = TestEnvironment.Create();
}
bool _disposed;
private TestEnvironment _testEnvironment;

public void Dispose()
{
if (!_disposed)
public MSBuildTestEnvironmentFixture()
{
_testEnvironment = TestEnvironment.Create();
}

public void Dispose()
{
_testEnvironment.Dispose();
if (!_disposed)
{
_testEnvironment.Dispose();

_disposed = true;
_disposed = true;
}
}
}
}