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: Refresh FileInfo & DirectoryInfo after adding to MockFileSystem #984

Merged
merged 3 commits into from
May 12, 2023

Conversation

rcdailey
Copy link
Contributor

@rcdailey rcdailey commented May 8, 2023

Modify the IFileInfo and IDirectoryInfo overloads of AddFile(), AddEmptyFile(), or AddDirectory() in MockFileSystem, to call Refresh() on those objects after the paths they refer to are created.

Modify the `IFileInfo` and `IDirectoryInfo` overloads of `AddFile()`,
`AddEmptyFile()`, or `AddDirectory()` in `MockFileSystem`, to call
`Refresh()` on those objects after the paths they refer to are created.
@rcdailey rcdailey force-pushed the mock-fs-add-file-refresh branch from 47cfa07 to d3e823c Compare May 10, 2023 13:26
@rcdailey
Copy link
Contributor Author

Latest push has no changes; just rebased to pull in latest on main.

Copy link
Contributor

@fgreinacher fgreinacher left a comment

Choose a reason for hiding this comment

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

Thanks, makes sense!

@mergify mergify bot merged commit 49747fa into TestableIO:main May 12, 2023
vbreuss referenced this pull request in Testably/Testably.Abstractions May 12, 2023
…19.2.26 (#306)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
|
[TestableIO.System.IO.Abstractions](https://togithub.com/TestableIO/System.IO.Abstractions)
| nuget | patch | `19.2.22` -> `19.2.26` |

---

### Release Notes

<details>
<summary>TestableIO/System.IO.Abstractions</summary>

###
[`v19.2.26`](https://togithub.com/TestableIO/System.IO.Abstractions/releases/tag/v19.2.26)

##### What's Changed

- fix: Refresh FileInfo & DirectoryInfo after adding to MockFileSystem
by [@&#8203;rcdailey](https://togithub.com/rcdailey) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/984](https://togithub.com/TestableIO/System.IO.Abstractions/pull/984)

**Full Changelog**:
TestableIO/System.IO.Abstractions@v19.2.25...v19.2.26

###
[`v19.2.25`](https://togithub.com/TestableIO/System.IO.Abstractions/releases/tag/v19.2.25)

#### What's Changed

- chore(deps): update dependency githubactionstestlogger to v2.1.0 by
[@&#8203;renovate](https://togithub.com/renovate) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/986](https://togithub.com/TestableIO/System.IO.Abstractions/pull/986)
- chore(deps): update dependency nerdbank.gitversioning to v3.6.132 by
[@&#8203;renovate](https://togithub.com/renovate) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/987](https://togithub.com/TestableIO/System.IO.Abstractions/pull/987)
- feat: Add Flush(bool) to FileSystemStream. by
[@&#8203;gregington](https://togithub.com/gregington) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/985](https://togithub.com/TestableIO/System.IO.Abstractions/pull/985)

**Full Changelog**:
TestableIO/System.IO.Abstractions@v19.2.22...v19.2.25

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/Testably/Testably.Abstractions).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS43OS4xIiwidXBkYXRlZEluVmVyIjoiMzUuNzkuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
vbreuss referenced this pull request in Testably/Testably.Abstractions May 12, 2023
…19.2.26 (#306)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
|
[TestableIO.System.IO.Abstractions](https://togithub.com/TestableIO/System.IO.Abstractions)
| nuget | patch | `19.2.22` -> `19.2.26` |

---

### Release Notes

<details>
<summary>TestableIO/System.IO.Abstractions</summary>

###
[`v19.2.26`](https://togithub.com/TestableIO/System.IO.Abstractions/releases/tag/v19.2.26)

##### What's Changed

- fix: Refresh FileInfo & DirectoryInfo after adding to MockFileSystem
by [@&#8203;rcdailey](https://togithub.com/rcdailey) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/984](https://togithub.com/TestableIO/System.IO.Abstractions/pull/984)

**Full Changelog**:
TestableIO/System.IO.Abstractions@v19.2.25...v19.2.26

###
[`v19.2.25`](https://togithub.com/TestableIO/System.IO.Abstractions/releases/tag/v19.2.25)

#### What's Changed

- chore(deps): update dependency githubactionstestlogger to v2.1.0 by
[@&#8203;renovate](https://togithub.com/renovate) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/986](https://togithub.com/TestableIO/System.IO.Abstractions/pull/986)
- chore(deps): update dependency nerdbank.gitversioning to v3.6.132 by
[@&#8203;renovate](https://togithub.com/renovate) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/987](https://togithub.com/TestableIO/System.IO.Abstractions/pull/987)
- feat: Add Flush(bool) to FileSystemStream. by
[@&#8203;gregington](https://togithub.com/gregington) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/985](https://togithub.com/TestableIO/System.IO.Abstractions/pull/985)

**Full Changelog**:
TestableIO/System.IO.Abstractions@v19.2.22...v19.2.25

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/Testably/Testably.Abstractions).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS43OS4xIiwidXBkYXRlZEluVmVyIjoiMzUuNzkuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@github-actions
Copy link

This is addressed in release v19.2.26.

@github-actions github-actions bot added the state: released Issues that are released label May 17, 2023
@rcdailey
Copy link
Contributor Author

@fgreinacher Thanks for merging this, I really appreciate it!

One thing I'm noticing, is that this doesn't cover all use cases. For example, if there are other IFileInfo objects pointing at the same file, they do not get refreshed. I know one of the driving philosophies for the mock filesystem is that it should model the behavior of .NET's file system objects. To that end, I'm not sure how this is officially handled with real FileInfo objects. If I were just "solving the problem", I'd probably go for some observable behavior where 'Info' objects subscribe to MockFileSystem for file events based on a particular path, that way all of those objects will get refreshed when a file is added. Again, however, if the official version of FileSystem does not do this, then I don't that would be an acceptable solution here, either.

Do you have any insight here you can share? This is somewhat a pain point I only experience in unit tests, having to manually refresh file info objects.

Thanks for any feedback you can share.

@vbreuss
Copy link
Member

vbreuss commented May 21, 2023

@rcdailey :
The functionality on the real file system is a bit complicated:

  • .NET Framework does not update the exists cache at all, so the Exists property gets set on the first read and never updated afterwards
  • .NET Core evaluates the Exists cache also on first read, but updates it for when calling Create() for the current instance (but not for other instances, that point to the same location)

The following test is successful on the real file system on all platforms:

	[Fact]
	public void Create_ShouldRefreshExistsCacheForCurrentItem_ExceptOnNetFramework()
	{
		string path = "file-path-to-check",
		IFileInfo sut1 = FileSystem.FileInfo.New(path);
		IFileInfo sut2 = FileSystem.FileInfo.New(path);
		IFileInfo sut3 = FileSystem.FileInfo.New(path);
		sut1.Exists.Should().BeFalse();
		sut2.Exists.Should().BeFalse();
		// Do not call Exists for `sut3`

		using FileSystemStream stream = sut1.Create();

		if (Test.IsNetFramework)
		{
			sut1.Exists.Should().BeFalse();
			sut2.Exists.Should().BeFalse();
			sut3.Exists.Should().BeFalse();
		}
		else
		{
			sut1.Exists.Should().BeTrue();
			sut2.Exists.Should().BeFalse();
			sut3.Exists.Should().BeTrue();
		}

		FileSystem.File.Exists(path).Should().BeTrue();
	}

For reference see Testably/Testably.Abstractions#313

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.

3 participants