Skip to content

Commit

Permalink
Merge branch 'fabionuno-feature/cake-buildGH-930' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Jun 3, 2016
2 parents 8242857 + e2907a2 commit 142516e
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Cake.Common.Tests/Fixtures/XmlPeekAliasesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class XmlPeekAliasesFixture
public FilePath XmlPath { get; set; }
public XmlPeekSettings Settings { get; set; }

public XmlPeekAliasesFixture(bool xmlExists = true)
public XmlPeekAliasesFixture(bool xmlExists = true, bool xmlWithDtd = false)
{
Settings = new XmlPeekSettings();

Expand All @@ -24,7 +24,8 @@ public XmlPeekAliasesFixture(bool xmlExists = true)

if (xmlExists)
{
var xmlFile = fileSystem.CreateFile("/Working/web.config").SetContent(Resources.XmlPeek_Xml);
string content = xmlWithDtd ? Resources.XmlPeek_Xml_Dtd : Resources.XmlPeek_Xml;
var xmlFile = fileSystem.CreateFile("/Working/web.config").SetContent(content);
XmlPath = xmlFile.Path;
}

Expand Down
27 changes: 23 additions & 4 deletions src/Cake.Common.Tests/Fixtures/XmlPokeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal sealed class XmlPokeFixture
public FilePath XmlPath { get; set; }
public XmlPokeSettings Settings { get; set; }

public XmlPokeFixture(bool xmlExists = true)
public XmlPokeFixture(bool xmlExists = true, bool xmlWithDtd = false)
{
Settings = new XmlPokeSettings();

Expand All @@ -27,7 +27,8 @@ public XmlPokeFixture(bool xmlExists = true)

if (xmlExists)
{
var xmlFile = fileSystem.CreateFile("/Working/web.config").SetContent(Resources.XmlPoke_Xml);
string content = xmlWithDtd ? Resources.XmlPoke_Xml_Dtd : Resources.XmlPoke_Xml;
var xmlFile = fileSystem.CreateFile("/Working/web.config").SetContent(content);
XmlPath = xmlFile.Path;
}

Expand All @@ -43,6 +44,11 @@ public void Poke(string xpath, string value)
XmlPokeAliases.XmlPoke(Context, XmlPath, xpath, value, Settings);
}

public string PokeString(string xml, string xpath, string value)
{
return XmlPokeAliases.XmlPokeString(Context, xml, xpath, value, Settings);
}

public bool TestIsValue(string xpath, string value)
{
var xmlString = new StreamReader(FileSystem.GetFile(XmlPath).OpenRead()).ReadToEnd();
Expand All @@ -52,7 +58,7 @@ public bool TestIsValue(string xpath, string value)
public bool TestIsValue(string xml, string xpath, string value)
{
using (var reader = new StringReader(xml))
using (var xmlReader = XmlReader.Create(reader))
using (var xmlReader = XmlReader.Create(reader, GetXmlReaderSettings(Settings)))
{

var document = new XmlDocument();
Expand All @@ -78,7 +84,7 @@ public bool TestIsRemoved(string xpath)
public bool TestIsRemoved(string xml, string xpath)
{
using (var reader = new StringReader(xml))
using (var xmlReader = XmlReader.Create(reader))
using (var xmlReader = XmlReader.Create(reader, GetXmlReaderSettings(Settings)))
{

var document = new XmlDocument();
Expand All @@ -94,5 +100,18 @@ public bool TestIsRemoved(string xml, string xpath)
return nodes != null && nodes.Count == 0;
}
}

/// <summary>
/// Gets a XmlReaderSettings from a XmlPokeSettings
/// </summary>
/// <returns>The xml reader settings.</returns>
/// <param name="settings">Additional settings to tweak Xml Poke behavior.</param>
private static XmlReaderSettings GetXmlReaderSettings(XmlPokeSettings settings)
{
var xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.DtdProcessing = (DtdProcessing)settings.DtdProcessing;

return xmlReaderSettings;
}
}
}
33 changes: 33 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.

22 changes: 22 additions & 0 deletions src/Cake.Common.Tests/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,17 @@ Line #3]]&gt;&lt;/releaseNotes&gt;
&lt;/appSettings&gt;
&lt;/configuration&gt;</value>
</data>
<data name="XmlPoke_Xml_Dtd" xml:space="preserve">
<value>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;
&lt;plist version=&quot;1.0&quot;&gt;
&lt;dict&gt;
&lt;key&gt;CFBundleDisplayName&lt;/key&gt;
&lt;string&gt;Cake&lt;/string&gt;
&lt;/dict&gt;
&lt;/plist&gt;
</value>
</data>
<data name="XmlPeek_Xml" xml:space="preserve">
<value>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
Expand All @@ -535,6 +546,17 @@ Line #3]]&gt;&lt;/releaseNotes&gt;
&lt;/configuration&gt;
</value>
</data>
<data name="XmlPeek_Xml_Dtd" xml:space="preserve">
<value>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;
&lt;plist version=&quot;1.0&quot;&gt;
&lt;dict&gt;
&lt;key&gt;CFBundleDisplayName&lt;/key&gt;
&lt;string&gt;Cake&lt;/string&gt;
&lt;/dict&gt;
&lt;/plist&gt;
</value>
</data>
<data name="XmlTransformation_Htm" xml:space="preserve">
<value>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;html&gt;&lt;body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"&gt;&lt;div style="background-color:teal;color:white;padding:4px"&gt;&lt;span style="font-weight:bold"&gt;Belgian Waffles
-
Expand Down
28 changes: 28 additions & 0 deletions src/Cake.Common.Tests/Unit/XML/XmlPeekAliasesTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using Cake.Common.Tests.Fixtures;
using Cake.Common.Xml;
using Xunit;

namespace Cake.Common.Tests.Unit.XML
Expand Down Expand Up @@ -47,6 +48,19 @@ public void Should_Throw_If_No_Xpath()
// Then
Assert.IsArgumentNullException(result, "xpath");
}

[Fact]
public void Should_Throw_If_File_Has_Dtd()
{
// Given
var fixture = new XmlPeekAliasesFixture(xmlWithDtd: true);

// When
var result = Record.Exception(() => fixture.Peek("CFBundleDisplayName"));

// Then
Assert.IsType<System.Xml.XmlException>(result);
}

[Fact]
public void Should_Get_Attribute_Value()
Expand All @@ -73,6 +87,20 @@ public void Should_Get_Node_Value()
// Then
Assert.Equal("test value", result);
}

