[tests] Fix tests which rely on temp dir location being a real dir
authorMarek Habersack <grendel@twistedcode.net>
Thu, 2 Jul 2015 15:59:06 +0000 (17:59 +0200)
committerMarek Habersack <grendel@twistedcode.net>
Wed, 15 Jul 2015 18:45:31 +0000 (20:45 +0200)
Android M (that's where I ran into it) makes the path returned by
Path.GetTempPath () a symlink to the real directory used for temporary
files. When the tests change the directory the symlink is followed and
subsequent reads of the current directory name will return the real dir
name which doesn't match what was constructed using the value returned by
GetTempPath ()

mcs/class/corlib/Test/System.IO/FileStreamTest.cs

index 92a3b0147ec80fe76c7f5d55969fa514889b8109..28080443ea07b9b343c23cd65a52fd26d529fa83 100644 (file)
@@ -87,12 +87,13 @@ namespace MonoTests.System.IO
                [Test]
                public void CtorFileNotFoundException_Mode_Open ()
                {
+                       const string file_name = "thisfileshouldnotexist.test";
                        // absolute path
-                       string path = TempFolder + DSC + "thisfileshouldnotexists.test";
+                       string path = TempFolder + DSC + file_name;
                        DeleteFile (path);
                        FileStream stream = null;
                        try {
-                               stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
+                               stream = new FileStream (TempFolder + DSC + file_name, FileMode.Open);
                                Assert.Fail ("#A1");
                        } catch (FileNotFoundException ex) {
                                Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
@@ -111,8 +112,15 @@ namespace MonoTests.System.IO
                        // relative path
                        string orignalCurrentDir = Directory.GetCurrentDirectory ();
                        Directory.SetCurrentDirectory (TempFolder);
+
+                       // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
+                       // the real directory name, not that of the TempFolder symlink and the test will fail
+                       // (happens e.g. on Android M)
+                       string realTempDir = Directory.GetCurrentDirectory ();
+                       path = realTempDir + DSC + file_name;
+
                        try {
-                               stream = new FileStream ("thisfileshouldnotexists.test", FileMode.Open);
+                               stream = new FileStream (file_name, FileMode.Open);
                                Assert.Fail ("#B1");
                        } catch (FileNotFoundException ex) {
                                Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
@@ -135,8 +143,9 @@ namespace MonoTests.System.IO
                [Test]
                public void CtorFileNotFoundException_Mode_Truncate ()
                {
+                       const string file_name = "thisfileshouldNOTexist.test";
                        // absolute path
-                       string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
+                       string path = TempFolder + DSC + file_name;
                        DeleteFile (path);
                        FileStream stream = null;
                        try {
@@ -159,8 +168,15 @@ namespace MonoTests.System.IO
                        // relative path
                        string orignalCurrentDir = Directory.GetCurrentDirectory ();
                        Directory.SetCurrentDirectory (TempFolder);
+
+                       // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
+                       // the real directory name, not that of the TempFolder symlink and the test will fail
+                       // (happens e.g. on Android M)
+                       string realTempDir = Directory.GetCurrentDirectory ();
+                       path = realTempDir + DSC + file_name;
+
                        try {
-                               stream = new FileStream ("thisfileshouldNOTexists.test", FileMode.Truncate);
+                               stream = new FileStream (file_name, FileMode.Truncate);
                                Assert.Fail ("#B1");
                        } catch (FileNotFoundException ex) {
                                Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
@@ -183,8 +199,9 @@ namespace MonoTests.System.IO
                [Test]
                public void CtorIOException1 ()
                {
+                       const string file_name = "thisfileshouldexists.test";
                        // absolute path
-                       string path = TempFolder + DSC + "thisfileshouldexists.test";
+                       string path = TempFolder + DSC + file_name;
                        FileStream stream = null;
                        DeleteFile (path);
                        try {
@@ -209,11 +226,18 @@ namespace MonoTests.System.IO
                        // relative path
                        string orignalCurrentDir = Directory.GetCurrentDirectory ();
                        Directory.SetCurrentDirectory (TempFolder);
+
+                       // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
+                       // the real directory name, not that of the TempFolder symlink and the test will fail
+                       // (happens e.g. on Android M)
+                       string realTempDir = Directory.GetCurrentDirectory ();
+                       path = realTempDir + DSC + file_name;
+
                        try {
-                               stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
+                               stream = new FileStream (file_name, FileMode.CreateNew);
                                stream.Close ();
                                stream = null;
-                               stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
+                               stream = new FileStream (file_name, FileMode.CreateNew);
                                Assert.Fail ("#B1");
                        } catch (IOException ex) {
                                Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
@@ -327,8 +351,14 @@ namespace MonoTests.System.IO
                {
                        string orignalCurrentDir = Directory.GetCurrentDirectory ();
                        Directory.SetCurrentDirectory (TempFolder);
+
+                       // If TempFolder is a symlink, Mono will follow it and ex.FileName below will contain
+                       // the real directory name, not that of the TempFolder symlink and the test will fail
+                       // (happens e.g. on Android M)
+                       string realTempDir = Directory.GetCurrentDirectory ();
+                       
                        string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
-                       string fullPath = Path.Combine (TempFolder, relativePath);
+                       string fullPath = Path.Combine (realTempDir, relativePath);
                        try {
                                new FileStream (relativePath, FileMode.Open);
                                Assert.Fail ("#A1");