Skip to content

Commit

Permalink
(cake-buildGH-1537) - Refactor XmlPeek alias to use XPathNavigator to…
Browse files Browse the repository at this point in the history
… handle retrieval of element values; add supporting tests
  • Loading branch information
kcamp committed Aug 2, 2017
1 parent 93ca222 commit 21725ac
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Cake.Common.Tests/Fixtures/XmlPeekAliasesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public XmlPeekAliasesFixture(bool xmlExists = true, bool xmlWithDtd = false, boo
Context.Log.Returns(FakeLog);
}

public void SetContent(string xml)
{
var file = ((FakeFileSystem)FileSystem).GetFile(XmlPath).SetContent(xml);
}

public string Peek(string xpath)
{
return XmlPeekAliases.XmlPeek(Context, XmlPath, xpath, Settings);
Expand Down
8 changes: 8 additions & 0 deletions src/Cake.Common.Tests/Properties/Resources.Designer.cs

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

15 changes: 15 additions & 0 deletions src/Cake.Common.Tests/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1012,4 +1012,19 @@ Imports System.Runtime.CompilerServices;
'<Assembly: AssemblyDelaySign(false)>
'&lt;Assembly: AssemblyKeyFile("")&gt;</value>
</data>
<data name="XmlPeek_Xml_With_Namespace" xml:space="preserve">
<value>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
&lt;PropertyGroup&gt;
&lt;WebPublishMethod&gt;FileSystem&lt;/WebPublishMethod&gt;
&lt;LastUsedBuildConfiguration&gt;DeploymentTemplate&lt;/LastUsedBuildConfiguration&gt;
&lt;LastUsedPlatform&gt;Any CPU&lt;/LastUsedPlatform&gt;
&lt;SiteUrlToLaunchAfterPublish /&gt;
&lt;LaunchSiteAfterPublish&gt;True&lt;/LaunchSiteAfterPublish&gt;
&lt;ExcludeApp_Data&gt;False&lt;/ExcludeApp_Data&gt;
&lt;publishUrl&gt;C:\Deployment\DeploymentTemplate\WebApi&lt;/publishUrl&gt;
&lt;DeleteExistingFiles&gt;False&lt;/DeleteExistingFiles&gt;
&lt;/PropertyGroup&gt;
&lt;/Project&gt;</value>
</data>
</root>
28 changes: 28 additions & 0 deletions src/Cake.Common.Tests/Unit/XML/XmlPeekAliasesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,34 @@ public void Should_Get_Node_Value()
Assert.Equal("test value", result);
}

[Fact]
public void Should_Get_Element_Value()
{
// Given
var fixture = new XmlPeekAliasesFixture();

// When
var result = fixture.Peek("/configuration/test");

// Then
Assert.Equal("test value", result);
}

[Fact]
public void Should_Get_Element_Value_With_Namespace()
{
// Given
var fixture = new XmlPeekAliasesFixture();
fixture.SetContent(Properties.Resources.XmlPeek_Xml_With_Namespace);
fixture.Settings.Namespaces.Add("msbuild", "http://schemas.microsoft.com/developer/msbuild/2003");

// When
var result = fixture.Peek("/msbuild:Project/msbuild:PropertyGroup/msbuild:publishUrl");

// Then
Assert.Equal("C:\\Deployment\\DeploymentTemplate\\WebApi", result);
}

[Fact]
public void Should_Get_Node_Value_From_File_With_Dtd()
{
Expand Down
7 changes: 4 additions & 3 deletions src/Cake.Common/Xml/XmlPeekAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ private static string XmlPeek(XmlReader source, string xpath, XmlPeekSettings se
document.PreserveWhitespace = settings.PreserveWhitespace;
document.Load(source);

var namespaceManager = new XmlNamespaceManager(document.NameTable);
var navigator = document.CreateNavigator();
var namespaceManager = new XmlNamespaceManager(navigator.NameTable);

foreach (var xmlNamespace in settings.Namespaces)
{
namespaceManager.AddNamespace(xmlNamespace.Key /* Prefix */, xmlNamespace.Value /* URI */);
}

var node = document.SelectSingleNode(xpath, namespaceManager);

var node = navigator.SelectSingleNode(xpath, namespaceManager);
return node?.Value;
}

Expand Down

0 comments on commit 21725ac

Please sign in to comment.