[Fact]
public void Should_Get_Node_Value_From_File_With_Dtd()
{
// Given
var fixture = new XmlPeekAliasesFixture(xmlWithDtd:true);
fixture.Settings.DtdProcessing = XmlDtdProcessing.Parse;

// When
var result = fixture.Peek("/plist/dict/key/text()");

// Then
Assert.Equal("CFBundleDisplayName", result);
}
}
}
}
53 changes: 49 additions & 4 deletions src/Cake.Common.Tests/Unit/XML/XmlPokeTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.IO;
using Cake.Common.Tests.Fixtures;
using Cake.Common.Xml;
using Xunit;
using Cake.Common.Tests.Properties;
using System.Text;

namespace Cake.Common.Tests.Unit.XML
{
Expand Down Expand Up @@ -47,6 +50,32 @@ public void Should_Throw_If_No_Xpath()
// Then
Assert.IsArgumentNullException(result, "xpath");
}

[Fact]
public void Should_Throw_If_Xml_File_Has_Dtd()
{
// Given
var fixture = new XmlPokeFixture(xmlWithDtd: true);

// When
var result = Record.Exception(() => fixture.Poke("/plist/dict/string/text()",""));

// Then
Assert.IsType<System.Xml.XmlException>(result);
}

[Fact]
public void Should_Throw_If_Xml_String_Has_Dtd()
{
// Given
var fixture = new XmlPokeFixture(xmlExists:false);

// When
var result = Record.Exception(() => fixture.PokeString(Resources.XmlPoke_Xml_Dtd, "/plist/dict/string/text()",""));

// Then
Assert.IsType<System.Xml.XmlException>(result);
}
}

public sealed class Transform
Expand All @@ -61,9 +90,9 @@ public void Should_Change_Attribute()
fixture.Poke("/configuration/appSettings/add[@key = 'server']/@value", "productionhost.somecompany.com");

