Skip to content

Commit

Permalink
Fix deletion tests on Windows 10 1903. (dotnet/corefx#38186)
Browse files Browse the repository at this point in the history
In the 1903 release of Windows 10 the deletion behavior has changed. Previously, the filename would be reserved until all open handles were closed after being marked for deletion. Now the filename is immediately released.

Commit migrated from dotnet/corefx@a4cf9a4
  • Loading branch information
JeremyKuhne authored and stephentoub committed Jun 4, 2019
1 parent 9ec8d59 commit 34a08f4
Showing 1 changed file with 20 additions and 9 deletions.
Expand Up @@ -19,9 +19,10 @@ public void FileShareDeleteNew()
{
Assert.True(File.Exists(fileName));
File.Delete(fileName);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // file sharing restriction limitations on Unix
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.True(File.Exists(fileName));
// Prior to 1903 Windows would not delete the filename until the last file handle is closed.
Assert.Equal(PlatformDetection.IsWindows10Version1903OrGreater, !File.Exists(fileName));
}
}

Expand Down Expand Up @@ -56,9 +57,10 @@ public void FileShareDeleteExisting()
using (FileStream fs = CreateFileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Delete))
{
File.Delete(fileName);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // file sharing restriction limitations on Unix
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.True(File.Exists(fileName));
// Prior to 1903 Windows would not delete the filename until the last file handle is closed.
Assert.Equal(PlatformDetection.IsWindows10Version1903OrGreater, !File.Exists(fileName));
}
}

Expand Down Expand Up @@ -104,16 +106,25 @@ public void FileShareDeleteExistingMultipleClients()
{
File.Delete(fileName);
Assert.Equal(0, fs2.ReadByte());
Assert.True(File.Exists(fileName), $"'{fileName}' should still exist after calling delete with two handles open.");

// Prior to 1903 Windows would not delete the filename until the last file handle is closed.
Assert.Equal(PlatformDetection.IsWindows10Version1903OrGreater, !File.Exists(fileName));
}

Assert.Equal(0, fs1.ReadByte());
fs1.WriteByte(0xFF);

// Any attempt to reopen a file in pending-delete state will return Access-denied
Assert.Throws<UnauthorizedAccessException>(() => CreateFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite));

Assert.True(File.Exists(fileName), $"'{fileName}' should still exist after calling delete with inner filestream closed.");
if (PlatformDetection.IsWindows10Version1903OrGreater)
{
// On 1903 the filename is immediately released after delete is called
Assert.Throws<FileNotFoundException>(() => CreateFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite));
}
else
{
// Any attempt to reopen a file in pending-delete state will return Access-denied
Assert.Throws<UnauthorizedAccessException>(() => CreateFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite));
Assert.True(File.Exists(fileName), $"'{fileName}' should still exist after calling delete with inner filestream closed.");
}
}

Assert.False(File.Exists(fileName));
Expand Down

0 comments on commit 34a08f4

Please sign in to comment.