Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the log of CombineTargetFrameworkInfoProperties fails with not valid RootElementName #9002

Merged
merged 3 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Tasks.UnitTests/CombineTargetFrameworkInfoProperties_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Shouldly;
using Xunit;

namespace Microsoft.Build.UnitTests
{
public sealed class CombineTargetFrameworkInfoProperties_Tests
{
/// <summary>
/// https://github.com/dotnet/msbuild/issues/8320
/// </summary>
[Fact]
public void RootElementNameNotValid()
{
var task = new CombineTargetFrameworkInfoProperties();
var items = new ITaskItem[]
{
new TaskItemData("ItemSpec1", null)
};
task.PropertiesAndValues = items;
task.UseAttributeForTargetFrameworkInfoPropertyNames = true;
var exp = Assert.Throws<ArgumentNullException>(() => task.Execute());
exp.Message.ShouldContain("RootElementName");

task.RootElementName = string.Empty;
task.UseAttributeForTargetFrameworkInfoPropertyNames = false;
var exp1 = Assert.Throws<ArgumentException>(() => task.Execute());
exp1.Message.ShouldContain("RootElementName");
}
}
}
35 changes: 32 additions & 3 deletions src/Tasks/CombineTargetFrameworkInfoProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,28 @@ public class CombineTargetFrameworkInfoProperties : TaskExtension
/// <summary>
/// The root element name to use for the generated XML string
/// </summary>
public string RootElementName { get; set; }
private string _rootElementName;

/// <summary>
/// Gets or sets the root element name to use for the generated XML string
/// </summary>
public string RootElementName
{
get
{
if (!UseAttributeForTargetFrameworkInfoPropertyNames)
{
ErrorUtilities.VerifyThrowArgumentLength(_rootElementName, nameof(RootElementName));
}
else
{
ErrorUtilities.VerifyThrowArgumentNull(_rootElementName, nameof(RootElementName));
}
return _rootElementName;
}

set => _rootElementName = value;
}

/// <summary>
/// Items to include in the XML. The ItemSpec should be the property name, and it should have Value metadata for its value.
Expand All @@ -39,9 +60,17 @@ public override bool Execute()
{
if (PropertiesAndValues != null)
{
if (!UseAttributeForTargetFrameworkInfoPropertyNames)
{
ErrorUtilities.VerifyThrowArgumentLength(_rootElementName, nameof(RootElementName));
}
else
{
ErrorUtilities.VerifyThrowArgumentNull(_rootElementName, nameof(RootElementName));
}
XElement root = UseAttributeForTargetFrameworkInfoPropertyNames ?
new("TargetFramework", new XAttribute("Name", EscapingUtilities.Escape(RootElementName))) :
new(RootElementName);
new("TargetFramework", new XAttribute("Name", EscapingUtilities.Escape(_rootElementName))) :
new(_rootElementName);

foreach (ITaskItem item in PropertiesAndValues)
{
Expand Down