// Then
fixture.TestIsValue(
Assert.True(fixture.TestIsValue(
"/configuration/appSettings/add[@key = 'server']/@value",
"productionhost.somecompany.com");
"productionhost.somecompany.com"));
}

[Fact]
Expand All @@ -76,8 +105,24 @@ public void Should_Remove_Attribute()
fixture.Poke("/configuration/appSettings/add[@key = 'server']", null);

// Then
fixture.TestIsRemoved(
"/configuration/appSettings/add[@key = 'server']");
Assert.True(fixture.TestIsRemoved(
"/configuration/appSettings/add[@key = 'server']"));
}

[Fact]
public void Should_Change_Attribute_From_Xml_File_With_Dtd()
{
// Given
var fixture = new XmlPokeFixture(xmlWithDtd:true);
fixture.Settings.DtdProcessing = XmlDtdProcessing.Parse;

// When
fixture.Poke("/plist/dict/string", "Cake Version");

// Then
Assert.True(fixture.TestIsValue(
"/plist/dict/string/text()",
"Cake Version"));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Cake.Common/Cake.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@
<Compile Include="Build\Bamboo\Data\BambooPlanInfo.cs" />
<Compile Include="Xml\XmlPeekAliases.cs" />
<Compile Include="Xml\XmlPeekSettings.cs" />
<Compile Include="Xml\XmlDtdProcessing.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Cake.Core\Cake.Core.csproj">
Expand Down
25 changes: 25 additions & 0 deletions src/Cake.Common/Xml/XmlDtdProcessing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Cake.Common.Xml
{
/// <summary>
/// Speficies how will an XmlReader handle DTDs in the XML document.
/// </summary>
public enum XmlDtdProcessing
{
/// <summary>
/// The XmlReader will throw an exception when it finds a 'DOCTYPE' markup.
/// </summary>
Prohibit,

/// <summary>
/// The DTD will be ignored. Any reference to a general entity in the XML document
/// will cause an exception (except for the predefined entities &lt; &gt; &amp; &quot; and &apos;).
/// The DocumentType node will not be reported.
/// </summary>
Ignore,

/// <summary>
/// The DTD will be parsed and fully processed (entities expanded, default attributes added etc.)
/// </summary>
Parse,
}
}
21 changes: 19 additions & 2 deletions src/Cake.Common/Xml/XmlPeekAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,20 @@ public static string XmlPeek(this ICakeContext context, FilePath filePath, strin
{
throw new ArgumentNullException("filePath");
}

if (settings == null)
{
throw new ArgumentNullException("settings");
}

var file = context.FileSystem.GetFile(filePath);
if (!file.Exists)
{
throw new FileNotFoundException("Source File not found.", file.Path.FullPath);
}

using (var fileStream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
using (var xmlReader = XmlReader.Create(fileStream))
using (var xmlReader = XmlReader.Create(fileStream, GetXmlReaderSettings(settings)))
{
var xmlValue = XmlPeek(xmlReader, xpath, settings);
if (xmlValue == null)
Expand Down Expand Up @@ -128,5 +133,17 @@ private static string XmlPeek(XmlReader source, string xpath, XmlPeekSettings se

return node != null ? node.Value : null;
}

/// <summary>
/// Gets a XmlReaderSettings from a XmlPeekSettings
/// </summary>
/// <returns>The xml reader settings.</returns>
/// <param name="settings">Additional settings to tweak Xml Peek behavior.</param>
private static XmlReaderSettings GetXmlReaderSettings(XmlPeekSettings settings)
{
var xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.DtdProcessing = (DtdProcessing)settings.DtdProcessing;
return xmlReaderSettings;
}
}
}
6 changes: 6 additions & 0 deletions src/Cake.Common/Xml/XmlPeekSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ public sealed class XmlPeekSettings
/// </summary>
public bool PreserveWhitespace { get; set; }

/// <summary>
/// Gets or sets a value that determines the processing of DTDs.
/// </summary>
public XmlDtdProcessing DtdProcessing { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="XmlPeekSettings"/> class.
/// </summary>
public XmlPeekSettings()
{
PreserveWhitespace = true;
Namespaces = new Dictionary<string, string>();
DtdProcessing = XmlDtdProcessing.Prohibit;
}
}
}
Loading

0 comments on commit 142516e

Please sign in to comment.