From d52c2e327fef530f36800ec50f717bbba10b1424 Mon Sep 17 00:00:00 2001 From: Anthony Dahanne Date: Wed, 23 Oct 2024 19:36:24 -0400 Subject: [PATCH] Close #513 * replace deprecated calls to os.ReadFile/WriteFile * fix "handles empty names gracefully" test: if a property did not make it into dataflow-configuration-metadata.properties then it's not considered, even if it was found in spring-configuration-metadata.json * add another test: similarly, if you have the properties alpha and beta in spring-configuration-metadata.json, we'll only consider alpha if dataflow-configuration-metadata.properties only has alpha --- boot/configuration_metadata.go | 5 +- boot/configuration_metadata_test.go | 139 +++++++++++++--------------- 2 files changed, 66 insertions(+), 78 deletions(-) diff --git a/boot/configuration_metadata.go b/boot/configuration_metadata.go index 88733a5..268926c 100644 --- a/boot/configuration_metadata.go +++ b/boot/configuration_metadata.go @@ -20,7 +20,6 @@ import ( "archive/zip" "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -135,10 +134,10 @@ func DataFlowConfigurationExists(path string) (bool, error) { func NewDataFlowConfigurationMetadata(path string, metadata ConfigurationMetadata) (ConfigurationMetadata, error) { file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties") - b, err := ioutil.ReadFile(file) + b, err := os.ReadFile(file) if os.IsNotExist(err) { file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties") - b, err = ioutil.ReadFile(file) + b, err = os.ReadFile(file) if os.IsNotExist(err) { return ConfigurationMetadata{}, nil } else if err != nil { diff --git a/boot/configuration_metadata_test.go b/boot/configuration_metadata_test.go index 0c0bb0b..b0d7738 100644 --- a/boot/configuration_metadata_test.go +++ b/boot/configuration_metadata_test.go @@ -17,7 +17,6 @@ package boot_test import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -38,7 +37,7 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) { it.Before(func() { var err error - path, err = ioutil.TempDir("", "configuration-metadata") + path, err = os.MkdirTemp("", "configuration-metadata") Expect(err).NotTo(HaveOccurred()) }) @@ -47,47 +46,42 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) { }) context("from path", func() { - // ... (existing test cases) - it("returns dataflow decoded contents with names", func() { - Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), - []byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), - []byte("configuration-properties.names=alpha"), 0644)).To(Succeed()) + it("returns empty if file does not exist", func() { + Expect(boot.NewConfigurationMetadataFromPath(path)).To(BeZero()) + }) - cm, err := boot.NewConfigurationMetadataFromPath(path) - Expect(err).NotTo(HaveOccurred()) + it("returns decoded contents", func() { + Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), + []byte(`{ "groups": [ { "name": "alpha" } ] }`), 0644)).To(Succeed()) - Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{ - Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}}, + Expect(boot.NewConfigurationMetadataFromPath(path)).To(Equal(boot.ConfigurationMetadata{ + Groups: []boot.Group{{Name: "alpha"}}, })) }) - it("returns combined dataflow decoded contents with classes and names", func() { + it("returns dataflow decoded contents", func() { Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), - []byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), - []byte("configuration-properties.classes=alpha\nconfiguration-properties.names=beta"), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), + []byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), + []byte("configuration-properties.classes=alpha"), 0644)) cm, err := boot.NewConfigurationMetadataFromPath(path) Expect(err).NotTo(HaveOccurred()) Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{ - Groups: []boot.Group{ - {Name: "alpha", SourceType: "alpha"}, - {Name: "beta", SourceType: "beta"}, - }, + Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}}, })) }) - it("handles empty names gracefully", func() { + it("returns dataflow decoded contents handling trailing comma correctly", func() { Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), - []byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), - []byte("configuration-properties.names="), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), + []byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta" } ] }`), 0644)) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), + []byte("configuration-properties.classes=alpha,"), 0644)) cm, err := boot.NewConfigurationMetadataFromPath(path) Expect(err).NotTo(HaveOccurred()) @@ -96,64 +90,61 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) { Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}}, })) }) - }) - -func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) { - var ( - Expect = NewWithT(t).Expect - - path string - ) - - it.Before(func() { - var err error - - path, err = ioutil.TempDir("", "configuration-metadata") - Expect(err).NotTo(HaveOccurred()) - }) - - it.After(func() { - Expect(os.RemoveAll(path)).To(Succeed()) - }) + it("returns dataflow decoded contents", func() { + Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), + []byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"), + []byte("configuration-properties.classes=alpha"), 0644)).To(Succeed()) - context("from path", func() { + cm, err := boot.NewConfigurationMetadataFromPath(path) + Expect(err).NotTo(HaveOccurred()) - it("returns empty if file does not exist", func() { - Expect(boot.NewConfigurationMetadataFromPath(path)).To(BeZero()) + Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{ + Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}}, + })) }) - it("returns decoded contents", func() { + it("returns dataflow decoded contents with names", func() { Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), - []byte(`{ "groups": [ { "name": "alpha" } ] }`), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), + []byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), + []byte("configuration-properties.names=alpha"), 0644)).To(Succeed()) - Expect(boot.NewConfigurationMetadataFromPath(path)).To(Equal(boot.ConfigurationMetadata{ - Groups: []boot.Group{{Name: "alpha"}}, + cm, err := boot.NewConfigurationMetadataFromPath(path) + Expect(err).NotTo(HaveOccurred()) + + Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{ + Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}}, })) }) - it("returns dataflow decoded contents", func() { + it("returns combined dataflow decoded contents with classes and names", func() { Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), - []byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), - []byte("configuration-properties.classes=alpha"), 0644)) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), + []byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), + []byte("configuration-properties.classes=alpha\nconfiguration-properties.names=beta"), 0644)).To(Succeed()) cm, err := boot.NewConfigurationMetadataFromPath(path) Expect(err).NotTo(HaveOccurred()) Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{ - Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}}, + Groups: []boot.Group{ + {Name: "alpha", SourceType: "alpha"}, + {Name: "beta", SourceType: "beta"}, + }, })) }) - it("returns dataflow decoded contents handling trailing comma correctly", func() { + it("handles missing names properly", func() { Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), - []byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta" } ] }`), 0644)) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), - []byte("configuration-properties.classes=alpha,"), 0644)) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), + []byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), + []byte("configuration-properties.names=alpha"), 0644)).To(Succeed()) cm, err := boot.NewConfigurationMetadataFromPath(path) Expect(err).NotTo(HaveOccurred()) @@ -163,19 +154,17 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) { })) }) - it("returns dataflow decoded contents", func() { + it("handles empty names gracefully", func() { Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), - []byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"), - []byte("configuration-properties.classes=alpha"), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"), + []byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed()) + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"), + []byte("configuration-properties.names="), 0644)).To(Succeed()) cm, err := boot.NewConfigurationMetadataFromPath(path) Expect(err).NotTo(HaveOccurred()) - Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{ - Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}}, - })) + Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{})) }) }) @@ -203,14 +192,14 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) { it("returns true if the file does exist", func() { Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"), + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"), []byte("configuration-properties.classes=alpha"), 0644)).To(Succeed()) Expect(boot.DataFlowConfigurationExists(path)).To(BeFalse()) }) it("return false and the error if the file cannot be read", func() { Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed()) - Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"), + Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"), []byte("configuration-properties.classes=alpha"), 0644)).To(Succeed()) Expect(os.Chmod(filepath.Join(path, "META-INF"), 0000)).To(Succeed())