From 632bb59b211fbd53dbbc31457edc1a66e153ae52 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 4 Dec 2018 16:43:23 +0100 Subject: [PATCH] [System] Implement a helper function to create unique temporary directories and use it in tests. (#11831) While working on PR #11829 I stress-tested the tests by running multiple test runs in parallel, and it turned out that many tests were not parallel-safe, because they used the same temporary file names and ended up stomping on eachother. So I implemented a helper method that creates a guaranteed unique temporary directory, and I've changed all tests to use this new helper method. --- mcs/class/System/System_test.dll.sources | 1 + .../Microsoft.CSharp/CSharpCodeProviderTest.cs | 38 +-- .../TempFileCollectionCas.cs | 6 +- .../ApplicationSettingsBaseTest.cs | 9 +- .../System.Configuration/ConfigXmlDocumentTest.cs | 11 +- .../ConfigurationExceptionTest.cs | 12 +- .../System/Test/System.Diagnostics/EventLogTest.cs | 10 +- .../Test/System.Diagnostics/FileVersionInfoTest.cs | 11 +- .../System/Test/System.IO/FileSystemWatcherTest.cs | 26 +- .../System/Test/System.Net.Mail/SmtpClientTest.cs | 12 +- .../System/Test/System.Net/FileWebRequestTest.cs | 22 +- .../System/Test/System.Net/FileWebResponseTest.cs | 22 +- .../System/Test/System.Net/FtpWebRequestTest.cs | 17 +- mcs/class/System/Test/System.Net/WebClientTest.cs | 272 ++++++++++----------- mcs/class/test-helpers/PathHelpers.cs | 68 ++++++ 15 files changed, 287 insertions(+), 250 deletions(-) create mode 100644 mcs/class/test-helpers/PathHelpers.cs diff --git a/mcs/class/System/System_test.dll.sources b/mcs/class/System/System_test.dll.sources index d322e8ac2fb..532f0a562b5 100644 --- a/mcs/class/System/System_test.dll.sources +++ b/mcs/class/System/System_test.dll.sources @@ -516,3 +516,4 @@ System.Collections.Concurrent/ParallelTestHelper.cs System.Net.WebSockets/ClientWebSocketTest.cs ../../test-helpers/NetworkHelpers.cs ../../test-helpers/SocketResponder.cs +../../test-helpers/PathHelpers.cs diff --git a/mcs/class/System/Test/Microsoft.CSharp/CSharpCodeProviderTest.cs b/mcs/class/System/Test/Microsoft.CSharp/CSharpCodeProviderTest.cs index 212ab339e46..e03f59a5826 100644 --- a/mcs/class/System/Test/Microsoft.CSharp/CSharpCodeProviderTest.cs +++ b/mcs/class/System/Test/Microsoft.CSharp/CSharpCodeProviderTest.cs @@ -18,12 +18,14 @@ using Microsoft.CSharp; using NUnit.Framework; using System.Text; using System.Linq; +using MonoTests.Helpers; namespace MonoTests.Microsoft.CSharp { [TestFixture] public class CSharpCodeProviderTest { + private TempDirectory _tempDirectory; private string _tempDir; private CodeDomProvider _codeProvider; @@ -38,13 +40,14 @@ namespace MonoTests.Microsoft.CSharp public void SetUp () { _codeProvider = new CSharpCodeProvider (); - _tempDir = CreateTempDirectory (); + _tempDirectory = new TempDirectory (); + _tempDir = _tempDirectory.Path; } [TearDown] public void TearDown () { - RemoveDirectory (_tempDir); + _tempDirectory.Dispose (); } [Test] @@ -601,37 +604,6 @@ namespace MonoTests.Microsoft.CSharp Assert.IsTrue (results.Output.Cast().ToArray ()[1].Contains ("Trigger Some Warning")); } - private static string CreateTempDirectory () - { - // create a uniquely named zero-byte file - string tempFile = Path.GetTempFileName (); - // remove the temporary file - File.Delete (tempFile); - // create a directory named after the unique temporary file - Directory.CreateDirectory (tempFile); - // return the path to the temporary directory - return tempFile; - } - - private static void RemoveDirectory (string path) - { - try { - if (Directory.Exists (path)) { - string[] directoryNames = Directory.GetDirectories (path); - foreach (string directoryName in directoryNames) { - RemoveDirectory (directoryName); - } - string[] fileNames = Directory.GetFiles (path); - foreach (string fileName in fileNames) { - File.Delete (fileName); - } - Directory.Delete (path, true); - } - } catch (Exception ex) { - throw new AssertionException ("Unable to cleanup '" + path + "'.", ex); - } - } - private static void AssertCompileResults (CompilerResults results, bool allowWarnings) { foreach (CompilerError compilerError in results.Errors) { diff --git a/mcs/class/System/Test/System.CodeDom.Compiler/TempFileCollectionCas.cs b/mcs/class/System/Test/System.CodeDom.Compiler/TempFileCollectionCas.cs index caf0eb0940a..4fb1639aa76 100644 --- a/mcs/class/System/Test/System.CodeDom.Compiler/TempFileCollectionCas.cs +++ b/mcs/class/System/Test/System.CodeDom.Compiler/TempFileCollectionCas.cs @@ -37,12 +37,15 @@ using System.Reflection; using System.Security; using System.Security.Permissions; +using MonoTests.Helpers; + namespace MonoCasTests.System.CodeDom.Compiler { [TestFixture] [Category ("CAS")] public class TempFileCollectionCas { + private TempDirectory _temp; private string temp; private string[] array; @@ -50,7 +53,8 @@ namespace MonoCasTests.System.CodeDom.Compiler { public void FixtureSetUp () { // at full trust - temp = Path.GetTempPath (); + _temp = new TempDirectory (); + temp = _temp.Path; array = new string[1]; } diff --git a/mcs/class/System/Test/System.Configuration/ApplicationSettingsBaseTest.cs b/mcs/class/System/Test/System.Configuration/ApplicationSettingsBaseTest.cs index 562e524601d..92004ddee4b 100644 --- a/mcs/class/System/Test/System.Configuration/ApplicationSettingsBaseTest.cs +++ b/mcs/class/System/Test/System.Configuration/ApplicationSettingsBaseTest.cs @@ -42,6 +42,8 @@ using System.Collections.Specialized; using NUnit.Framework; using CategoryAttribute = NUnit.Framework.CategoryAttribute; +using MonoTests.Helpers; + namespace MonoTests.System.Configuration { class ProviderPoker : LocalFileSettingsProvider { public override void Initialize (string name, @@ -164,14 +166,15 @@ namespace MonoTests.System.Configuration { [TestFixture] public class ApplicationSettingsBaseTest { + TempDirectory _tempDir; string tempDir; [TestFixtureSetUp] public void FixtureSetup () { // Use random temp directory to store settings files of tests. - tempDir = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ()); - Directory.CreateDirectory (tempDir); + _tempDir = new TempDirectory (); + tempDir = _tempDir.Path; var localAppData = Path.Combine (tempDir, "LocalAppData"); Directory.CreateDirectory (localAppData); var appData = Path.Combine (tempDir, "AppData"); @@ -186,7 +189,7 @@ namespace MonoTests.System.Configuration { { Environment.SetEnvironmentVariable ("XDG_DATA_HOME", null); Environment.SetEnvironmentVariable ("XDG_CONFIG_HOME", null); - Directory.Delete (tempDir, true); + _tempDir.Dispose (); } [Test] diff --git a/mcs/class/System/Test/System.Configuration/ConfigXmlDocumentTest.cs b/mcs/class/System/Test/System.Configuration/ConfigXmlDocumentTest.cs index bf60640e07e..387f38a22b2 100644 --- a/mcs/class/System/Test/System.Configuration/ConfigXmlDocumentTest.cs +++ b/mcs/class/System/Test/System.Configuration/ConfigXmlDocumentTest.cs @@ -34,24 +34,25 @@ using System.Configuration; using NUnit.Framework; +using MonoTests.Helpers; + namespace MonoTests.System.Configuration { [TestFixture] public class ConfigXmlDocumentTest { + private TempDirectory _tempFolder; private string tempFolder; [SetUp] public void SetUp () { - tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName); - if (!Directory.Exists (tempFolder)) - Directory.CreateDirectory (tempFolder); + _tempFolder = new TempDirectory (); + tempFolder = _tempFolder.Path; } [TearDown] public void TearDown () { - if (Directory.Exists (tempFolder)) - Directory.Delete (tempFolder, true); + _tempFolder.Dispose (); } [Test] diff --git a/mcs/class/System/Test/System.Configuration/ConfigurationExceptionTest.cs b/mcs/class/System/Test/System.Configuration/ConfigurationExceptionTest.cs index 40feac8c806..8de71570f8c 100644 --- a/mcs/class/System/Test/System.Configuration/ConfigurationExceptionTest.cs +++ b/mcs/class/System/Test/System.Configuration/ConfigurationExceptionTest.cs @@ -33,27 +33,27 @@ using System.Xml; using NUnit.Framework; +using MonoTests.Helpers; + namespace MonoTests.System.Configuration { [TestFixture] public class ConfigurationExceptionTest { + private TempDirectory temp; private string foldername; [SetUp] public void SetUp () { - foldername = Path.Combine (Path.GetTempPath (), - this.GetType ().FullName); - if (!Directory.Exists (foldername)) - Directory.CreateDirectory (foldername); + temp = new TempDirectory (); + foldername = temp.Path; } [TearDown] public void TearDown () { - if (Directory.Exists (foldername)) - Directory.Delete (foldername, true); + temp.Dispose (); } [Test] // ctor () diff --git a/mcs/class/System/Test/System.Diagnostics/EventLogTest.cs b/mcs/class/System/Test/System.Diagnostics/EventLogTest.cs index 3b03dd8aaab..f267064ee78 100644 --- a/mcs/class/System/Test/System.Diagnostics/EventLogTest.cs +++ b/mcs/class/System/Test/System.Diagnostics/EventLogTest.cs @@ -53,11 +53,14 @@ using Microsoft.Win32; using NUnit.Framework; +using MonoTests.Helpers; + namespace MonoTests.System.Diagnostics { [TestFixture] public class EventLogTest { + private TempDirectory _temp; private string _originalEventLogImpl; private string _eventLogStore; @@ -75,8 +78,8 @@ namespace MonoTests.System.Diagnostics return; // determine temp directory for eventlog store - _eventLogStore = Path.Combine (Path.GetTempPath (), - Guid.NewGuid ().ToString ()); + _temp = new TempDirectory (); + _eventLogStore = _temp.Path; // save original eventlog implementation type (if set) _originalEventLogImpl = Environment.GetEnvironmentVariable ( @@ -98,8 +101,7 @@ namespace MonoTests.System.Diagnostics _originalEventLogImpl); // delete temp directory for eventlog store - if (Directory.Exists (_eventLogStore)) - Directory.Delete (_eventLogStore, true); + _temp.Dispose (); } [Test] diff --git a/mcs/class/System/Test/System.Diagnostics/FileVersionInfoTest.cs b/mcs/class/System/Test/System.Diagnostics/FileVersionInfoTest.cs index 815bbb85c45..3ef13ae74aa 100644 --- a/mcs/class/System/Test/System.Diagnostics/FileVersionInfoTest.cs +++ b/mcs/class/System/Test/System.Diagnostics/FileVersionInfoTest.cs @@ -19,26 +19,27 @@ using System.Text; using NUnit.Framework; +using MonoTests.Helpers; + namespace MonoTests.System.Diagnostics { [TestFixture] public class FileVersionInfoTest { + private TempDirectory _tempDir; private string tempDir; [SetUp] public void SetUp () { - tempDir = Path.Combine (Path.GetTempPath (), Environment.UserName); - tempDir = Path.Combine (tempDir, "MonoTests.System.Diagnostics.AppDomainTest"); - if (!Directory.Exists (tempDir)) - Directory.CreateDirectory (tempDir); + _tempDir = new TempDirectory (); + tempDir = _tempDir.Path; } [TearDown] public void TearDown () { - Directory.Delete (tempDir, true); + _tempDir.Dispose (); } [Test] diff --git a/mcs/class/System/Test/System.IO/FileSystemWatcherTest.cs b/mcs/class/System/Test/System.IO/FileSystemWatcherTest.cs index 09087252aaf..585c15bcf77 100644 --- a/mcs/class/System/Test/System.IO/FileSystemWatcherTest.cs +++ b/mcs/class/System/Test/System.IO/FileSystemWatcherTest.cs @@ -12,6 +12,8 @@ using NUnit.Framework; using System; using System.IO; +using MonoTests.Helpers; + namespace MonoTests.System.IO { [TestFixture] @@ -54,7 +56,9 @@ namespace MonoTests.System.IO [ExpectedException (typeof (ArgumentNullException))] public void CheckCtor4 () { - FileSystemWatcher fw = new FileSystemWatcher (Path.GetTempPath (), null); + using (var tmp = new TempDirectory ()) { + FileSystemWatcher fw = new FileSystemWatcher (tmp.Path, null); + } } [Test] @@ -62,8 +66,12 @@ namespace MonoTests.System.IO // [ExpectedException (typeof (ArgumentException))] public void CheckCtor5 () { - FileSystemWatcher fw = new FileSystemWatcher (Path.GetTempPath (), "invalidpath|"); - fw = new FileSystemWatcher (Path.GetTempPath (), "*"); + using (var tmp1 = new TempDirectory ()) { + using (var tmp2 = new TempDirectory ()) { + FileSystemWatcher fw = new FileSystemWatcher (tmp1.Path, "invalidpath|"); + fw = new FileSystemWatcher (tmp2.Path, "*"); + } + } } [Test] @@ -71,8 +79,10 @@ namespace MonoTests.System.IO [ExpectedException (typeof (ArgumentException))] public void CheckInvalidPath () { - FileSystemWatcher fw = new FileSystemWatcher (Path.GetTempPath (), "invalidpath|"); - fw.Path = "invalidpath|"; + using (var tmp = new TempDirectory ()) { + FileSystemWatcher fw = new FileSystemWatcher (tmp.Path, "invalidpath|"); + fw.Path = "invalidpath|"; + } } [Test] @@ -80,8 +90,10 @@ namespace MonoTests.System.IO [ExpectedException (typeof (ArgumentException))] public void CheckPathWildcard () { - FileSystemWatcher fw = new FileSystemWatcher (Path.GetTempPath (), "*"); - fw.Path = "*"; + using (var tmp = new TempDirectory ()) { + FileSystemWatcher fw = new FileSystemWatcher (tmp.Path, "*"); + fw.Path = "*"; + } } } } diff --git a/mcs/class/System/Test/System.Net.Mail/SmtpClientTest.cs b/mcs/class/System/Test/System.Net.Mail/SmtpClientTest.cs index d3a39e41315..233415074c3 100644 --- a/mcs/class/System/Test/System.Net.Mail/SmtpClientTest.cs +++ b/mcs/class/System/Test/System.Net.Mail/SmtpClientTest.cs @@ -13,6 +13,8 @@ using System.Net.Mail; using System.Net.Mime; using System.Threading; +using MonoTests.Helpers; + namespace MonoTests.System.Net.Mail { [TestFixture] @@ -20,23 +22,21 @@ namespace MonoTests.System.Net.Mail { SmtpClient _smtp; SmtpClient smtp { get { return _smtp ?? (_smtp = new SmtpClient ()); } } + TempDirectory _tempFolder; string tempFolder; [SetUp] public void GetReady () { - tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName); - if (Directory.Exists (tempFolder)) - Directory.Delete (tempFolder, true); - Directory.CreateDirectory (tempFolder); + _tempFolder = new TempDirectory (); + tempFolder = _tempFolder.Path; } [TearDown] public void TearDown () { _smtp = null; - if (Directory.Exists (tempFolder)) - Directory.Delete (tempFolder, true); + _tempFolder.Dispose (); } [Test] diff --git a/mcs/class/System/Test/System.Net/FileWebRequestTest.cs b/mcs/class/System/Test/System.Net/FileWebRequestTest.cs index 5cfdabe0a43..869666699b0 100644 --- a/mcs/class/System/Test/System.Net/FileWebRequestTest.cs +++ b/mcs/class/System/Test/System.Net/FileWebRequestTest.cs @@ -21,37 +21,29 @@ using System.Security.Permissions; using NUnit.Framework; +using MonoTests.Helpers; namespace MonoTests.System.Net { [TestFixture] public class FileWebRequestTest { - private string _tempDirectory; + private TempDirectory _tempDirectory; private string _tempFile; private Uri _tempFileUri; [SetUp] public void SetUp () { - _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest"); - _tempFile = Path.Combine (_tempDirectory, "FileWebRequestTest.tmp"); - if (!Directory.Exists (_tempDirectory)) { - Directory.CreateDirectory (_tempDirectory); - } else { - // ensure no files are left over from previous runs - string [] files = Directory.GetFiles (_tempDirectory, "*"); - foreach (string file in files) - File.Delete (file); - } + _tempDirectory = new TempDirectory (); + _tempFile = Path.Combine (_tempDirectory.Path, "FileWebRequestTest.tmp"); _tempFileUri = GetTempFileUri (); } [TearDown] public void TearDown () { - if (Directory.Exists (_tempDirectory)) - Directory.Delete (_tempDirectory, true); + _tempDirectory.Dispose (); } [Test] @@ -198,7 +190,7 @@ namespace MonoTests.System.Net } // the temp file should not be in use - Directory.Delete (_tempDirectory, true); + _tempDirectory.Dispose (); } [Test] @@ -305,7 +297,7 @@ namespace MonoTests.System.Net } // the temp file should not be in use - Directory.Delete (_tempDirectory, true); + _tempDirectory.Dispose (); } [Test] diff --git a/mcs/class/System/Test/System.Net/FileWebResponseTest.cs b/mcs/class/System/Test/System.Net/FileWebResponseTest.cs index 472c71d8c9c..97f1f33a6f6 100644 --- a/mcs/class/System/Test/System.Net/FileWebResponseTest.cs +++ b/mcs/class/System/Test/System.Net/FileWebResponseTest.cs @@ -13,6 +13,7 @@ using System.Net; using NUnit.Framework; +using MonoTests.Helpers; namespace MonoTests.System.Net @@ -20,35 +21,22 @@ namespace MonoTests.System.Net [TestFixture] public class FileWebResponseTest { - private string _tempDirectory; + private TempDirectory _tempDirectory; private string _tempFile; private Uri _tempFileUri; [SetUp] public void SetUp () { - _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebResponseTest"); - _tempFile = Path.Combine (_tempDirectory, "FileWebResponseTest.tmp"); - if (!Directory.Exists (_tempDirectory)) { - Directory.CreateDirectory (_tempDirectory); - } else { - // ensure no files are left over from previous runs - string [] files = Directory.GetFiles (_tempDirectory, "*"); - foreach (string file in files) - File.Delete (file); - } + _tempDirectory = new TempDirectory (); + _tempFile = Path.Combine (_tempDirectory.Path, "FileWebResponseTest.tmp"); _tempFileUri = GetTempFileUri (); } [TearDown] public void TearDown () { - if (Directory.Exists (_tempDirectory)) { - string [] files = Directory.GetFiles (_tempDirectory, "*"); - foreach (string file in files) - File.Delete (file); - Directory.Delete (_tempDirectory, true); - } + _tempDirectory.Dispose (); } [Test] diff --git a/mcs/class/System/Test/System.Net/FtpWebRequestTest.cs b/mcs/class/System/Test/System.Net/FtpWebRequestTest.cs index dd5df82310b..9834c9ff35a 100644 --- a/mcs/class/System/Test/System.Net/FtpWebRequestTest.cs +++ b/mcs/class/System/Test/System.Net/FtpWebRequestTest.cs @@ -16,6 +16,8 @@ using System.Net.Sockets; using System.Text; using System.Threading; +using MonoTests.Helpers; + namespace MonoTests.System.Net { [TestFixture] @@ -32,23 +34,14 @@ namespace MonoTests.System.Net [SetUp] public void SetUp () { - _tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebRequestTest"); - _tempFile = Path.Combine (_tempDirectory, "FtpWebRequestTest.tmp"); - if (!Directory.Exists (_tempDirectory)) { - Directory.CreateDirectory (_tempDirectory); - } else { - // ensure no files are left over from previous runs - string [] files = Directory.GetFiles (_tempDirectory, "*"); - foreach (string file in files) - File.Delete (file); - } + _tempDirectory = new TempDirectory (); + _tempFile = Path.Combine (_tempDirectory.Path, "FtpWebRequestTest.tmp"); } [TearDown] public void TearDown () { - if (Directory.Exists (_tempDirectory)) - Directory.Delete (_tempDirectory, true); + _tempDirectory.Dispose (); } [Test] diff --git a/mcs/class/System/Test/System.Net/WebClientTest.cs b/mcs/class/System/Test/System.Net/WebClientTest.cs index 2c746494588..b50da19a91a 100644 --- a/mcs/class/System/Test/System.Net/WebClientTest.cs +++ b/mcs/class/System/Test/System.Net/WebClientTest.cs @@ -139,28 +139,26 @@ namespace MonoTests.System.Net [Test] // DownloadFile (string, string) public void DownloadFile1_Address_SchemeNotSupported () { - string file = Path.Combine (Path.GetTempPath (), "tmp.out"); - WebClient wc = new WebClient (); - try { - wc.DownloadFile ("tp://scheme.notsupported", file); - Assert.Fail ("#1"); - } catch (WebException ex) { - // An error occurred performing a WebClient request - Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); - Assert.IsNotNull (ex.InnerException, "#3"); - Assert.IsNotNull (ex.Message, "#4"); - Assert.IsNull (ex.Response, "#5"); - Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6"); - - // The URI prefix is not recognized - Exception inner = ex.InnerException; - Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7"); - Assert.IsNull (inner.InnerException, "#8"); - Assert.IsNotNull (inner.Message, "#9"); - } - finally { - if (File.Exists (file)) - File.Delete (file); + using (var tmpdir = new TempDirectory ()) { + string file = Path.Combine (tmpdir.Path, "tmp.out"); + WebClient wc = new WebClient (); + try { + wc.DownloadFile ("tp://scheme.notsupported", file); + Assert.Fail ("#1"); + } catch (WebException ex) { + // An error occurred performing a WebClient request + Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); + Assert.IsNotNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.Response, "#5"); + Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6"); + + // The URI prefix is not recognized + Exception inner = ex.InnerException; + Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7"); + Assert.IsNull (inner.InnerException, "#8"); + Assert.IsNotNull (inner.Message, "#9"); + } } } @@ -200,28 +198,26 @@ namespace MonoTests.System.Net [Test] // DownloadFile (Uri, string) public void DownloadFile2_Address_SchemeNotSupported () { - string file = Path.Combine (Path.GetTempPath (), "tmp.out"); - WebClient wc = new WebClient (); - try { - wc.DownloadFile (new Uri ("tp://scheme.notsupported"), file); - Assert.Fail ("#1"); - } catch (WebException ex) { - // An error occurred performing a WebClient request - Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); - Assert.IsNotNull (ex.InnerException, "#3"); - Assert.IsNotNull (ex.Message, "#4"); - Assert.IsNull (ex.Response, "#5"); - Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6"); - - // The URI prefix is not recognized - Exception inner = ex.InnerException; - Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7"); - Assert.IsNull (inner.InnerException, "#8"); - Assert.IsNotNull (inner.Message, "#9"); - } - finally { - if (File.Exists (file)) - File.Delete (file); + using (var tmpdir = new TempDirectory ()) { + string file = Path.Combine (tmpdir.Path, "tmp.out"); + WebClient wc = new WebClient (); + try { + wc.DownloadFile (new Uri ("tp://scheme.notsupported"), file); + Assert.Fail ("#1"); + } catch (WebException ex) { + // An error occurred performing a WebClient request + Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); + Assert.IsNotNull (ex.InnerException, "#3"); + Assert.IsNotNull (ex.Message, "#4"); + Assert.IsNull (ex.Response, "#5"); + Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#6"); + + // The URI prefix is not recognized + Exception inner = ex.InnerException; + Assert.AreEqual (typeof (NotSupportedException), inner.GetType (), "#7"); + Assert.IsNull (inner.InnerException, "#8"); + Assert.IsNotNull (inner.Message, "#9"); + } } } @@ -873,30 +869,31 @@ namespace MonoTests.System.Net [Test] // UploadFile (string, string) public void UploadFile1_FileName_NotFound () { - var tempPath = Path.GetTempPath (); - string tempFile = Path.Combine (tempPath, Path.GetRandomFileName ()); + using (var tempPath = new TempDirectory ()) { + string tempFile = Path.Combine (tempPath.Path, Path.GetRandomFileName ()); - WebClient wc = new WebClient (); - try { - wc.UploadFile ("tp://scheme.notsupported", - tempFile); - Assert.Fail ("#1"); - } catch (WebException ex) { - // An error occurred performing a WebClient request - Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); - Assert.IsNotNull (ex.Message, "#3"); - Assert.IsNull (ex.Response, "#4"); - Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5"); - - // Could not find file "..." - FileNotFoundException inner = ex.InnerException - as FileNotFoundException; - Assert.IsNotNull (inner, "#6"); - Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7"); - Assert.IsNotNull (inner.FileName, "#8"); - Assert.AreEqual (tempFile, inner.FileName, "#9"); - Assert.IsNull (inner.InnerException, "#10"); - Assert.IsNotNull (inner.Message, "#11"); + WebClient wc = new WebClient (); + try { + wc.UploadFile ("tp://scheme.notsupported", + tempFile); + Assert.Fail ("#1"); + } catch (WebException ex) { + // An error occurred performing a WebClient request + Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); + Assert.IsNotNull (ex.Message, "#3"); + Assert.IsNull (ex.Response, "#4"); + Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5"); + + // Could not find file "..." + FileNotFoundException inner = ex.InnerException + as FileNotFoundException; + Assert.IsNotNull (inner, "#6"); + Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7"); + Assert.IsNotNull (inner.FileName, "#8"); + Assert.AreEqual (tempFile, inner.FileName, "#9"); + Assert.IsNull (inner.InnerException, "#10"); + Assert.IsNotNull (inner.Message, "#11"); + } } } @@ -967,30 +964,31 @@ namespace MonoTests.System.Net [Test] // UploadFile (Uri, string) public void UploadFile2_FileName_NotFound () { - var tempPath = Path.GetTempPath (); - string tempFile = Path.Combine (tempPath, Path.GetRandomFileName ()); - - WebClient wc = new WebClient (); - try { - wc.UploadFile (new Uri ("tp://scheme.notsupported"), - tempFile); - Assert.Fail ("#1"); - } catch (WebException ex) { - // An error occurred performing a WebClient request - Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); - Assert.IsNotNull (ex.Message, "#3"); - Assert.IsNull (ex.Response, "#4"); - Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5"); + using (var tempPath = new TempDirectory ()) { + string tempFile = Path.Combine (tempPath.Path, Path.GetRandomFileName ()); - // Could not find file "..." - FileNotFoundException inner = ex.InnerException - as FileNotFoundException; - Assert.IsNotNull (inner, "#6"); - Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7"); - Assert.IsNotNull (inner.FileName, "#8"); - Assert.AreEqual (tempFile, inner.FileName, "#9"); - Assert.IsNull (inner.InnerException, "#10"); - Assert.IsNotNull (inner.Message, "#11"); + WebClient wc = new WebClient (); + try { + wc.UploadFile (new Uri ("tp://scheme.notsupported"), + tempFile); + Assert.Fail ("#1"); + } catch (WebException ex) { + // An error occurred performing a WebClient request + Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); + Assert.IsNotNull (ex.Message, "#3"); + Assert.IsNull (ex.Response, "#4"); + Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5"); + + // Could not find file "..." + FileNotFoundException inner = ex.InnerException + as FileNotFoundException; + Assert.IsNotNull (inner, "#6"); + Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7"); + Assert.IsNotNull (inner.FileName, "#8"); + Assert.AreEqual (tempFile, inner.FileName, "#9"); + Assert.IsNull (inner.InnerException, "#10"); + Assert.IsNotNull (inner.Message, "#11"); + } } } @@ -1061,30 +1059,31 @@ namespace MonoTests.System.Net [Test] // UploadFile (string, string, string) public void UploadFile3_FileName_NotFound () { - var tempPath = Path.GetTempPath (); - string tempFile = Path.Combine (tempPath, Path.GetRandomFileName ()); - - WebClient wc = new WebClient (); - try { - wc.UploadFile ("tp://scheme.notsupported", - "POST", tempFile); - Assert.Fail ("#1"); - } catch (WebException ex) { - // An error occurred performing a WebClient request - Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); - Assert.IsNotNull (ex.Message, "#3"); - Assert.IsNull (ex.Response, "#4"); - Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5"); + using (var tempPath = new TempDirectory ()) { + string tempFile = Path.Combine (tempPath.Path, Path.GetRandomFileName ()); - // Could not find file "..." - FileNotFoundException inner = ex.InnerException - as FileNotFoundException; - Assert.IsNotNull (inner, "#6"); - Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7"); - Assert.IsNotNull (inner.FileName, "#8"); - Assert.AreEqual (tempFile, inner.FileName, "#9"); - Assert.IsNull (inner.InnerException, "#10"); - Assert.IsNotNull (inner.Message, "#11"); + WebClient wc = new WebClient (); + try { + wc.UploadFile ("tp://scheme.notsupported", + "POST", tempFile); + Assert.Fail ("#1"); + } catch (WebException ex) { + // An error occurred performing a WebClient request + Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); + Assert.IsNotNull (ex.Message, "#3"); + Assert.IsNull (ex.Response, "#4"); + Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5"); + + // Could not find file "..." + FileNotFoundException inner = ex.InnerException + as FileNotFoundException; + Assert.IsNotNull (inner, "#6"); + Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7"); + Assert.IsNotNull (inner.FileName, "#8"); + Assert.AreEqual (tempFile, inner.FileName, "#9"); + Assert.IsNull (inner.InnerException, "#10"); + Assert.IsNotNull (inner.Message, "#11"); + } } } @@ -1155,30 +1154,31 @@ namespace MonoTests.System.Net [Test] // UploadFile (Uri, string, string) public void UploadFile4_FileName_NotFound () { - var tempPath = Path.GetTempPath (); - string tempFile = Path.Combine (tempPath, Path.GetRandomFileName ()); + using (var tempPath = new TempDirectory ()) { + string tempFile = Path.Combine (tempPath.Path, Path.GetRandomFileName ()); - WebClient wc = new WebClient (); - try { - wc.UploadFile (new Uri ("tp://scheme.notsupported"), - "POST", tempFile); - Assert.Fail ("#1"); - } catch (WebException ex) { - // An error occurred performing a WebClient request - Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); - Assert.IsNotNull (ex.Message, "#3"); - Assert.IsNull (ex.Response, "#4"); - Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5"); - - // Could not find file "..." - FileNotFoundException inner = ex.InnerException - as FileNotFoundException; - Assert.IsNotNull (inner, "#6"); - Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7"); - Assert.IsNotNull (inner.FileName, "#8"); - Assert.AreEqual (tempFile, inner.FileName, "#9"); - Assert.IsNull (inner.InnerException, "#10"); - Assert.IsNotNull (inner.Message, "#11"); + WebClient wc = new WebClient (); + try { + wc.UploadFile (new Uri ("tp://scheme.notsupported"), + "POST", tempFile); + Assert.Fail ("#1"); + } catch (WebException ex) { + // An error occurred performing a WebClient request + Assert.AreEqual (typeof (WebException), ex.GetType (), "#2"); + Assert.IsNotNull (ex.Message, "#3"); + Assert.IsNull (ex.Response, "#4"); + Assert.AreEqual (WebExceptionStatus.UnknownError, ex.Status, "#5"); + + // Could not find file "..." + FileNotFoundException inner = ex.InnerException + as FileNotFoundException; + Assert.IsNotNull (inner, "#6"); + Assert.AreEqual (typeof (FileNotFoundException), inner.GetType (), "#7"); + Assert.IsNotNull (inner.FileName, "#8"); + Assert.AreEqual (tempFile, inner.FileName, "#9"); + Assert.IsNull (inner.InnerException, "#10"); + Assert.IsNotNull (inner.Message, "#11"); + } } } diff --git a/mcs/class/test-helpers/PathHelpers.cs b/mcs/class/test-helpers/PathHelpers.cs new file mode 100644 index 00000000000..a2b949a2398 --- /dev/null +++ b/mcs/class/test-helpers/PathHelpers.cs @@ -0,0 +1,68 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; + +namespace MonoTests.Helpers { + + /// + /// Represents a temporary directory. Creating an instance creates a directory at the specified path, + /// and disposing the instance deletes the directory. + /// + public sealed class TempDirectory : IDisposable + { + /// Gets the created directory's path. + public string Path { get; private set; } + + public TempDirectory () + : this (CreateTemporaryDirectory ()) + { + } + + public TempDirectory (string path) + { + Path = path; + } + + ~TempDirectory () + { + Dispose (); + } + + public void Dispose() + { + GC.SuppressFinalize (this); + DeleteDirectory (Path); + } + + // Tries to recursively delete the specified path. + // Doesn't throw exceptions if path is null, empty, or doesn't exist. + public static void DeleteDirectory (string path) + { + if (string.IsNullOrEmpty (path)) + return; + + if (Directory.Exists (path)) + Directory.Delete (path, true); + } + + // Creates a unique temporary directory. + // + // The calling method's type/method name will be a part of the + // returned path to make it somewhat nicer/more useful. + // + // This method is only meant for testing code, not production code (it + // will leave some temporary directories behind). + static string CreateTemporaryDirectory () + { + var name = string.Empty; + var calling_method = new StackFrame (2).GetMethod (); + if (calling_method != null) + name = calling_method.DeclaringType.FullName + "_" + calling_method.Name + "_"; + + var rv = global::System.IO.Path.Combine (global::System.IO.Path.GetTempPath (), name + Guid.NewGuid ().ToString ()); + Directory.CreateDirectory (rv); + return rv; + } + } +} -- 2.11.4.GIT