diff options
3 files changed, 68 insertions, 71 deletions
diff --git a/src/Common/tests/System/IO/TempFile.cs b/src/Common/tests/System/IO/TempFile.cs index 4c3753899f..f5aa954ef9 100644 --- a/src/Common/tests/System/IO/TempFile.cs +++ b/src/Common/tests/System/IO/TempFile.cs @@ -2,6 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.CompilerServices; +using Xunit; + namespace System.IO { /// <summary> @@ -16,11 +19,31 @@ namespace System.IO public TempFile(string path, long length = 0) { Path = path; - File.WriteAllBytes(path, new byte[length]); + + if (length > -1) + File.WriteAllBytes(path, new byte[length]); } ~TempFile() { DeleteFile(); } + public static TempFile Create(long length = -1, [CallerMemberName] string memberName = null, [CallerLineNumber] int lineNumber = 0) + { + string file = string.Format("{0}_{1}_{2}", System.IO.Path.GetRandomFileName(), memberName, lineNumber); + string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), file); + + return new TempFile(path, length); + } + + public void AssertExists() + { + Assert.True(File.Exists(Path)); + } + + public string ReadAllText() + { + return File.ReadAllText(Path); + } + public void Dispose() { GC.SuppressFinalize(this); diff --git a/src/System.Private.Xml.Linq/tests/TreeManipulation/SaveWithFileName.cs b/src/System.Private.Xml.Linq/tests/TreeManipulation/SaveWithFileName.cs index f30dbf48d2..05ca02f0f0 100644 --- a/src/System.Private.Xml.Linq/tests/TreeManipulation/SaveWithFileName.cs +++ b/src/System.Private.Xml.Linq/tests/TreeManipulation/SaveWithFileName.cs @@ -17,8 +17,6 @@ namespace XLinqTests { public class SaveWithFileName { - private string _fileName = "SaveBaseline"; - [Fact] public void XDocumentSaveToFile() { @@ -29,16 +27,14 @@ namespace XLinqTests public void XDocumentSave() { string markup = "<e> <e2 /> </e>"; - try + + using (TempFile temp = TempFile.Create()) { XDocument d = XDocument.Parse(markup, LoadOptions.PreserveWhitespace); - d.Save(_fileName); - } - finally - { - Assert.True(File.Exists(_fileName)); - Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, NormalizeNewLines(File.ReadAllText(_fileName))); - File.Delete(_fileName); + d.Save(temp.Path); + + temp.AssertExists(); + Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, NormalizeNewLines(temp.ReadAllText())); } } @@ -46,16 +42,13 @@ namespace XLinqTests public void XDocumentSave_SaveOptions() { string markup = "<e> <e2 /> </e>"; - try + using (TempFile temp = TempFile.Create()) { XDocument d = XDocument.Parse(markup, LoadOptions.PreserveWhitespace); - d.Save(_fileName, SaveOptions.DisableFormatting); - } - finally - { - Assert.True(File.Exists(_fileName)); - Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, File.ReadAllText(_fileName)); - File.Delete(_fileName); + d.Save(temp.Path, SaveOptions.DisableFormatting); + + temp.AssertExists(); + Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, temp.ReadAllText()); } } @@ -77,16 +70,13 @@ namespace XLinqTests public void XElementSave() { string markup = "<e a=\"value\"> <e2 /> </e>"; - try + using (TempFile temp = TempFile.Create()) { XElement e = XElement.Parse(markup, LoadOptions.PreserveWhitespace); - e.Save(_fileName); - } - finally - { - Assert.True(File.Exists(_fileName)); - Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, NormalizeNewLines(File.ReadAllText(_fileName))); - File.Delete(_fileName); + e.Save(temp.Path); + + temp.AssertExists(); + Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, NormalizeNewLines(temp.ReadAllText())); } } @@ -94,16 +84,13 @@ namespace XLinqTests public void XElementSave_SaveOptions() { string markup = "<e a=\"value\"> <e2 /> </e>"; - try + using (TempFile temp = TempFile.Create()) { XElement e = XElement.Parse(markup, LoadOptions.PreserveWhitespace); - e.Save(_fileName, SaveOptions.DisableFormatting); - } - finally - { - Assert.True(File.Exists(_fileName)); - Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, File.ReadAllText(_fileName)); - File.Delete(_fileName); + e.Save(temp.Path, SaveOptions.DisableFormatting); + + temp.AssertExists(); + Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, temp.ReadAllText()); } } @@ -127,17 +114,14 @@ namespace XLinqTests public void XStreamingElementSave() { string markup = "<e a=\"value\"> <!--comment--> <e2> <![CDATA[cdata]]> </e2> <?pi target?> </e>"; - try + using (TempFile temp = TempFile.Create()) { XElement e = XElement.Parse(markup, LoadOptions.PreserveWhitespace); XStreamingElement e2 = new XStreamingElement(e.Name, e.Attributes(), e.Nodes()); - e2.Save(_fileName); - } - finally - { - Assert.True(File.Exists(_fileName)); - Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, NormalizeNewLines(File.ReadAllText(_fileName))); - File.Delete(_fileName); + e2.Save(temp.Path); + + temp.AssertExists(); + Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, NormalizeNewLines(temp.ReadAllText())); } } @@ -145,17 +129,14 @@ namespace XLinqTests public void XStreamingElementSave_SaveOptions() { string markup = "<e a=\"value\"> <!--comment--> <e2> <![CDATA[cdata]]> </e2> <?pi target?> </e>"; - try + using (TempFile temp = TempFile.Create()) { XElement e = XElement.Parse(markup, LoadOptions.PreserveWhitespace); XStreamingElement e2 = new XStreamingElement(e.Name, e.Attributes(), e.Nodes()); - e2.Save(_fileName, SaveOptions.DisableFormatting); - } - finally - { - Assert.True(File.Exists(_fileName)); - Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, File.ReadAllText(_fileName)); - File.Delete(_fileName); + e2.Save(temp.Path, SaveOptions.DisableFormatting); + + temp.AssertExists(); + Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + markup, temp.ReadAllText()); } } @@ -164,42 +145,32 @@ namespace XLinqTests // private static string SerializeXDocumentSaveToFile(XNode node) { - string filename = "DontExist"; string result; - try + using (TempFile temp = TempFile.Create()) { - filename = Path.GetTempFileName(); - ((XDocument)node).Save(filename); - using (TextReader tr = new StreamReader(filename)) + ((XDocument)node).Save(temp.Path); + using (TextReader tr = new StreamReader(temp.Path)) { result = StripOffXmlDeclaration(tr.ReadToEnd()); } - } - finally - { - Assert.True(File.Exists(filename)); - File.Delete(filename); + + temp.AssertExists(); } return result; } private static string SerializeXElementSaveToFile(XNode node) { - string filename = "DontExist"; string result; - try + using (TempFile temp = TempFile.Create()) { - filename = Path.GetTempFileName(); - ((XElement)node).Save(filename); - using (TextReader tr = new StreamReader(filename)) + ((XElement)node).Save(temp.Path); + using (TextReader tr = new StreamReader(temp.Path)) { result = StripOffXmlDeclaration(tr.ReadToEnd()); } - } - finally - { - Assert.True(File.Exists(filename)); - File.Delete(filename); + + temp.AssertExists(); } return result; } diff --git a/src/System.Private.Xml.Linq/tests/TreeManipulation/System.Xml.Linq.TreeManipulation.Tests.csproj b/src/System.Private.Xml.Linq/tests/TreeManipulation/System.Xml.Linq.TreeManipulation.Tests.csproj index 6e01214563..d907197608 100644 --- a/src/System.Private.Xml.Linq/tests/TreeManipulation/System.Xml.Linq.TreeManipulation.Tests.csproj +++ b/src/System.Private.Xml.Linq/tests/TreeManipulation/System.Xml.Linq.TreeManipulation.Tests.csproj @@ -13,6 +13,9 @@ <Compile Include="$(CommonTestPath)\System\Collections\DictionaryExtensions.cs"> <Link>Common\System\Collections\DictionaryExtensions.cs</Link> </Compile> + <Compile Include="$(CommonTestPath)\System\IO\TempFile.cs"> + <Link>Common\System\IO\TempFile.cs</Link> + </Compile> <Compile Include="AddFirstAddFirstIntoDocument.cs" /> <Compile Include="AddFirstInvalidIntoXDocument.cs" /> <Compile Include="AddFirstSingeNodeAddIntoElement.cs" /> |