FileInfo.MoveTo() throws DirectoryNotFoundException rather than
authorNiklas Therning <niklas@therning.org>
Tue, 23 Aug 2016 14:19:11 +0000 (16:19 +0200)
committerNiklas Therning <niklas@therning.org>
Wed, 24 Aug 2016 13:02:59 +0000 (15:02 +0200)
FileNotFoundException on Mono on Windows and on .NET when target doesn't exist

FileInfoTest.MoveTo_DestFileName_DirectoryDoesNotExist() expects
FileNotFoundException to be thrown but on Windows a DirectoryNotFoundException
is thrown instead.

This patch changes MoveFile() in mono/io-layer/io.c to set the last error to
ERROR_PATH_NOT_FOUND rather than ERROR_FILE_NOT_FOUND when the destination
path doesn't exist. This results in a DirectoryNotFoundException being thrown
in managed code rather than FileNotFoundException. This is the behaviour
observed in .NET.

Also updated FileInfoTest.MoveTo_DestFileName_DirectoryDoesNotExist() to check
for DirectoryNotFoundException rather than FileNotFoundException.

mcs/class/corlib/Test/System.IO/FileInfoTest.cs
mono/io-layer/io.c

index 50755fa0dc5bf4ac8b4ee615910d3872d75d1dd4..e4000ea33d81b2877940c9a4dcd3007d63dca7a9 100644 (file)
@@ -758,9 +758,9 @@ namespace MonoTests.System.IO
                                try {
                                        info.MoveTo (destFile);
                                        Assert.Fail ("#1");
-                               } catch (FileNotFoundException ex) {
+                               } catch (DirectoryNotFoundException ex) {
                                        // Could not find a part of the path
-                                       Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
+                                       Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
                                        Assert.IsNull (ex.InnerException, "#3");
                                        Assert.IsNotNull (ex.Message, "#4");
                                }
index 3a275dc83513af2c1aedabb890fa8a763fc29d61..496efa70bf2c349b406ddb0a722b5e6f5f57a18a 100644 (file)
@@ -1994,9 +1994,14 @@ gboolean MoveFile (const gunichar2 *name, const gunichar2 *dest_name)
                case EXDEV:
                        /* Ignore here, it is dealt with below */
                        break;
-                       
+
+               case ENOENT:
+                       /* We already know src exists. Must be dest that doesn't exist. */
+                       _wapi_set_last_path_error_from_errno (NULL, utf8_dest_name);
+                       break;
+
                default:
-                       _wapi_set_last_path_error_from_errno (NULL, utf8_name);
+                       _wapi_set_last_error_from_errno ();
                }
        }