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

fix: MockFileInfo.Exists no longer returns stale cached data #828

Merged
merged 1 commit into from
Mar 22, 2022

Conversation

siprbaum
Copy link
Contributor

In 5216e0d ("fix: make Mock{File,Directory}Info cache file attributes
(#791)", 2022-01-09) a cache of the MockFileData was introduced, to mimic
the behaviour that a FileInfo object will not read the data from disk
but instead also remembers its internal state, which can be updated by
calling its Refresh method. This internal (or cached state) is
invalidated when calling the methods Create, Delete and MoveTo
methods, as can be seen by the Invalidate calls in
https://github.com/dotnet/runtime/blob/8766a1cf1ed6c3b69cef50154139699b72fb52c5/src/libraries/System.Private.CoreLib/src/System/IO/FileInfo.cs

In the PR #791 it was overlooked to also invalidate the cached data
in these methods. This commit fixes it by invalidating the cached data
only for Create, CreateText and Delete.

The two MoveTo variants are not changed as I could not manage to find
any behaviour which is broken without that invalidation.
Most likely reason is that the MockFileData is updated to represent the
destination file ofter the move, so invalidating is not needed.

Fixes #822

In 5216e0d ("fix: make Mock{File,Directory}Info cache file attributes
(TestableIO#791)", 2022-01-09) a cache of the MockFileData was introduced, to mimic
the behaviour that a FileInfo object will not read the data from disk
but instead also remembers its internal state, which can be updated by
calling its `Refresh` method. This internal (or cached state) is
invalidated when calling the methods `Create`, `Delete` and `MoveTo`
methods, as can be seen by the `Invalidate` calls in
https://github.com/dotnet/runtime/blob/8766a1cf1ed6c3b69cef50154139699b72fb52c5/src/libraries/System.Private.CoreLib/src/System/IO/FileInfo.cs

In the PR TestableIO#791 it was overlooked to also invalidate the cached data
in these methods. This commit fixes it by invalidating the cached data
only for `Create`, `CreateText` and `Delete`.

The two `MoveTo` variants are not changed as I could not manage to find
any behaviour which is broken without that invalidation.
Most likely reason is that the  MockFileData is updated to represent the
destination file ofter the move, so invalidating is not needed.

Fixes TestableIO#822
@fgreinacher
Copy link
Contributor

Thanks a ton 👍

@github-actions
Copy link

This is addressed in release v16.1.24.

@rcdailey
Copy link
Contributor

This same issue exists for IDirectoryInfo. I'm going to open a pull request with similar changes for that class.

rcdailey added a commit to rcdailey/System.IO.Abstractions that referenced this pull request Jun 30, 2022
When invoking the `Create()` and `Delete()` methods on
`MockDirectoryInfo`, the `Exists` property should intrinsically invoke
`Refresh()` the next time it is called. This is necessary to prevent
returning stale state about the directory after those operations.

Additional test cases have been added for `MoveTo()` as well, to ensure
it also not returning stale state.

For context, this change was motivated by PR TestableIO#828 where similar issues
were solved in `MockFileInfo`.
rcdailey added a commit to rcdailey/System.IO.Abstractions that referenced this pull request Jul 11, 2022
When invoking the `Create()` and `Delete()` methods on
`MockDirectoryInfo`, the `Exists` property should intrinsically invoke
`Refresh()` the next time it is called. This is necessary to prevent
returning stale state about the directory after those operations.

Additional test cases have been added for `MoveTo()` as well, to ensure
it also not returning stale state.

For context, this change was motivated by PR TestableIO#828 where similar issues
were solved in `MockFileInfo`.
mergify bot pushed a commit that referenced this pull request Jul 19, 2022
When invoking the `Create()` and `Delete()` methods on
`MockDirectoryInfo`, the `Exists` property should intrinsically invoke
`Refresh()` the next time it is called. This is necessary to prevent
returning stale state about the directory after those operations.

Additional test cases have been added for `MoveTo()` as well, to ensure
it also not returning stale state.

For context, this change was motivated by PR #828 where similar issues
were solved in `MockFileInfo`.

Co-authored-by: Florian Greinacher <florian@greinacher.de>
fgreinacher added a commit that referenced this pull request Jul 19, 2022
When invoking the `Create()` and `Delete()` methods on
`MockDirectoryInfo`, the `Exists` property should intrinsically invoke
`Refresh()` the next time it is called. This is necessary to prevent
returning stale state about the directory after those operations.

Additional test cases have been added for `MoveTo()` as well, to ensure
it also not returning stale state.

For context, this change was motivated by PR #828 where similar issues
were solved in `MockFileInfo`.

Co-authored-by: Florian Greinacher <florian@greinacher.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: released Issues that are released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MockFileSystem created file don't exist
3 participants