From 171d1dc0a2b0c927f352a42a56d42ae57b83e5b2 Mon Sep 17 00:00:00 2001 From: Anirudh Agnihotry Date: Fri, 30 Mar 2018 12:35:45 -0700 Subject: [PATCH] Using random string for temp files (dotnet/corefx#28589) Commit migrated from https://github.com/dotnet/corefx/commit/73e8a1f8130a792e6661819e6236533576fca45a --- .../tests/Functional/SmtpClientTest.cs | 20 +++--------- .../XslCompiledTransform.cs | 38 +++++++++++++--------- .../Xslt/XslCompiledTransformApi/XsltApiV2.cs | 2 +- .../tests/Xslt/XslTransformApi/XSLTransform.cs | 2 +- 4 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs b/src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs index 618ebf8..6060512 100644 --- a/src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs +++ b/src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs @@ -17,10 +17,9 @@ using Xunit; namespace System.Net.Mail.Tests { - public class SmtpClientTest : IDisposable + public class SmtpClientTest : FileCleanupTestBase { private SmtpClient _smtp; - private string _tempFolder; private SmtpClient Smtp { @@ -34,28 +33,17 @@ namespace System.Net.Mail.Tests { get { - if (_tempFolder == null) - { - _tempFolder = Path.Combine(Path.GetTempPath(), GetType().FullName, Guid.NewGuid().ToString()); - if (Directory.Exists(_tempFolder)) - Directory.Delete(_tempFolder, true); - - Directory.CreateDirectory(_tempFolder); - } - - return _tempFolder; + return TestDirectory; } } - public void Dispose() + protected override void Dispose(bool disposing) { if (_smtp != null) { _smtp.Dispose(); } - - if (Directory.Exists(_tempFolder)) - Directory.Delete(_tempFolder, true); + base.Dispose(disposing); } [Theory] diff --git a/src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XslCompiledTransform.cs b/src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XslCompiledTransform.cs index 0c6dfaf..2f2649b 100644 --- a/src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XslCompiledTransform.cs +++ b/src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XslCompiledTransform.cs @@ -609,18 +609,13 @@ namespace System.Xml.Tests AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true); string expected = @"123"; + string fileName = GetType().Name + "_" + Path.GetRandomFileName(); + string testFile = Path.Combine(Path.GetTempPath(), fileName); + string xmlFile = FullFilePath(fileName); // copy file on the local machine try { - string tempPath = Path.GetTempPath(); - string testFile = Path.Combine(tempPath, "xmlResolver_document_function.xml"); - if (File.Exists(testFile)) - { - File.SetAttributes(testFile, FileAttributes.Normal); - File.Delete(testFile); - } - string xmlFile = FullFilePath("xmlResolver_document_function.xml"); File.Copy(xmlFile, testFile, true); } catch (Exception e) @@ -629,6 +624,14 @@ namespace System.Xml.Tests _output.WriteLine("Could not copy file to local. Some other issues prevented this test from running"); return; //TEST_SKIPPED; } + finally + { + if (File.Exists(testFile)) + { + File.SetAttributes(testFile, FileAttributes.Normal); + File.Delete(testFile); + } + } // copy file on the local machine (this is now done with createAPItestfiles.js, see Oasys scenario.) if (LoadXSL("xmlResolver_document_function_absolute_uri.xsl", xslInputType, readerType) == 1) @@ -2522,18 +2525,13 @@ namespace System.Xml.Tests AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true); string expected = @"123"; + string fileName = GetType().Name + "_" + Path.GetRandomFileName(); + string testFile = Path.Combine(Path.GetTempPath(), fileName); + string xmlFile = FullFilePath(fileName); // copy file on the local machine try { - string tempPath = Path.GetTempPath(); - string testFile = Path.Combine(tempPath, "xmlResolver_document_function.xml"); - if (File.Exists(testFile)) - { - File.SetAttributes(testFile, FileAttributes.Normal); - File.Delete(testFile); - } - string xmlFile = FullFilePath("xmlResolver_document_function.xml"); File.Copy(xmlFile, testFile, true); } catch (Exception e) @@ -2542,6 +2540,14 @@ namespace System.Xml.Tests _output.WriteLine("Could not copy file to local. Some other issues prevented this test from running"); return; //TEST_SKIPPED; } + finally + { + if (File.Exists(testFile)) + { + File.SetAttributes(testFile, FileAttributes.Normal); + File.Delete(testFile); + } + } if (LoadXSL("xmlResolver_document_function_absolute_uri.xsl", xslInputType, readerType) == 1) { diff --git a/src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XsltApiV2.cs b/src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XsltApiV2.cs index e06d0a7..f5c7c15 100644 --- a/src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XsltApiV2.cs +++ b/src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XsltApiV2.cs @@ -76,7 +76,7 @@ namespace System.Xml.Tests static XsltApiTestCaseBase2() { // Replace absolute URI in xmlResolver_document_function.xml based on the environment - string targetFile = Path.Combine(Path.GetTempPath(), "xmlResolver_document_function.xml"); + string targetFile = Path.Combine(Path.GetTempPath(), typeof(XsltApiTestCaseBase2) + "_" + Path.GetRandomFileName()); string xslFile = Path.Combine("TestFiles", FilePathUtil.GetTestDataPath(), "XsltApiV2", "xmlResolver_document_function_absolute_uri.xsl"); XmlDocument doc = new XmlDocument(); doc.Load(xslFile); diff --git a/src/libraries/System.Private.Xml/tests/Xslt/XslTransformApi/XSLTransform.cs b/src/libraries/System.Private.Xml/tests/Xslt/XslTransformApi/XSLTransform.cs index 010efa9..3ef33bd 100644 --- a/src/libraries/System.Private.Xml/tests/Xslt/XslTransformApi/XSLTransform.cs +++ b/src/libraries/System.Private.Xml/tests/Xslt/XslTransformApi/XSLTransform.cs @@ -49,7 +49,7 @@ namespace System.Xml.Tests public class XsltApiTestCaseBase : FileCleanupTestBase { private const string XmlResolverDocumentName = "xmlResolver_document_function.xml"; - private static readonly string s_temporaryResolverDocumentFullName = Path.Combine(Path.GetTempPath(), "XslTransformApi", XmlResolverDocumentName); + private static readonly string s_temporaryResolverDocumentFullName = Path.Combine(Path.GetTempPath(), typeof(XsltApiTestCaseBase) + "_" + Path.GetRandomFileName()); private static readonly object s_temporaryResolverDocumentLock = new object(); // Generic data for all derived test cases -- 2.7.4