feat: emit parent CreateDirectory in dictionary-ctor fix#20
Merged
Conversation
The MockFileSystem dictionary constructor auto-creates each entry's
parent directory. Testably's File.WriteAllText does not, so the
rewritten code threw DirectoryNotFoundException at runtime whenever a
seeded path had a non-root parent. The code fix now emits one
Directory.CreateDirectory call per unique non-root parent before the
WriteAll* follow-ups. Literal-string keys only; non-literal keys are
left to the user. Root paths ("/file") and Windows drive roots ("C:")
are suppressed.
Removes unused TypeExtensions.GloballyQualified helpers — they were 0% covered because nothing referenced them. Adds four AddDrive code-fix tests that exercise the previously-untested receiver-declaration branches in IsRetargetableMockFileSystemReceiver: local variable (VariableDeclarator), property (PropertyDeclaration), method-return (MethodDeclaration), and nullable property (NullableType). The existing tests only covered parameter-declared receivers.
|
There was a problem hiding this comment.
Pull request overview
This PR updates the migration code fix so dictionary-seeded MockFileSystem rewrites preserve parent-directory creation before file writes.
Changes:
- Adds parent directory creation for literal dictionary keys with non-root parents.
- Adds tests for parent creation and additional
AddDrivereceiver shapes. - Updates example migration comments and removes unused type-extension helpers.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
SystemIOAbstractionsCodeFixProvider.cs |
Adds parent-directory follow-up generation and path-parent helper logic. |
ConstructorTests.cs |
Adds constructor rewrite tests for parent directories and non-literal keys. |
AccessorMethodTests.cs |
Adds AddDrive rewrite tests for local, property, method, and nullable receivers. |
SystemIOAbstractionsMigrationExamples.cs |
Updates example comments to reflect emitted parent directory creation. |
TypeExtensions.cs |
Deletes unused symbol display extension helpers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+773
to
+774
| public MockFileSystem? Fs { get; set; } | ||
| public void Run() => {|#0:Fs!.AddDrive("D:", new MockDriveData())|}; |
| seeded.File.WriteAllText("/work/readonly.txt", "readonly"); | ||
| seeded.File.SetAttributes("/work/readonly.txt", FileAttributes.ReadOnly); | ||
|
|
||
|
|
Comment on lines
+1014
to
+1018
| // Windows drive root ("C:" or "C:\" already collapsed to "C:") — skip. | ||
| if (parent.Length == 2 && parent[1] == ':' && IsAsciiLetter(parent[0])) | ||
| { | ||
| return null; | ||
| } |
Comment on lines
+662
to
+664
| // Local-variable receiver: declarator syntax is VariableDeclaratorSyntax. | ||
| // The `var` type annotation parses to an unqualified IdentifierName, so the | ||
| // using-swap can safely retarget the inferred MockFileSystem. |
|
This is addressed in release v0.2.0. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This pull request enhances the migration analyzer's code fixer for
MockFileSystemconstructors and drive migration, ensuring more accurate code transformations and improved test coverage. The main improvements are the addition of logic to emit parent directory creation statements when migrating file system entries, expanded unit tests for various migration scenarios, and cleanup of unused code.Enhancements to migration logic:
CreateDirectorycall for each unique non-root parent directory when migrating from theDictionary<string, MockFileData>constructor, preserving the original behavior of auto-creating parent directories. Non-literal keys are skipped, and only one call is emitted per shared parent.Expanded and improved unit tests:
CreateDirectorycalls for non-root parents, shared parents, and skips them for non-literal keys in file constructor scenarios.AddDrivetoWithDrivefor various receiver types (local variable, property, method return, nullable property).Codebase cleanup:
TypeExtensionshelper file, as its logic is no longer required.