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 a2ba1e0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 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
2 changes: 1 addition & 1 deletion pkg/openapiclient/local_crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func Test_localCRDsClient_Paths(t *testing.T) {
),
},
}, {
name: "invalid fs",
name: "not a directory",
fileSystems: []fs.FS{os.DirFS("../../invalid")},
wantErr: true,
}}
Expand Down
22 changes: 17 additions & 5 deletions pkg/openapiclient/local_schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ func (k *localSchemasClient) Paths() (map[string]openapi.GroupVersion, error) {
}
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 +53,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 +68,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 errors.Is(err, fs.ErrNotExist)
}
return false
}
18 changes: 9 additions & 9 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 Expand Up @@ -91,13 +90,14 @@ func Test_localSchemasClient_Paths(t *testing.T) {
"apis/storage.k8s.io/v1",
),
}, {
name: "invalid fs",
fs: os.DirFS("../../invalid"),
want: sets.New[string](),
}, {
name: "not a dir",
fs: os.DirFS("../../testcases/schemas/error_not_a_dir"),
name: "invalid fs",
fs: os.DirFS("../../invalid"),
want: sets.New[string](),
wantErr: true,
}, {
name: "not a dir",
fs: os.DirFS("../../testcases/schemas/error_not_a_dir"),
want: sets.New[string](),
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit a2ba1e0

Please sign in to comment.