-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
OutputPathTests.cs
186 lines (141 loc) · 7.04 KB
/
OutputPathTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// 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.Evaluation;
using Microsoft.Build.Shared;
using Microsoft.Build.UnitTests;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.Build.Tasks.UnitTests
{
public sealed class OutputPathTests : IDisposable
{
private readonly ITestOutputHelper _output;
private readonly string _projectRelativePath = Path.Combine("src", "test", "test.csproj");
public OutputPathTests(ITestOutputHelper output)
{
_output = output;
ObjectModelHelpers.DeleteTempProjectDirectory();
}
public void Dispose()
{
ObjectModelHelpers.DeleteTempProjectDirectory();
}
/// <summary>
/// Test when both BaseOutputPath and OutputPath are not specified.
/// </summary>
[Fact]
public void BothBaseOutputPathAndOutputPathWereNotSpecified()
{
// Arrange
var baseOutputPath = "bin";
var projectFilePath = ObjectModelHelpers.CreateFileInTempProjectDirectory(_projectRelativePath,
$@"<Project DefaultTargets=`Build` xmlns=`msbuildnamespace` ToolsVersion=`msbuilddefaulttoolsversion`>
<Import Project=`$(MSBuildToolsPath)\Microsoft.Common.props`/>
<PropertyGroup>
<Platform>AnyCPU</Platform>
<Configuration>Debug</Configuration>
</PropertyGroup>
<Import Project=`$(MSBuildToolsPath)\Microsoft.Common.targets`/>
<Target Name=`Build`/>
</Project>");
// Act
Project project = ObjectModelHelpers.LoadProjectFileInTempProjectDirectory(projectFilePath, touchProject: false);
project.Build(new MockLogger(_output)).ShouldBeFalse();
// Assert
project.GetPropertyValue("BaseOutputPath").ShouldBe(baseOutputPath + '\\');
project.GetPropertyValue("BaseOutputPathWasSpecified").ShouldBe(string.Empty);
project.GetPropertyValue("_OutputPathWasMissing").ShouldBe("true");
}
/// <summary>
/// Test when BaseOutputPath is specified without the OutputPath.
/// </summary>
[Fact]
public void BaseOutputPathWasSpecifiedAndIsOverridable()
{
// Arrange
var baseOutputPath = Path.Combine("build", "bin");
var projectFilePath = ObjectModelHelpers.CreateFileInTempProjectDirectory(_projectRelativePath,
$@"<Project DefaultTargets=`Build` xmlns=`msbuildnamespace` ToolsVersion=`msbuilddefaulttoolsversion`>
<Import Project=`$(MSBuildToolsPath)\Microsoft.Common.props`/>
<PropertyGroup>
<Platform>AnyCPU</Platform>
<Configuration>Debug</Configuration>
<BaseOutputPath>{baseOutputPath}</BaseOutputPath>
</PropertyGroup>
<Import Project=`$(MSBuildToolsPath)\Microsoft.Common.targets`/>
<Target Name=`Build`/>
</Project>");
// Act
Project project = ObjectModelHelpers.LoadProjectFileInTempProjectDirectory(projectFilePath, touchProject: false);
project.Build(new MockLogger(_output)).ShouldBeTrue();
// Assert
project.GetPropertyValue("BaseOutputPath").ShouldBe(baseOutputPath.WithTrailingSlash());
project.GetPropertyValue("BaseOutputPathWasSpecified").ShouldBe("true");
project.GetPropertyValue("_OutputPathWasMissing").ShouldBe("true");
}
/// <summary>
/// Test when both BaseOutputPath and OutputPath are specified.
/// </summary>
[Fact]
public void BothBaseOutputPathAndOutputPathWereSpecified()
{
// Arrange
var baseOutputPath = Path.Combine("build", "bin");
var outputPath = Path.Combine("bin", "Debug");
var outputPathAlt = Path.Combine("bin", "Release");
var projectFilePath = ObjectModelHelpers.CreateFileInTempProjectDirectory(_projectRelativePath,
$@"<Project DefaultTargets=`Build` xmlns=`msbuildnamespace` ToolsVersion=`msbuilddefaulttoolsversion`>
<Import Project=`$(MSBuildToolsPath)\Microsoft.Common.props`/>
<PropertyGroup>
<Platform>AnyCPU</Platform>
<Configuration>Debug</Configuration>
</PropertyGroup>
<PropertyGroup>
<BaseOutputPath>{baseOutputPath}</BaseOutputPath>
<OutputPath Condition=`'$(Platform)|$(Configuration)' == 'AnyCPU|Debug'`>{outputPath}</OutputPath>
<OutputPath Condition=`'$(Platform)|$(Configuration)' == 'AnyCPU|Release'`>{outputPathAlt}</OutputPath>
</PropertyGroup>
<Import Project=`$(MSBuildToolsPath)\Microsoft.Common.targets`/>
<Target Name=`Build`/>
</Project>");
// Act
Project project = ObjectModelHelpers.LoadProjectFileInTempProjectDirectory(projectFilePath, touchProject: false);
project.Build(new MockLogger(_output)).ShouldBeTrue();
// Assert
project.GetPropertyValue("BaseOutputPath").ShouldBe(baseOutputPath.WithTrailingSlash());
project.GetPropertyValue("OutputPath").ShouldBe(outputPath.WithTrailingSlash());
project.GetPropertyValue("BaseOutputPathWasSpecified").ShouldBe("true");
project.GetPropertyValue("_OutputPathWasMissing").ShouldBe(string.Empty);
}
/// <summary>
/// Test for [MSBuild]::NormalizePath and [MSBuild]::NormalizeDirectory returning current directory instead of current Project directory.
/// </summary>
[ConditionalFact(typeof(NativeMethodsShared), nameof(NativeMethodsShared.IsWindows), Skip = "Skipping this test for now until we have a consensus about this issue.")]
public void MSBuildNormalizePathShouldReturnProjectDirectory()
{
// Arrange
var configuration = "Debug";
var baseOutputPath = "bin";
var projectFilePath = ObjectModelHelpers.CreateFileInTempProjectDirectory(_projectRelativePath,
$@"<Project DefaultTargets=`Build` xmlns=`msbuildnamespace` ToolsVersion=`msbuilddefaulttoolsversion`>
<Import Project=`$(MSBuildToolsPath)\Microsoft.Common.props`/>
<PropertyGroup Condition=`'$(OutputPath)' == ''`>
<OutputPath>$([MSBuild]::NormalizeDirectory('{baseOutputPath}', '{configuration}'))</OutputPath>
</PropertyGroup>
<Import Project=`$(MSBuildToolsPath)\Microsoft.Common.targets`/>
<Target Name=`Build`/>
</Project>");
// Act
Project project = ObjectModelHelpers.LoadProjectFileInTempProjectDirectory(projectFilePath, touchProject: false);
project.Build(new MockLogger(_output)).ShouldBeTrue();
// Assert
project.GetPropertyValue("Configuration").ShouldBe(configuration);
project.GetPropertyValue("BaseOutputPath").ShouldBe(baseOutputPath.WithTrailingSlash());
var expectedOutputPath = FileUtilities.CombinePaths(project.DirectoryPath, baseOutputPath, configuration).WithTrailingSlash();
project.GetPropertyValue("OutputPath").ShouldBe(expectedOutputPath);
}
}
}