Skip to content

Commit

Permalink
[filesystem] Support for ZipFS (#292)
Browse files Browse the repository at this point in the history
<!--
Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors.
All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->
### Description

- Added support for  `ZipFS`



### Test Coverage

<!--
Please put an `x` in the correct box e.g. `[x]` to indicate the testing
coverage of this change.
-->

- [x]  This change is covered by existing or additional automated tests.
- [ ] Manual testing has been performed (and evidence provided) as
automated testing was not feasible.
- [ ] Additional tests are not required for this change (e.g.
documentation update).
  • Loading branch information
acabarbaye authored Aug 1, 2023
1 parent 6e82f33 commit 70ded98
Show file tree
Hide file tree
Showing 21 changed files with 2,188 additions and 45 deletions.
1 change: 1 addition & 0 deletions changes/20230724173139.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `[errortest]` Added utilities to check the error description
1 change: 1 addition & 0 deletions changes/20230724173259.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:recycle: `[filesystem]` Use `afero.ReadOnlyFS` rather than our own implementation when exposing `embed.FS`
1 change: 1 addition & 0 deletions changes/20230728174153.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `[filesystem]` Added zipFS to enable filesystem utilities on zip files
1 change: 1 addition & 0 deletions changes/20230731170709.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: Added module `[resource]` to define generic utilities regarding resource management
19 changes: 19 additions & 0 deletions utils/commonerrors/errortest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ func AssertError(t *testing.T, err error, expectedErrors ...error) bool {
return assert.Fail(t, fmt.Sprintf("Failed error assertion:\n actual: %v\n expected: %+v", err, expectedErrors))
}

// AssertErrorDescription asserts that the error description corresponds to one of the `expectedErrorDescriptions`
// This is a wrapper for commonerrors.CorrespondTo.
func AssertErrorDescription(t *testing.T, err error, expectedErrorDescriptions ...string) bool {
if commonerrors.CorrespondTo(err, expectedErrorDescriptions...) {
return true
}
return assert.Fail(t, fmt.Sprintf("Failed error description assertion:\n actual: %v\n expected: %+v", err, expectedErrorDescriptions))
}

// RequireError requires that the error is matching one of the `expectedErrors`
// This is a wrapper for commonerrors.Any.
func RequireError(t *testing.T, err error, expectedErrors ...error) {
Expand All @@ -27,3 +36,13 @@ func RequireError(t *testing.T, err error, expectedErrors ...error) {
}
t.FailNow()
}

// RequireErrorDescription requires that the error description corresponds to one of the `expectedErrorDescriptions`
// This is a wrapper for commonerrors.CorrespondTo.
func RequireErrorDescription(t *testing.T, err error, expectedErrorDescriptions ...string) {
t.Helper()
if commonerrors.CorrespondTo(err, expectedErrorDescriptions...) {
return
}
t.FailNow()
}
8 changes: 8 additions & 0 deletions utils/commonerrors/errortest/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ func TestAssertError(t *testing.T) {
AssertError(t, commonerrors.ErrUndefined, commonerrors.ErrNotFound, commonerrors.ErrMarshalling, commonerrors.ErrUndefined)
}

func TestAssertErrorDescription(t *testing.T) {
AssertErrorDescription(t, commonerrors.ErrUndefined, "undefined", "not found")
}

func TestRequireError(t *testing.T) {
RequireError(t, commonerrors.ErrUndefined, commonerrors.ErrNotFound, commonerrors.ErrMarshalling, commonerrors.ErrUndefined)
}

func TestRequireErrorContent(t *testing.T) {
RequireErrorDescription(t, commonerrors.ErrUndefined, "undefined", "not found")
}
20 changes: 3 additions & 17 deletions utils/filesystem/embedfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,17 @@ package filesystem
import (
"embed"
"fmt"
"os"

"github.com/spf13/afero"

"github.com/ARM-software/golang-utils/utils/commonerrors"
)

type embedFsAdapter struct {
afero.Fs
}

func (e *embedFsAdapter) OpenFile(name string, flag int, _ os.FileMode) (afero.File, error) {
if flag != os.O_RDONLY {
return nil, fmt.Errorf("%w: embed.FS is readonly", commonerrors.ErrUnsupported)
}
return e.Open(name)
}

func newEmbedFSAdapter(fs *embed.FS) (afero.Fs, error) {
if fs == nil {
return nil, fmt.Errorf("%w: missing filesystem", commonerrors.ErrUndefined)
}
return &embedFsAdapter{
Fs: afero.FromIOFS{
FS: *fs,
},
}, nil
return afero.NewReadOnlyFs(afero.FromIOFS{
FS: *fs,
}), nil
}
6 changes: 3 additions & 3 deletions utils/filesystem/embedfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func Test_embed_not_supported(t *testing.T) {
require.NoError(t, err)

_, err = efs.TempDir("testdata", "aaaa")
assert.True(t, commonerrors.CorrespondTo(err, "permission denied"))
errortest.AssertErrorDescription(t, err, "operation not permitted")

f, err := efs.OpenFile("testdata/embed/test.txt", os.O_RDWR, os.FileMode(0600))
defer func() {
Expand All @@ -146,10 +146,10 @@ func Test_embed_not_supported(t *testing.T) {
}
}()
require.Error(t, err)
errortest.AssertError(t, err, commonerrors.ErrUnsupported)
errortest.AssertErrorDescription(t, err, "operation not permitted")

err = efs.Chmod("testdata/embed/test.txt", os.FileMode(0600))
require.Error(t, err)
assert.True(t, commonerrors.CorrespondTo(err, "permission denied"))
errortest.AssertErrorDescription(t, err, "operation not permitted")

}
Loading

0 comments on commit 70ded98

Please sign in to comment.