diff --git a/pkg/devfile/parser/reader.go b/pkg/devfile/parser/reader.go index a97e775c..6cd4da10 100644 --- a/pkg/devfile/parser/reader.go +++ b/pkg/devfile/parser/reader.go @@ -17,6 +17,7 @@ package parser import ( "bytes" + "fmt" "io" "github.com/devfile/library/pkg/util" @@ -53,8 +54,9 @@ type KubernetesResources struct { // ReadKubernetesYaml reads a yaml Kubernetes file from either the Path, URL or Data provided. // It returns all the parsed Kubernetes objects as an array of interface. // Consumers interested in the Kubernetes resources are expected to Unmarshal -// it to the struct of the respective Kubernetes resource. -func ReadKubernetesYaml(src YamlSrc, fs afero.Afero) ([]interface{}, error) { +// it to the struct of the respective Kubernetes resource. If a Path is being passed, +// provide a filesystem, otherwise nil can be passed in +func ReadKubernetesYaml(src YamlSrc, fs *afero.Afero) ([]interface{}, error) { var data []byte var err error @@ -65,6 +67,9 @@ func ReadKubernetesYaml(src YamlSrc, fs afero.Afero) ([]interface{}, error) { return nil, errors.Wrapf(err, "failed to download file %q", src.URL) } } else if src.Path != "" { + if fs == nil { + return nil, fmt.Errorf("cannot read from %s because fs passed in was nil", src.Path) + } data, err = fs.ReadFile(src.Path) if err != nil { return nil, errors.Wrapf(err, "failed to read yaml from path %q", src.Path) diff --git a/pkg/devfile/parser/reader_test.go b/pkg/devfile/parser/reader_test.go index 897d3a13..05c09456 100644 --- a/pkg/devfile/parser/reader_test.go +++ b/pkg/devfile/parser/reader_test.go @@ -71,7 +71,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) { tests := []struct { name string src YamlSrc - fs afero.Afero + fs *afero.Afero wantErr bool wantDeploymentNames []string wantServiceNames []string @@ -84,7 +84,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) { src: YamlSrc{ URL: "http://" + serverIP, }, - fs: fs, + fs: nil, wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"}, wantServiceNames: []string{"service-sample", "service-sample-2"}, wantRouteNames: []string{"route-sample", "route-sample-2"}, @@ -96,19 +96,27 @@ func TestReadAndParseKubernetesYaml(t *testing.T) { src: YamlSrc{ Path: "../../../tests/yamls/resources.yaml", }, - fs: fs, + fs: &fs, wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"}, wantServiceNames: []string{"service-sample", "service-sample-2"}, wantRouteNames: []string{"route-sample", "route-sample-2"}, wantIngressNames: []string{"ingress-sample", "ingress-sample-2"}, wantOtherNames: []string{"pvc-sample", "pvc-sample-2"}, }, + { + name: "Read the YAML from the Path with no fs passed", + src: YamlSrc{ + Path: "../../../tests/yamls/resources.yaml", + }, + fs: nil, + wantErr: true, + }, { name: "Read the YAML from the Data", src: YamlSrc{ Data: data, }, - fs: fs, + fs: nil, wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"}, wantServiceNames: []string{"service-sample", "service-sample-2"}, wantRouteNames: []string{"route-sample", "route-sample-2"}, @@ -120,7 +128,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) { src: YamlSrc{ URL: "http://badurl", }, - fs: fs, + fs: nil, wantErr: true, }, { @@ -128,7 +136,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) { src: YamlSrc{ Path: "$%^&", }, - fs: fs, + fs: &fs, wantErr: true, }, { @@ -136,7 +144,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) { src: YamlSrc{ Data: badData, }, - fs: fs, + fs: nil, wantErr: true, }, }