Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate FileInfo and DirectoryInfo upon create and delete #34229

Merged
merged 12 commits into from
Apr 10, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ public void MoveTo(string destDirName)
Invalidate();
}

public override void Delete() => FileSystem.RemoveDirectory(FullPath, recursive: false);
public override void Delete() => Delete(recursive: false);

public void Delete(bool recursive) => FileSystem.RemoveDirectory(FullPath, recursive);
public void Delete(bool recursive)
{
FileSystem.RemoveDirectory(FullPath, recursive);
Invalidate();
}
}
}
13 changes: 11 additions & 2 deletions src/libraries/System.IO.FileSystem/src/System/IO/FileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,18 @@ public FileInfo CopyTo(string destFileName, bool overwrite)
return new FileInfo(destinationPath, isNormalized: true);
}

public FileStream Create() => File.Create(NormalizedPath);
public FileStream Create()
{
FileStream fileStream = File.Create(NormalizedPath);
Invalidate();
return fileStream;
}

public override void Delete() => FileSystem.DeleteFile(FullPath);
public override void Delete()
{
FileSystem.DeleteFile(FullPath);
Invalidate();
}

public FileStream Open(FileMode mode)
=> Open(mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void ExistsThenDelete_ReturnsDefaultValues()
}

[Fact]
public void TimesStillSetAfterDelete()
public void TimesResetAfterDelete()
{
T item = GetExistingItem();

Expand All @@ -63,11 +63,13 @@ public void TimesStillSetAfterDelete()
times.Add(timeFunction, timeFunction.Getter(item));
}

// Deleting shouldn't change any info state
// Deleting should refresh state
item.Delete();

Assert.All(times, time =>
{
Assert.Equal(time.Value, time.Key.Getter(item));
// We check that all the file times have been refreshed
Assert.NotEqual(time.Key.Getter(item), time.Value);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,14 @@ public void CreateDirectoryWithAttributes()
testInfo.Create();
testInfo.Attributes = FileAttributes.Directory | FileAttributes.Normal;
}

[Fact]
public void CreateInvalidatesDirectoryInfo()
{
string testDir = Path.Combine(GetTestFilePath(), "DirectoryCreate");
DirectoryInfo testDirectoryInfo = new DirectoryInfo(testDir);
testDirectoryInfo.Create();
Assert.True(testDirectoryInfo.Exists);
}
}
}
17 changes: 7 additions & 10 deletions src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ protected override void Delete(string path)
}

[Fact]
public void ExistsDoesntRefreshOnDelete()
public void DeleteInvalidatesDirectoryInfo()
{
DirectoryInfo dir = Directory.CreateDirectory(Path.Combine(TestDirectory, Path.GetRandomFileName()));

Assert.True(dir.Exists);

dir.Delete();

Assert.True(dir.Exists);
dir.Refresh();
Assert.False(dir.Exists);
string testDir = Path.Combine(GetTestFilePath(), "DirectoryCreate");
DirectoryInfo testDirectoryInfo = new DirectoryInfo(testDir);
testDirectoryInfo.Create();
Assert.True(testDirectoryInfo.Exists);
testDirectoryInfo.Delete();
Assert.False(testDirectoryInfo.Exists);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/libraries/System.IO.FileSystem/tests/FileInfo/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ public void FullNameUpdatesOnCreate()
}
}

[Fact]
public void FileInfoInvalidAfterCreate()
{
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
string testFilePath = Path.Combine(testDir.FullName, GetTestFileName());
FileInfo info = new FileInfo(testFilePath);
using (FileStream fileStream = info.Create())
{
Assert.True(info.Exists);
}
}

#endregion
}
}
15 changes: 15 additions & 0 deletions src/libraries/System.IO.FileSystem/tests/FileInfo/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,20 @@ protected override void Delete(string path)
{
new FileInfo(path).Delete();
}

[Fact]
public void FileInfoInvalidatedOnDelete()
{
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
string testFilePath = Path.Combine(testDir.FullName, GetTestFileName());
FileInfo info = new FileInfo(testFilePath);
using (FileStream fileStream = info.Create())
{
Assert.True(info.Exists);
}

info.Delete();
Assert.False(info.Exists);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ public void DeleteAfterEnumerate_TimesStillSet()

DateTime afterTime = DateTime.UtcNow.AddSeconds(1);

// Deleting doesn't change any info state
info.Delete();
ValidateSetTimes(info, beforeTime, afterTime);
Assert.Equal(DateTime.FromFileTimeUtc(0), info.LastAccessTimeUtc);
}


Expand Down