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

Fix files kept in use in XslTransformation #6946

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/Tasks.UnitTests/XslTransformation_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text.RegularExpressions;
using System.Xml.Xsl;
using System.Xml;
using Shouldly;
using Xunit;

namespace Microsoft.Build.UnitTests
Expand Down Expand Up @@ -386,7 +387,7 @@ public void OutputTest()
/// Setting correct "Parameter" parameters for Xsl.
/// </summary>
[Fact]
public void XsltParamatersCorrect()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the change is here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in "parameters"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry about that, I'm on the verge of OCD for these kind of things, and when I spotted that while skimming through the tests, I couldn't resist...

public void XsltParametersCorrect()
{
string dir;
TaskItem[] xmlPaths;
Expand Down Expand Up @@ -780,6 +781,39 @@ public void OutputFileCannotBeWritten()
CleanUp(dir);
}

/// <summary>
/// The files are not kept locked by the task
/// </summary>
[Fact]
public void InputFilesDontLock()
{
string dir;
TaskItem[] xmlPaths;
TaskItem xslPath;
TaskItem[] outputPaths;
MockEngine engine;
Prepare(out dir, out xmlPaths, out xslPath, out _, out outputPaths, out _, out _, out engine);

// Test with files
{
XslTransformation t = new XslTransformation();
t.BuildEngine = engine;
t.XmlInputPaths = xmlPaths;
t.XslInputPath = xslPath;
t.OutputPaths = outputPaths;

t.Execute().ShouldBeTrue();
string xmlInputPath = xmlPaths[0].ItemSpec;
File.Delete(xmlInputPath); // this should succeed (file not locked by task)
File.Exists(xmlInputPath).ShouldBeFalse();
string xslInputPath = xslPath.ItemSpec;
File.Delete(xslInputPath); // this should succeed (file not locked by task)
File.Exists(xslInputPath).ShouldBeFalse();
}

CleanUp(dir);
}

/// <summary>
/// XslDocument that throws runtime exception.
/// </summary>
Expand Down
9 changes: 6 additions & 3 deletions src/Tasks/XslTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ public XmlReader CreateReader(int itemPos)
{
if (XmlMode == XmlModes.XmlFile)
{
return XmlReader.Create(new StreamReader(_data[itemPos]), null, _data[itemPos]);
return XmlReader.Create(new StreamReader(_data[itemPos]), new XmlReaderSettings { CloseInput = true }, _data[itemPos]);
}
else // xmlModes.Xml
else // xmlModes.Xml
{
return XmlReader.Create(new StringReader(_data[itemPos]));
}
Expand Down Expand Up @@ -459,7 +459,10 @@ public XslCompiledTransform LoadXslt(bool useTrustedSettings)
_log.LogMessageFromResources(MessageImportance.Low, "XslTransform.UseTrustedSettings", _data);
}

xslct.Load(new XPathDocument(XmlReader.Create(new StreamReader(_data), null, _data)), settings, new XmlUrlResolver());
using (XmlReader reader = XmlReader.Create(new StreamReader(_data), new XmlReaderSettings { CloseInput = true }, _data))
{
xslct.Load(new XPathDocument(reader), settings, new XmlUrlResolver());
}
break;
case XslModes.XsltCompiledDll:
#if FEATURE_COMPILED_XSL
Expand Down