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

Conversation

Jlalond
Copy link
Contributor

@Jlalond Jlalond commented Mar 28, 2020

This is the change discussed in #31425

@wfurt

@@ -50,6 +50,12 @@ private void Init(string originalPath, string? fullPath = null, string? fileName
_isNormalized = isNormalized;
}

private void DeleteAndInvalidate(bool recurisve)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: recurisve

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here I was thinking I could spell

@@ -235,8 +241,8 @@ public void MoveTo(string destDirName)
Invalidate();
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public override void Delete() => DeleteAndInvalidate(false);
public override void Delete() => Delete(false);

You do not really need a new method for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Changed

@@ -141,6 +141,7 @@ internal long LengthCore
}
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra new line?

@@ -235,8 +235,12 @@ public void MoveTo(string destDirName)
Invalidate();
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public override void Delete() => Delete(false);
public override void Delete() => Delete(recursive: false);

info.Delete();
ValidateSetTimes(info, beforeTime, afterTime);
Assert.True(info.LastAccessTimeUtc > afterTime);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to use Assert.InRange. If the assert fails, we'll then get meaningful informationa about why.

Also, what about the other times? ValidateSetTimes can't be used to validate those?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some debugging I actually think we should bin this test. On my local machine the invalidated date time ends up being 01/01/1601 (W10), I could change it so the time isn't in the series, or is simple different than both but I think the over-all goal of the test has been invalidated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we deleted the file. I think we have a test somewhere that validates the time we return for a non-existent file. Ideally we'd do the same check here. Otherwise, we can just delete the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub after some digging I actually think something is incorrectly happening here. https://github.com/dotnet/runtime/blob/master/src/libraries/System.IO.FileSystem/src/System/IO/FileSystemInfo.Windows.cs#L111 the underlying OS specific filesysteminfo is checking if it's been initialized, and stepping through it's set to 0, vs -1 so I'm investigating that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redacting my statement, I still think we should delete the test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't we want to validate the times behave as they would for a nonexistent file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense to me too, but should be assert that it isn't in the range of those two times? Or just that the file has been defaulted to the system default

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should've noticed that, eek. Implemented!

DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
string testFilePath = Path.Combine(testDir.FullName, GetTestFileName());
FileInfo info = new FileInfo(testFilePath);
using(FileStream fileStream = info.Create())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using(FileStream fileStream = info.Create())
using (FileStream fileStream = info.Create())

DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
string testFilePath = Path.Combine(testDir.FullName, GetTestFileName());
FileInfo info = new FileInfo(testFilePath);
using(FileStream fileStream = info.Create())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using(FileStream fileStream = info.Create())
using (FileStream fileStream = info.Create())

@Jlalond
Copy link
Contributor Author

Jlalond commented Mar 31, 2020

@stephentoub I think I implemented all your linting and I got all the tests working old and new, could I get a final review from you or @carlossanlop / @JeremyKuhne

Thanks!

info.Delete();
ValidateSetTimes(info, beforeTime, afterTime);
Assert.NotInRange(info.LastAccessTimeUtc, beforeTime, afterTime);
Assert.Equal(info.LastAccessTimeUtc, DateTime.FromFileTimeUtc(0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two arguments here should be swapped; the first is expected, the second is actual.

info.Delete();
ValidateSetTimes(info, beforeTime, afterTime);
Assert.NotInRange(info.LastAccessTimeUtc, beforeTime, afterTime);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert can be deleted.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of small comments, otherwise LGTM. Thanks.

@Jlalond
Copy link
Contributor Author

Jlalond commented Apr 10, 2020

@jaredpar would you mind bumping this build

@stephentoub stephentoub reopened this Apr 10, 2020
@Jlalond
Copy link
Contributor Author

Jlalond commented Apr 10, 2020

@stephentoub I flipped the unit test and removed the useless Assert, so I think we're good to merge on my end

@danmoseley danmoseley merged commit 1b683ec into dotnet:master Apr 10, 2020
@danmoseley
Copy link
Member

thank you @Jlalond

@Jlalond
Copy link
Contributor Author

Jlalond commented Apr 10, 2020

Anytime @danmosemsft! I'll pick up a new issue shortly

@Jlalond Jlalond deleted the fileinfo-delete branch April 10, 2020 16:48
@danmoseley
Copy link
Member

@Jlalond sounds good to me - we have plenty marked up for grabs.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants