Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Apr 25, 2024
1 parent 9fa5b31 commit 3bed0bb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
5 changes: 3 additions & 2 deletions pkg/openapiclient/local_crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func (k *localCRDsClient) Paths() (map[string]openapi.GroupVersion, error) {
for _, current := range k.fileSystems {
files, err := fs.ReadDir(current, ".")
if err != nil {
return nil, fmt.Errorf("error listing %s: %w", current, err)
if crossPlatformCheckDirExists(current, ".") {
return nil, fmt.Errorf("error listing: %w", err)
}
}

for _, f := range files {
path := f.Name()
if f.IsDir() {
Expand Down
6 changes: 5 additions & 1 deletion pkg/openapiclient/local_crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ func Test_localCRDsClient_Paths(t *testing.T) {
),
},
}, {
name: "invalid fs",
name: "does not exist",
fileSystems: []fs.FS{os.DirFS("../../invalid")},
want: map[string]sets.Set[string]{},
}, {
name: "not a directory",
fileSystems: []fs.FS{os.DirFS("../../testcases/schemas/error_not_a_dir")},
wantErr: true,
}}
for _, tt := range tests {
Expand Down
30 changes: 24 additions & 6 deletions pkg/openapiclient/local_schemas.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package openapiclient

import (
"errors"
"fmt"
"io/fs"
"os"
"path"
"strings"

Expand All @@ -30,16 +30,24 @@ func (k *localSchemasClient) Paths() (map[string]openapi.GroupVersion, error) {
if k.fs == nil {
return nil, nil
}
// check if '.' can be listed
if _, err := fs.ReadDir(k.fs, "."); err != nil {
if crossPlatformCheckDirExists(k.fs, ".") {
return nil, fmt.Errorf("error listing %s: %w", ".", err)
}
}
res := map[string]openapi.GroupVersion{}
apiGroups, err := fs.ReadDir(k.fs, "apis")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("failed reading local files dir %s: %w", "apis", err)
if err != nil {
if crossPlatformCheckDirExists(k.fs, "apis") {
return nil, fmt.Errorf("error listing %s: %w", "apis", err)
}
}
for _, f := range apiGroups {
groupPath := path.Join("apis", f.Name())
versions, err := fs.ReadDir(k.fs, groupPath)
if err != nil {
return nil, fmt.Errorf("failed reading local files dir %s: %w", groupPath, err)
return nil, fmt.Errorf("error listing %s: %w", groupPath, err)
}
for _, v := range versions {
if !utils.IsJson(v.Name()) {
Expand All @@ -51,8 +59,10 @@ func (k *localSchemasClient) Paths() (map[string]openapi.GroupVersion, error) {
}
}
coregroup, err := fs.ReadDir(k.fs, "api")
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("failed reading local files dir %s: %w", "api", err)
if err != nil {
if crossPlatformCheckDirExists(k.fs, "api") {
return nil, fmt.Errorf("error listing %s: %w", "api", err)
}
}
for _, v := range coregroup {
if !utils.IsJson(v.Name()) {
Expand All @@ -64,3 +74,11 @@ func (k *localSchemasClient) Paths() (map[string]openapi.GroupVersion, error) {
}
return res, nil
}

func crossPlatformCheckDirExists(f fs.FS, path string) bool {
_, err := fs.Stat(f, path)
if err != nil {
return !os.IsNotExist(err)
}
return true
}
5 changes: 2 additions & 3 deletions pkg/openapiclient/local_schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ func TestNewLocalSchemaFiles(t *testing.T) {

func Test_localSchemasClient_Paths(t *testing.T) {
tests := []struct {
name string
fs fs.FS
// dir string
name string
fs fs.FS
want sets.Set[string]
wantErr bool
}{{
Expand Down

0 comments on commit 3bed0bb

Please sign in to comment.