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

MockFileSystem extensions #32

Closed
rcdailey opened this issue Jan 6, 2023 · 2 comments · Fixed by TestableIO/System.IO.Abstractions#951
Closed

MockFileSystem extensions #32

rcdailey opened this issue Jan 6, 2023 · 2 comments · Fixed by TestableIO/System.IO.Abstractions#951

Comments

@rcdailey
Copy link

rcdailey commented Jan 6, 2023

Would you find the below extension methods useful? If so, I am happy to open a pull request. I'm hesitant to do so right away because I'm not sure if these should be extension methods in this library or if I should contribute them as actual class members upstream (upstream being the actual IO.Abstractions package).

I found these extension methods to be absolutely invaluable in my own code base in conjunction with the Extensions package. It eliminates a lot of boilerplate in my unit tests, namely these:

var fs = new MockFileSystem();
var myDir = fs.CurrentDirectory().SubDirectory("dir");
var myFile = fs.CurrentDirectory().File("file.txt");

// Creating empty files
fs.AddFile(myFile.FullName, new MockFileData(""));
// Becomes
fs.AddEmptyFile(foo);

// Specifying `FullName` all over the place...
fs.AddDirectory(myDir.FullName);
// Becomes
fs.AddDirectory(myDir);

If you could provide some guidance and feedback, I'm happy to move this to a PR. Thanks for your time!

public static class MockFileSystemExtensions
{
    public static void AddEmptyFile(this MockFileSystem fs, string path)
    {
        fs.AddFile(path, new MockFileData(""));
    }

    public static void AddEmptyFile(this MockFileSystem fs, IFileInfo path)
    {
        fs.AddEmptyFile(path.FullName);
    }

    public static void AddDirectory(this MockFileSystem fs, IDirectoryInfo path)
    {
        fs.AddDirectory(path.FullName);
    }

    public static void AddFile(this MockFileSystem fs, IFileInfo path, MockFileData data)
    {
        fs.AddFile(path.FullName, data);
    }

    public static MockFileData GetFile(this MockFileSystem fs, IFileInfo path)
    {
        return fs.GetFile(path.FullName);
    }
}
@gigi81
Copy link
Collaborator

gigi81 commented Jan 8, 2023

hi @rcdailey ,
The above looks usefull to me.
In my opinion, they should be part of the base package but I know @fgreinacher is not really into it.
@fgreinacher can you please review the above and see if it makes sense to add them to the TestableIO.System.IO.Abstractions.TestingHelpers library.

If not the only alternative is to create a second library in this repo, something like TestableIO.System.IO.Abstractions.TestingHelpers.Extensions, otherwise the normal extensions would depend on the testing library which doesn't make any sense.

@fgreinacher
Copy link
Contributor

I'm also fine adding them to MockFileSystem. The changes go well with existing methods like AddFileFromEmbeddedResource. I just don't want to change the core abstractions like IFile or IDirectory.

rcdailey added a commit to rcdailey/System.IO.Abstractions that referenced this issue Feb 25, 2023
- `AddEmptyFile()` added which is a convenience wrapper for `AddFile()`
  with an empty `MockFileData`.
- `AddFile()` overload added which consumes an `IFileInfo`.
- `AddDirectory()` overload added which consumes an `IDirectoryInfo`.
- `GetFile()` overload added which consumes an `IFileInfo`.

Unit test added for `AddEmptyFile()` but not the others, as the test
code for them would have very little value.

Fixes TestableIO/System.IO.Abstractions.Extensions#32
rcdailey added a commit to rcdailey/System.IO.Abstractions that referenced this issue Feb 27, 2023
- `AddEmptyFile()` added which is a convenience wrapper for `AddFile()`
  with an empty `MockFileData`.
- `AddFile()` overload added which consumes an `IFileInfo`.
- `AddDirectory()` overload added which consumes an `IDirectoryInfo`.
- `GetFile()` overload added which consumes an `IFileInfo`.

Unit test added for `AddEmptyFile()` but not the others, as the test
code for them would have very little value.

Fixes TestableIO/System.IO.Abstractions.Extensions#32
rcdailey added a commit to rcdailey/System.IO.Abstractions that referenced this issue Feb 27, 2023
- `AddEmptyFile()` added which is a convenience wrapper for `AddFile()`
  with an empty `MockFileData`.
- `AddFile()` overload added which consumes an `IFileInfo`.
- `AddDirectory()` overload added which consumes an `IDirectoryInfo`.
- `GetFile()` overload added which consumes an `IFileInfo`.

Unit test added for `AddEmptyFile()` but not the others, as the test
code for them would have very little value.

Fixes TestableIO/System.IO.Abstractions.Extensions#32
fgreinacher added a commit to TestableIO/System.IO.Abstractions that referenced this issue Mar 2, 2023
- `AddEmptyFile()` added which is a convenience wrapper for `AddFile()`
  with an empty `MockFileData`.
- `AddFile()` overload added which consumes an `IFileInfo`.
- `AddDirectory()` overload added which consumes an `IDirectoryInfo`.
- `GetFile()` overload added which consumes an `IFileInfo`.

Unit test added for `AddEmptyFile()` but not the others, as the test
code for them would have very little value.

Fixes TestableIO/System.IO.Abstractions.Extensions#32

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
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants