Skip to content

Commit 852522f

Browse files
committed
Test that DT targets evaluate and build
We recently had an issue where a design-time target was merged containing invalid XML. These targets are crucial to almost everything the project system does, so a failure in them completely breaks the IDE. In this change we add a unit test to validate that our C#/VB/F# design-time targets files can be correctly evaluated, and can build a simple target. This ensures both that they contain valid XML with valid MSBuild semantics.
1 parent 6104222 commit 852522f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
2+
3+
using Microsoft.VisualStudio.Utilities;
4+
using Microsoft.Build.Evaluation;
5+
using Microsoft.Build.Framework;
6+
7+
namespace Microsoft.VisualStudio.ProjectSystem.DesignTimeTargets;
8+
9+
public sealed class DesignTimeTargetsTests
10+
{
11+
[Theory]
12+
[InlineData("Microsoft.CSharp.DesignTime.targets")]
13+
[InlineData("Microsoft.VisualBasic.DesignTime.targets")]
14+
[InlineData("Microsoft.FSharp.DesignTime.targets")]
15+
public void ValidateDesignTimeTargetsEvaluateAndBuild(string targetFileName)
16+
{
17+
// eg: src\Microsoft.VisualStudio.ProjectSystem.Managed\ProjectSystem\DesignTimeTargets\Microsoft.CSharp.DesignTime.targets
18+
19+
string path = Path.Combine(RepoUtil.FindRepoRootPath(), "src", "Microsoft.VisualStudio.ProjectSystem.Managed", "ProjectSystem", "DesignTimeTargets", targetFileName);
20+
21+
// Force an evaluation of the project.
22+
Project project = new(path);
23+
24+
Logger logger = new();
25+
26+
// Build a target. This isn't a particularly interesting target, but it's one of the few
27+
// that don't require a target defined elsewhere. If we were to try and build all targets
28+
// in the project (via project.Targets.Keys) we would hit error MSB4057 (target not found)
29+
// because we are not importing the common targets, etc. here. This is just a smoke test
30+
// to make sure we can build a target.
31+
project.Build(["CollectPackageReferences"], [logger]);
32+
33+
// Unload everything when done.
34+
project.ProjectCollection.UnloadAllProjects();
35+
36+
Assert.Empty(logger.Errors);
37+
Assert.True(logger.Succeeded);
38+
}
39+
40+
private sealed class Logger : ILogger
41+
{
42+
public LoggerVerbosity Verbosity { get; set; } = LoggerVerbosity.Quiet;
43+
public string? Parameters { get; set; }
44+
45+
public bool? Succeeded { get; private set; }
46+
47+
public ImmutableList<BuildErrorEventArgs> Errors = [];
48+
49+
public void Initialize(IEventSource eventSource)
50+
{
51+
eventSource.ErrorRaised += (s, e) => ImmutableInterlocked.Update(
52+
ref Errors,
53+
static (errors, e) => errors.Add(e),
54+
e);
55+
56+
eventSource.BuildFinished += (s, e) => Succeeded = e.Succeeded;
57+
}
58+
59+
public void Shutdown()
60+
{
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)