Skip to content

Commit

Permalink
Fix TaskParser probably has a bug in switch de-duplication logic. (#5115
Browse files Browse the repository at this point in the history
)

* handle duplicate property name

* use string resource, assign next error code

* assert exception message

* use Shouldly as suggested in code review

* split huge line of xml in test

* correct use of Should.Throw

* eliminate _switchesAdded variable

- no significant effect on performance because item count is always low

* assert only error code as suggested in code review
  • Loading branch information
szaliszali authored Feb 18, 2020
1 parent 39a85e1 commit acd843e
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/Tasks.UnitTests/XamlTaskFactory_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Build.Tasks.Xaml;
using System.Xaml;
using Xunit;
using Shouldly;

namespace Microsoft.Build.UnitTests.XamlTaskFactory_Tests
{
Expand Down Expand Up @@ -191,6 +192,25 @@ public void TestBasicReversibleBooleanSwitches()
Assert.Equal(PropertyType.Boolean, properties.First.Value.Type);
}

[Fact]
public void TestParseIncorrect_PropertyNamesMustBeUnique()
{
string incorrectXmlContents = @"<ProjectSchemaDefinitions
xmlns=`clr-namespace:Microsoft.Build.Framework.XamlTypes;assembly=Microsoft.Build.Framework`
xmlns:x=`http://schemas.microsoft.com/winfx/2006/xaml`
xmlns:sys=`clr-namespace:System;assembly=mscorlib`
xmlns:impl=`clr-namespace:Microsoft.VisualStudio.Project.Contracts.Implementation;assembly=Microsoft.VisualStudio.Project.Contracts.Implementation`>
<Rule Name=`CL`>
<BoolProperty Name=`SameName` Switch=`Og` ReverseSwitch=`Og-` />
<BoolProperty Name=`SameName` Switch=`Og` ReverseSwitch=`Og-` />
</Rule>
</ProjectSchemaDefinitions>";

Should
.Throw<XamlParseException>(() => XamlTestHelpers.LoadAndParse(incorrectXmlContents, "CL"))
.Message.ShouldStartWith("MSB3724");
}

/// <summary>
/// Tests a basic non-reversible booleans switch
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Tasks/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,10 @@
<value>MSB3723: The parameter "{0}" requires missing parameter "{1}" to be set.</value>
<comment>{StrBegin="MSB3723: "}</comment>
</data>
<data name="Xaml.DuplicatePropertyName" xml:space="preserve">
<value>MSB3724: Unable to create Xaml task. Duplicate property name '{0}'.</value>
<comment>{StrBegin="MSB3724: "}</comment>
</data>

<!--
The XslTransformation message bucket is: MSB3701-3710.
Expand Down
5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.en.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Tasks/Resources/xlf/Strings.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/Tasks/XamlTaskFactory/TaskParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ namespace Microsoft.Build.Tasks.Xaml
/// </summary>
internal class TaskParser
{
/// <summary>
/// The set of switches added so far.
/// </summary>
private readonly HashSet<string> _switchesAdded = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// The ordered list of how the switches get emitted.
/// </summary>
Expand Down Expand Up @@ -305,10 +300,14 @@ private bool ParseParameter(XamlTypes.BaseProperty baseProperty, LinkedList<Prop
}

// generate the list of parameters in order
if (!_switchesAdded.Contains(propertyToAdd.Name))
if (!_switchOrderList.Contains(propertyToAdd.Name))
{
_switchOrderList.Add(propertyToAdd.Name);
}
else
{
throw new XamlParseException(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("Xaml.DuplicatePropertyName", propertyToAdd.Name));
}

// Inherit the Prefix from the Tool
if (String.IsNullOrEmpty(propertyToAdd.Prefix))
Expand Down

0 comments on commit acd843e

Please sign in to comment.