-
Notifications
You must be signed in to change notification settings - Fork 137
Consolidate test helpers for io/fs
#1906
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
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package fakefs | ||
|
|
||
| import ( | ||
| "io/fs" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestFile(t *testing.T) { | ||
| var fakefile fs.File = File{ | ||
| FileInfo: FileInfo{ | ||
| FakeName: "file", | ||
| }, | ||
| } | ||
|
|
||
| _, err := fakefile.Read([]byte{}) | ||
| assert.ErrorIs(t, err, ErrNotImplemented) | ||
|
|
||
| fi, err := fakefile.Stat() | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, "file", fi.Name()) | ||
|
|
||
| err = fakefile.Close() | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestFS(t *testing.T) { | ||
| var fakefs fs.FS = FS{ | ||
| "file": File{}, | ||
| } | ||
|
|
||
| _, err := fakefs.Open("doesntexist") | ||
| assert.ErrorIs(t, err, fs.ErrNotExist) | ||
|
|
||
| _, err = fakefs.Open("file") | ||
| assert.NoError(t, err) | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package filer | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "io/fs" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/libs/fakefs" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestFakeFiler_Read(t *testing.T) { | ||
| f := NewFakeFiler(map[string]fakefs.FileInfo{ | ||
| "file": {}, | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| r, err := f.Read(ctx, "file") | ||
| if !assert.NoError(t, err) { | ||
| return | ||
| } | ||
|
|
||
| contents, err := io.ReadAll(r) | ||
| if !assert.NoError(t, err) { | ||
| return | ||
| } | ||
|
|
||
| // Contents of every file is "foo". | ||
| assert.Equal(t, "foo", string(contents)) | ||
| } | ||
|
|
||
| func TestFakeFiler_Read_NotFound(t *testing.T) { | ||
| f := NewFakeFiler(map[string]fakefs.FileInfo{ | ||
| "foo": {}, | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| _, err := f.Read(ctx, "bar") | ||
| assert.ErrorIs(t, err, fs.ErrNotExist) | ||
| } | ||
|
|
||
| func TestFakeFiler_ReadDir_NotFound(t *testing.T) { | ||
| f := NewFakeFiler(map[string]fakefs.FileInfo{ | ||
| "dir1": {FakeDir: true}, | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| _, err := f.ReadDir(ctx, "dir2") | ||
| assert.ErrorIs(t, err, fs.ErrNotExist) | ||
| } | ||
|
|
||
| func TestFakeFiler_ReadDir_NotADirectory(t *testing.T) { | ||
| f := NewFakeFiler(map[string]fakefs.FileInfo{ | ||
| "file": {}, | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| _, err := f.ReadDir(ctx, "file") | ||
| assert.ErrorIs(t, err, fs.ErrInvalid) | ||
| } | ||
|
|
||
| func TestFakeFiler_ReadDir(t *testing.T) { | ||
| f := NewFakeFiler(map[string]fakefs.FileInfo{ | ||
| "dir1": {FakeDir: true}, | ||
| "dir1/file2": {}, | ||
| "dir1/dir2": {FakeDir: true}, | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| entries, err := f.ReadDir(ctx, "dir1/") | ||
| if !assert.NoError(t, err) { | ||
| return | ||
| } | ||
|
|
||
| if !assert.Len(t, entries, 2) { | ||
pietern marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
|
|
||
| // The entries are sorted by name. | ||
| assert.Equal(t, "dir2", entries[0].Name()) | ||
| assert.True(t, entries[0].IsDir()) | ||
| assert.Equal(t, "file2", entries[1].Name()) | ||
| assert.False(t, entries[1].IsDir()) | ||
| } | ||
|
|
||
| func TestFakeFiler_Stat(t *testing.T) { | ||
| f := NewFakeFiler(map[string]fakefs.FileInfo{ | ||
| "file": {}, | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| info, err := f.Stat(ctx, "file") | ||
| if !assert.NoError(t, err) { | ||
| return | ||
| } | ||
|
|
||
| assert.Equal(t, "file", info.Name()) | ||
| } | ||
|
|
||
| func TestFakeFiler_Stat_NotFound(t *testing.T) { | ||
| f := NewFakeFiler(map[string]fakefs.FileInfo{ | ||
| "foo": {}, | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| _, err := f.Stat(ctx, "bar") | ||
| assert.ErrorIs(t, err, fs.ErrNotExist) | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.