forked from dotnet/msbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a couple of tests around loading assembly dependencies
Related to dotnet#658. This change introduced a couple of basic tests to verify that we can find a task assembly's dependencies when they are located in the same directory. In order to do this, it adds a couple of projects to produce assemblies just for this test: TaskWithDependency.dll, and Dependency.dll.
- Loading branch information
Showing
14 changed files
with
499 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
|
||
using System; | ||
using System.IO; | ||
using Microsoft.Build.Shared; | ||
using Microsoft.Build.SharedUtilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.Build.UnitTests | ||
{ | ||
public class TypeLoader_Dependencies_Tests | ||
{ | ||
private static readonly string ProjectFilePath = Path.Combine(FileUtilities.CurrentExecutableDirectory, "TaskWithDependencyTest.proj"); | ||
private static readonly string TaskDllFileName = "TaskWithDependency.dll"; | ||
private static readonly string OriginalTaskDllPath = Path.Combine(FileUtilities.CurrentExecutableDirectory, TaskDllFileName); | ||
private static readonly string DependencyDllFileName = "Dependency.dll"; | ||
private static readonly string OriginalDependencyDllPath = Path.Combine(FileUtilities.CurrentExecutableDirectory, DependencyDllFileName); | ||
|
||
[Fact] | ||
public void LoadAssemblyAndDependency_Inside() | ||
{ | ||
bool successfulExit; | ||
string output = RunnerUtilities.ExecMSBuild(ProjectFilePath + " /v:diag", out successfulExit); | ||
Assert.True(successfulExit); | ||
|
||
CheckIfCorrectAssemblyLoaded(output, OriginalTaskDllPath); | ||
} | ||
|
||
[Fact] | ||
public void LoadAssemblyAndDependency_Outside() | ||
{ | ||
string tempDir = MoveOrCopyDllsToTempDir(copy: false); | ||
var newTaskDllPath = Path.Combine(tempDir, TaskDllFileName); | ||
|
||
try | ||
{ | ||
bool successfulExit; | ||
string output = RunnerUtilities.ExecMSBuild(ProjectFilePath + " /v:diag /p:AssemblyPath=" + newTaskDllPath, out successfulExit); | ||
Assert.True(successfulExit); | ||
|
||
CheckIfCorrectAssemblyLoaded(output, newTaskDllPath); | ||
} | ||
finally | ||
{ | ||
UndoDLLOperations(tempDir, moveBack: true); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// </summary> | ||
/// <param name="copy"></param> | ||
/// <returns>Path to temporary directory</returns> | ||
private string MoveOrCopyDllsToTempDir(bool copy) | ||
{ | ||
var temporaryDirectory = FileUtilities.GetTemporaryDirectory(); | ||
|
||
var newTaskDllPath = Path.Combine(temporaryDirectory, TaskDllFileName); | ||
var newDependencyDllPath = Path.Combine(temporaryDirectory, DependencyDllFileName); | ||
|
||
Assert.True(File.Exists(OriginalTaskDllPath)); | ||
Assert.True(File.Exists(OriginalDependencyDllPath)); | ||
|
||
if (copy) | ||
{ | ||
File.Copy(OriginalTaskDllPath, newTaskDllPath); | ||
File.Copy(OriginalDependencyDllPath, newDependencyDllPath); | ||
|
||
Assert.True(File.Exists(newTaskDllPath)); | ||
Assert.True(File.Exists(newDependencyDllPath)); | ||
} | ||
else | ||
{ | ||
File.Move(OriginalTaskDllPath, newTaskDllPath); | ||
File.Move(OriginalDependencyDllPath, newDependencyDllPath); | ||
|
||
|
||
Assert.True(File.Exists(newTaskDllPath)); | ||
Assert.True(File.Exists(newDependencyDllPath)); | ||
Assert.False(File.Exists(OriginalTaskDllPath)); | ||
Assert.False(File.Exists(OriginalDependencyDllPath)); | ||
} | ||
|
||
return temporaryDirectory; | ||
} | ||
|
||
/// <summary> | ||
/// Move / Delete newDllPath and delete temp directory | ||
/// </summary> | ||
/// <param name="newDllPath"></param> | ||
/// <param name="moveBack">If true, move newDllPath back to bin. If false, delete it</param> | ||
private void UndoDLLOperations(string tempDirectoryPath, bool moveBack) | ||
{ | ||
var currentTaskDllPath = Path.Combine(tempDirectoryPath, TaskDllFileName); | ||
var currentDependencyDllPath = Path.Combine(tempDirectoryPath, DependencyDllFileName); | ||
|
||
if (moveBack) | ||
{ | ||
File.Move(currentTaskDllPath, OriginalTaskDllPath); | ||
File.Move(currentDependencyDllPath, OriginalDependencyDllPath); | ||
} | ||
else | ||
{ | ||
File.Delete(currentTaskDllPath); | ||
File.Delete(currentDependencyDllPath); | ||
|
||
} | ||
|
||
Assert.True(File.Exists(OriginalTaskDllPath)); | ||
Assert.True(File.Exists(OriginalDependencyDllPath)); | ||
|
||
|
||
Assert.False(File.Exists(currentTaskDllPath)); | ||
Assert.False(File.Exists(currentDependencyDllPath)); | ||
|
||
Assert.Empty(Directory.EnumerateFiles(tempDirectoryPath)); | ||
|
||
Directory.Delete(tempDirectoryPath); | ||
Assert.False(Directory.Exists(tempDirectoryPath)); | ||
} | ||
|
||
private void CheckIfCorrectAssemblyLoaded(string scriptOutput, string expectedAssemblyPath, bool expectedSuccess = true) | ||
{ | ||
var successfulMessage = @"Using ""LogStringFromDependency"" task from assembly """ + expectedAssemblyPath + @"""."; | ||
|
||
if (expectedSuccess) | ||
{ | ||
Assert.Contains(successfulMessage, scriptOutput, StringComparison.OrdinalIgnoreCase); | ||
} | ||
else | ||
{ | ||
Assert.DoesNotContain(successfulMessage, scriptOutput, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Dependency | ||
{ | ||
public class Alpha | ||
{ | ||
public static string GetString() | ||
{ | ||
return nameof(Alpha) + "." + nameof(GetString); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="..\..\..\dir.props" /> | ||
<PropertyGroup> | ||
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion> | ||
<ProjectGuid>{9EAE36C3-50CD-49A6-9CA2-94649125DCD1}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Dependency</RootNamespace> | ||
<AssemblyName>Dependency</AssemblyName> | ||
<DefaultLanguage>en-US</DefaultLanguage> | ||
<UseProductOutputPath>true</UseProductOutputPath> | ||
<CopyNuGetImplementations>false</CopyNuGetImplementations> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(NetCoreBuild)' == 'true'"> | ||
<StartAction Condition="'$(StartAction)'==''">Program</StartAction> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include="project.json" /> | ||
<!-- A reference to the entire .NET Framework is automatically included --> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Alpha.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
</ItemGroup> | ||
<Import Project="..\..\..\dir.targets" /> | ||
<!-- OSS sign assembly after compile runs. --> | ||
<Import Condition="Exists('$(ToolsDir)\sign.targets')" Project="$(ToolsDir)\sign.targets" /> | ||
</Project> |
Oops, something went wrong.