Skip to content

Commit 588830f

Browse files
authored
Fixes issues related to SDK-style projects and non-existent targets (#37)
Always set OutputPath so that targets that require it (like Clean) are happy. Specify the list of targets in CleanDependsOn to disable the stock clean target. Fixes #35
1 parent b67c07a commit 588830f

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/Traversal.UnitTests/TraversalTests.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
//
33
// Licensed under the MIT license.
44

5+
using Microsoft.Build.Evaluation;
56
using Microsoft.Build.Framework;
67
using Microsoft.Build.Utilities.ProjectCreation;
78
using Shouldly;
9+
using System;
10+
using System.Collections.Generic;
811
using System.Linq;
912
using UnitTest.Common;
1013
using Xunit;
@@ -13,7 +16,7 @@ namespace Microsoft.Build.Traversal.UnitTests
1316
{
1417
public class TraversalTests : MSBuildSdkTestBase
1518
{
16-
[Fact(Skip = "This does not currently work, need to investigate why")]
19+
[Fact]
1720
public void SkipsNonExistentTargets()
1821
{
1922
string[] projects = new[]
@@ -35,17 +38,16 @@ public void SkipsNonExistentTargets()
3538
}
3639

3740
[Theory]
41+
[InlineData("Clean")]
3842
[InlineData("Build")]
3943
[InlineData("Test")]
4044
public void TraversalTargetsRun(string target)
4145
{
4246
string[] projects = new[]
4347
{
4448
ProjectCreator.Templates.LogsMessage("BF0C6E1044514FE3AE4B78EC308D6F45", MessageImportance.High, targetName: target, path: GetTempFileWithExtension(".proj"))
45-
.Target("GetNativeManifest")
4649
.Save(),
4750
ProjectCreator.Templates.LogsMessage("40869F4000B44D75A52AB305F24E0FDB", MessageImportance.High, targetName: target, path: GetTempFileWithExtension(".proj"))
48-
.Target("GetNativeManifest")
4951
.Save(),
5052
}.Select(i => i.FullPath).ToArray();
5153

@@ -72,5 +74,40 @@ public void TraversalTargetsRun(string target)
7274
ignoreOrder: true,
7375
customMessage: () => buildOutput.GetConsoleLog());
7476
}
77+
78+
[Theory]
79+
[InlineData(null)]
80+
[InlineData("Debug")]
81+
[InlineData("Release")]
82+
[InlineData("Random")]
83+
public void WorksWhenConfigurationSpecified(string configuration)
84+
{
85+
Dictionary<string, string> globalProperties = new Dictionary<string, string>
86+
{
87+
["DesignTimeBuild"] = "true"
88+
};
89+
90+
if (!String.IsNullOrWhiteSpace(configuration))
91+
{
92+
globalProperties.Add("Configuration", configuration);
93+
}
94+
95+
string[] projects = new[]
96+
{
97+
ProjectCreator.Templates.SdkCsproj(path: GetTempFileWithExtension(".csproj"))
98+
.Save(),
99+
}.Select(i => i.FullPath).ToArray();
100+
101+
ProjectCreator
102+
.Templates
103+
.TraversalProject(
104+
projects,
105+
path: GetTempFile("dirs.proj"),
106+
projectCollection: new ProjectCollection(globalProperties))
107+
.Save()
108+
.TryBuild("Clean", out bool result, out BuildOutput buildOutput);
109+
110+
result.ShouldBeTrue(customMessage: () => buildOutput.GetConsoleLog());
111+
}
75112
}
76113
}

src/Traversal/Sdk/Sdk.targets

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88

99
<Import Project="$(CustomBeforeTraversalTargets)" Condition=" '$(CustomBeforeTraversalTargets)' != '' And Exists('$(CustomBeforeTraversalTargets)') " />
1010

11+
<PropertyGroup Condition=" '$(OutputPath)' == '' ">
12+
<!--
13+
OutputPath is required to be set because targets like Clean will read it. Traversal projects shouldn't really emit any
14+
output but it could be hard to track down all of the places where $(OutputPath) and $(OutDir) are expected to be set.
15+
-->
16+
<OutputPath Condition=" '$(Configuration)' == '' And '$(Platform)' == '' ">bin\Debug\</OutputPath>
17+
<OutputPath Condition=" '$(Configuration)' != '' And '$(Platform)' == '' ">bin\$(Configuration)\</OutputPath>
18+
<OutputPath Condition=" '$(Configuration)' != '' And '$(Platform)' != '' ">bin\$(Configuration)\$(Platform)\</OutputPath>
19+
</PropertyGroup>
20+
1121
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" Condition=" Exists('$(MSBuildToolsPath)\Microsoft.Common.targets') " />
1222

1323
<PropertyGroup>
@@ -36,6 +46,15 @@
3646
<TestDependsOn>
3747
Build
3848
</TestDependsOn>
49+
50+
<CleanDependsOn>
51+
BeforeClean;
52+
UnmanagedUnregistration;
53+
CoreClean;
54+
PrepareProjectReferences;
55+
CleanPublishFolder;
56+
AfterClean
57+
</CleanDependsOn>
3958
</PropertyGroup>
4059

4160
<ItemGroup Condition=" '$(TraversalTranslateProjectFileItems)' != 'false' ">

0 commit comments

Comments
 (0)