Skip to content

Add FileStreamFactory and support for mocking FileStreams#177

Merged
fgreinacher merged 12 commits intoTestableIO:masterfrom
augustoproiete-forks:add-file-stream-factory-etc
Jul 17, 2018
Merged

Add FileStreamFactory and support for mocking FileStreams#177
fgreinacher merged 12 commits intoTestableIO:masterfrom
augustoproiete-forks:add-file-stream-factory-etc

Conversation

@augustoproiete
Copy link
Copy Markdown
Contributor

Add IFileStreamFactory, FileStreamFactory, and MockFileStreamFactory to allow creating instances of FileStream with more advanced options not exposed by FileBase and FileInfo, such as bufferSize, FileOptions, FileSecurity, and FileSystemRights, as well as opening FileStream from a SafeFileHandle instead of a file path, etc.

I've included all public constructors/overloads of the FileStream in IFileStreamFactory and consequently in FileStreamFactory

@manne manne mentioned this pull request Dec 3, 2016
@THammond9 THammond9 mentioned this pull request May 11, 2017
@jpreese jpreese added the type: enhancement Issues that propose new functionality label Jul 9, 2018
@jpreese
Copy link
Copy Markdown
Member

jpreese commented Jul 9, 2018

@caioproiete thanks for the PR! At first glance, this looks like it's in pretty decent shape, but does have some merge conflicts.

@fgreinacher
Copy link
Copy Markdown
Contributor

@jpreese I resolved the merge conflicts and updated it to handle the changes to MockFileStream.

For me it looks good now - and fills an major functionality gap.

@augustoproiete
Copy link
Copy Markdown
Contributor Author

@jpreese You're welcome! You know... The PR had zero merge conflicts 2 years ago when I created it 🙃 haha anyway thanks for reviewing it

@fgreinacher Awesome! Thanks for helping to move this one forward!

@jpreese
Copy link
Copy Markdown
Member

jpreese commented Jul 10, 2018

@caioproiete you're alive! Again, truly sorry for the delay in all of this. We're working on getting some momentum on this project again. Your PR is very appreciated.

@augustoproiete
Copy link
Copy Markdown
Contributor Author

@jpreese 😄 No worries! Great to see some traction on this project again! It's a great library!

FileBase File { get; }
DirectoryBase Directory { get; }
IFileInfoFactory FileInfo { get; }
IFileStreamFactory FileStream { get; }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is my only concern. Since we're adding onto IFileSystem, it's a breaking change.. but is anyone really writing their own classes using IFileSystem?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, same goes for 07227bc... I also don't think this will be a big problem for people. Whenever we are done with the old issues/PRs we should again think about release strategy, versioning, issue tagging, etc...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To be clear. I would merge this one and as a follow-up take care about our future strategy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I would agree. It's probably an incredibly small subset of users it would impact.

Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options);
#endif

#if NET40 || NETSTANDARD_20
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need the NET40/NETSTANDARD check here? I don't see a mirroring check in the MockFileStreamFactory

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, because the “real” implementation doesn’t support these overloads on NS1.4.

return new MockFileStream(mockFileSystem, path, GetStreamType(mode, access));
}

public Stream Create(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity fileSecurity)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The interface has a preprocessor check, but this is exposed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oops, will add a check here.

@jpreese
Copy link
Copy Markdown
Member

jpreese commented Jul 12, 2018

Have a couple eye raises around the NET40/NETSTANDARD stuff, seems to be some inconcistencies.

@fgreinacher
Copy link
Copy Markdown
Contributor

OK, feedback is addressed. I also mixed in some minor cleanups :)

[Test]
[TestCase(FileMode.Create)]
[TestCase(FileMode.Append)]
public void MockFileStreamFactory_Create_string_FileMode__ShouldReturnStreamForNonExistingFile(FileMode fileMode)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test name could use some love. MockFileStreamFactory_Create_ShouldReturnStreamForNonExistingFile()?

Quick tangent... I'm not a huge fan of the idea that we include the name of the class in the name of the test. In a greenfield scenario, I'd name this test Create_FileDoesNotExist_ReturnsStream -- but I think the suggestion follows more suite with how our tests are currently named.

[Test]
[TestCase(FileMode.Create)]
[TestCase(FileMode.Append)]
public void MockFileStreamFactory_Create_string_FileMode__ShouldReturnStreamForExistingFile(FileMode fileMode)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

More test name love.

[Serializable]
internal sealed class FileStreamFactory : IFileStreamFactory
{
public Stream Create(string path, FileMode mode)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not a nit, but thought this was interesting.
https://ericlippert.com/2014/09/15/internal-or-public/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 That's a good read, thanks! I also prefer public in these cases, but I could never provide non-subjective reasons for that

Copy link
Copy Markdown
Member

@jpreese jpreese left a comment

Choose a reason for hiding this comment

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

Looks fine to me overall, just a small issue with the test naming.

@fgreinacher
Copy link
Copy Markdown
Contributor

a6dc64d addresses the feedback and simplifies the test cases a bit more.

@fgreinacher
Copy link
Copy Markdown
Contributor

Merging this - if there is additional feedback on the changes just add it here and I’ll address it in a follow-up.

@fgreinacher fgreinacher merged commit 3fc0b03 into TestableIO:master Jul 17, 2018
@augustoproiete augustoproiete deleted the add-file-stream-factory-etc branch July 18, 2018 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement Issues that propose new functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants