Using random string for temp files (dotnet/corefx#28589)
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Fri, 30 Mar 2018 19:35:45 +0000 (12:35 -0700)
committerGitHub <noreply@github.com>
Fri, 30 Mar 2018 19:35:45 +0000 (12:35 -0700)
Commit migrated from https://github.com/dotnet/corefx/commit/73e8a1f8130a792e6661819e6236533576fca45a

src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs
src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XslCompiledTransform.cs
src/libraries/System.Private.Xml/tests/Xslt/XslCompiledTransformApi/XsltApiV2.cs
src/libraries/System.Private.Xml/tests/Xslt/XslTransformApi/XSLTransform.cs

index 618ebf8..6060512 100644 (file)
@@ -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]
index 0c6dfaf..2f2649b 100644 (file)
@@ -609,18 +609,13 @@ namespace System.Xml.Tests
             AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true);
 
             string expected = @"<?xml version=""1.0"" encoding=""utf-8""?><result>123</result>";
+            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 = @"<?xml version=""1.0"" encoding=""utf-8""?><result>123</result>";
+            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)
             {
index e06d0a7..f5c7c15 100644 (file)
@@ -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);
index 010efa9..3ef33bd 100644 (file)
@@ -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