Skip to content

Commit

Permalink
Merge pull request #5665 from paulomorgado/patch-1
Browse files Browse the repository at this point in the history
Use a counter instead of relying in XPathNodeIterator.Count
  • Loading branch information
Forgind authored Aug 28, 2020
2 parents 8ad1cf3 + a6c396b commit 091189a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/Tasks.UnitTests/XmlPoke_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Text;
using System.Xml;
using Xunit;
using Shouldly;

namespace Microsoft.Build.UnitTests
{
Expand Down Expand Up @@ -111,6 +112,22 @@ public void PokeChildren()
Assert.Equal("Testing", testNodes?.First().InnerText);
}

[Fact]
public void PokeAttributeWithCondition()
{
const string original = "b";
const string value = "x";
const string queryTemplate = "/class/variable[@Name='{0}']/@Name";

XmlDocument xmlDocument = ExecuteXmlPoke(query: string.Format(queryTemplate, original), value: value);

List<XmlAttribute> nodes = xmlDocument.SelectNodes(string.Format(queryTemplate, value))?.Cast<XmlAttribute>().ToList();

nodes?.Count.ShouldBe(1, $"There should be 1 <class /> element with an AccessModifier attribute {Environment.NewLine}{xmlDocument.OuterXml}");

nodes?[0].Value.ShouldBe(value);
}

[Fact]
public void PokeMissingParams()
{
Expand Down
6 changes: 4 additions & 2 deletions src/Tasks/XmlPoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ public override bool Execute()
}

XPathNodeIterator iter = nav.Select(expr);
int count = 0;

while (iter.MoveNext())
{
try
{
count++;
iter.Current.InnerXml = _value.ItemSpec;
Log.LogMessageFromResources(MessageImportance.Low, "XmlPoke.Replaced", iter.Current.Name, _value.ItemSpec);
}
Expand All @@ -190,9 +192,9 @@ public override bool Execute()
}
}

Log.LogMessageFromResources(MessageImportance.Normal, "XmlPoke.Count", iter.Count);
Log.LogMessageFromResources(MessageImportance.Normal, "XmlPoke.Count", count);

if (iter.Count > 0)
if (count > 0)
{
#if RUNTIME_TYPE_NETCORE
using (Stream stream = File.Create(_xmlInputPath.ItemSpec))
Expand Down

0 comments on commit 091189a

Please sign in to comment.