|
| 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