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

feat: Implemented MockDirectory.CreateTempSubDirectory #964

Merged
merged 15 commits into from
Apr 13, 2023
Merged

feat: Implemented MockDirectory.CreateTempSubDirectory #964

merged 15 commits into from
Apr 13, 2023

Conversation

gregington
Copy link
Contributor

Implemented MockDirectory.CreateTempSubdirectory(). This implementation generates a random 6 character string with an optional user specified prefix and creates the directory in the temporary directory returned by Path.GetTempPath().

The 6 random characters seems to be the algorithm used on my platform (Mac) by the System.IO implementation.

Notes on this PR

  1. There's a potential temporary directory namespace of $62^6$ or about 56.8 billion. As we get closer to this limit, the likelihood of generating a duplicate increases as well as the likelihood of slowdowns of the random generation. At the extreme case, if all possible temporary directories are created, creating a random directory name will effectively become an infinite loop. I assume we don't need to handle conditions close to the limit.
  2. MockDirectory now contains a Random instance to generate random temp directory names. Random.Shared can't be used as it was introduced in .NET 6 it it appears we are supporting earlier .NET versions.
  3. MockDirectory is marked as [Serializable], however the Random instance cannot be serialized. I have marked the Random instance as [NonSerialized] and created a new Random instance on deserializing. The state of the random number generator will be different when deserialized. I don't think this is an issue as there was no way to specify the seed of the Random instance when creating the MockDirectory instance, so there's no way to generate the same sequence of random directories again (besides a very slim chance).
  4. I have had to add <EnableUnsafeBinaryFormatterSerialization> on the test project and #pragma warning disable SYSLIB0011 on the deserialize method to allow testing that deserialization of a MockDirectory also instantiates a new random number generator. Deserialization is heavily deprecated and this is only on test classes, so I am assuming this is OK.

@gregington gregington changed the title Implemented MockDirectory.CreateTempSubDirectory feat: Implemented MockDirectory.CreateTempSubDirectory Apr 9, 2023
Copy link
Member

@vbreuss vbreuss left a comment

Choose a reason for hiding this comment

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

@gregington : Thank you for working on this missing implementation!

@gregington
Copy link
Contributor Author

I changed one of the tests to hopefully work with MacOS and whether the /private directory prefix is returned for temporary directories.

I don't have access to a Windows platform so I couldn't reproduce failure with the . in the temporary filename. In any case, the implementation has now been changed to delegate to Path.CreateTemporaryFileName() so the generation of file names is no longer the concern of that method, so the tests regarding temporary file naming have been removed.

Copy link
Member

@vbreuss vbreuss left a comment

Choose a reason for hiding this comment

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

Looks good to me...

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.

LGTM, thanks!

@mergify mergify bot merged commit 941f487 into TestableIO:main Apr 13, 2023
@gregington gregington deleted the create-temp-subdirectory branch April 13, 2023 10:00
vbreuss referenced this pull request in Testably/Testably.Abstractions Apr 13, 2023
…19.2.11 (#291)

[![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.9` -> `19.2.11` |

---

### Release Notes

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

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

##### What's Changed

- chore(deps): update dependency dotnet-sdk to v7.0.203 by
[@&#8203;renovate](https://togithub.com/renovate) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/967](https://togithub.com/TestableIO/System.IO.Abstractions/pull/967)
- feat: Implemented MockDirectory.CreateTempSubDirectory by
[@&#8203;gregington](https://togithub.com/gregington) in
[https://github.com/TestableIO/System.IO.Abstractions/pull/964](https://togithub.com/TestableIO/System.IO.Abstractions/pull/964)

##### New Contributors

- [@&#8203;gregington](https://togithub.com/gregington) made their first
contribution in
[https://github.com/TestableIO/System.IO.Abstractions/pull/964](https://togithub.com/TestableIO/System.IO.Abstractions/pull/964)

**Full Changelog**:
TestableIO/System.IO.Abstractions@v19.2.9...v19.2.11

</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:eyJjcmVhdGVkSW5WZXIiOiIzNS40MC4wIiwidXBkYXRlZEluVmVyIjoiMzUuNDAuMCJ9-->

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

This is addressed in release v19.2.11.

@github-actions github-actions bot added the state: released Issues that are released label Apr 18, 2023
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