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

File manager #183

Merged
merged 2 commits into from
May 30, 2023
Merged

File manager #183

merged 2 commits into from
May 30, 2023

Conversation

godrei
Copy link
Contributor

@godrei godrei commented May 30, 2023

This PR adds OpenReaderIfExists and ReadDirEntryNames to fileutil.FileManager.

OpenReaderIfExists

Can be used to obtain an io.Reader for a file. There were two motivations behind this function:
1, make file existence test easier:

with the stdlib:

f, err := os.Open(pth)
if err != nil {
  if os.IsNotExist(err) {
    // file doesn't exist
  } else {
    // other error
  }
}

with OpenReaderIfExists:

r, err := filemanager.OpenReaderIfExists(pth)
if err != nil {
   // other error
}
if r == nil {
   // file doesn't exist
}

example: https://github.com/bitrise-io/go-flutter/pull/2/files#diff-aa912ba3e8a7389b3812ef842a8be72c6d7dcda3e0ae4e884a75072e38e72753R13

2, Make testing easier:

filemanager := new(mocks.FileManager)
filemanager.On("OpenFile", "<SOME_PATH>").Return(strings.NewReader(<MOCK_CONTENT>), nil)

ReadDirEntryNames

Supports mocking os.ReadDir.

example usage: https://github.com/bitrise-io/go-flutter/pull/2/files#diff-1878dc8ea63a80f862179a2c298fce835f3ed2952f88b608a5b80f0fe8a30600R74

@godrei godrei marked this pull request as ready for review May 30, 2023 08:13
@godrei godrei merged commit 06a0eeb into master May 30, 2023
4 checks passed
@godrei godrei deleted the file-manager branch May 30, 2023 11:48
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 this pull request may close these issues.

None yet

2 participants