Skip to content

Commit 7e12f5c

Browse files
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
1 parent e54c32f commit 7e12f5c

File tree

2 files changed

+66
-78
lines changed

2 files changed

+66
-78
lines changed

boot/configuration_metadata.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"archive/zip"
2121
"encoding/json"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625
"strings"
@@ -135,10 +134,10 @@ func DataFlowConfigurationExists(path string) (bool, error) {
135134

136135
func NewDataFlowConfigurationMetadata(path string, metadata ConfigurationMetadata) (ConfigurationMetadata, error) {
137136
file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties")
138-
b, err := ioutil.ReadFile(file)
137+
b, err := os.ReadFile(file)
139138
if os.IsNotExist(err) {
140139
file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties")
141-
b, err = ioutil.ReadFile(file)
140+
b, err = os.ReadFile(file)
142141
if os.IsNotExist(err) {
143142
return ConfigurationMetadata{}, nil
144143
} else if err != nil {

boot/configuration_metadata_test.go

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package boot_test
1818

1919
import (
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"testing"
@@ -38,7 +37,7 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
3837
it.Before(func() {
3938
var err error
4039

41-
path, err = ioutil.TempDir("", "configuration-metadata")
40+
path, err = os.MkdirTemp("", "configuration-metadata")
4241
Expect(err).NotTo(HaveOccurred())
4342
})
4443

@@ -47,47 +46,42 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
4746
})
4847

4948
context("from path", func() {
50-
// ... (existing test cases)
5149

52-
it("returns dataflow decoded contents with names", func() {
53-
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
54-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
55-
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
56-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
57-
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())
50+
it("returns empty if file does not exist", func() {
51+
Expect(boot.NewConfigurationMetadataFromPath(path)).To(BeZero())
52+
})
5853

59-
cm, err := boot.NewConfigurationMetadataFromPath(path)
60-
Expect(err).NotTo(HaveOccurred())
54+
it("returns decoded contents", func() {
55+
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
56+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
57+
[]byte(`{ "groups": [ { "name": "alpha" } ] }`), 0644)).To(Succeed())
6158

62-
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
63-
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
59+
Expect(boot.NewConfigurationMetadataFromPath(path)).To(Equal(boot.ConfigurationMetadata{
60+
Groups: []boot.Group{{Name: "alpha"}},
6461
}))
6562
})
6663

67-
it("returns combined dataflow decoded contents with classes and names", func() {
64+
it("returns dataflow decoded contents", func() {
6865
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
69-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
70-
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
71-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
72-
[]byte("configuration-properties.classes=alpha\nconfiguration-properties.names=beta"), 0644)).To(Succeed())
66+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
67+
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644))
68+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
69+
[]byte("configuration-properties.classes=alpha"), 0644))
7370

7471
cm, err := boot.NewConfigurationMetadataFromPath(path)
7572
Expect(err).NotTo(HaveOccurred())
7673

7774
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
78-
Groups: []boot.Group{
79-
{Name: "alpha", SourceType: "alpha"},
80-
{Name: "beta", SourceType: "beta"},
81-
},
75+
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
8276
}))
8377
})
8478

85-
it("handles empty names gracefully", func() {
79+
it("returns dataflow decoded contents handling trailing comma correctly", func() {
8680
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
87-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
88-
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
89-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
90-
[]byte("configuration-properties.names="), 0644)).To(Succeed())
81+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
82+
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta" } ] }`), 0644))
83+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
84+
[]byte("configuration-properties.classes=alpha,"), 0644))
9185

9286
cm, err := boot.NewConfigurationMetadataFromPath(path)
9387
Expect(err).NotTo(HaveOccurred())
@@ -96,64 +90,61 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
9690
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
9791
}))
9892
})
99-
})
100-
10193

102-
func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
103-
var (
104-
Expect = NewWithT(t).Expect
105-
106-
path string
107-
)
108-
109-
it.Before(func() {
110-
var err error
111-
112-
path, err = ioutil.TempDir("", "configuration-metadata")
113-
Expect(err).NotTo(HaveOccurred())
114-
})
115-
116-
it.After(func() {
117-
Expect(os.RemoveAll(path)).To(Succeed())
118-
})
94+
it("returns dataflow decoded contents", func() {
95+
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
96+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
97+
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
98+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
99+
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
119100

120-
context("from path", func() {
101+
cm, err := boot.NewConfigurationMetadataFromPath(path)
102+
Expect(err).NotTo(HaveOccurred())
121103

122-
it("returns empty if file does not exist", func() {
123-
Expect(boot.NewConfigurationMetadataFromPath(path)).To(BeZero())
104+
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
105+
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
106+
}))
124107
})
125108

126-
it("returns decoded contents", func() {
109+
it("returns dataflow decoded contents with names", func() {
127110
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
128-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
129-
[]byte(`{ "groups": [ { "name": "alpha" } ] }`), 0644)).To(Succeed())
111+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
112+
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
113+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
114+
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())
130115

131-
Expect(boot.NewConfigurationMetadataFromPath(path)).To(Equal(boot.ConfigurationMetadata{
132-
Groups: []boot.Group{{Name: "alpha"}},
116+
cm, err := boot.NewConfigurationMetadataFromPath(path)
117+
Expect(err).NotTo(HaveOccurred())
118+
119+
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
120+
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
133121
}))
134122
})
135123

136-
it("returns dataflow decoded contents", func() {
124+
it("returns combined dataflow decoded contents with classes and names", func() {
137125
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
138-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
139-
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644))
140-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
141-
[]byte("configuration-properties.classes=alpha"), 0644))
126+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
127+
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
128+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
129+
[]byte("configuration-properties.classes=alpha\nconfiguration-properties.names=beta"), 0644)).To(Succeed())
142130

143131
cm, err := boot.NewConfigurationMetadataFromPath(path)
144132
Expect(err).NotTo(HaveOccurred())
145133

146134
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
147-
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
135+
Groups: []boot.Group{
136+
{Name: "alpha", SourceType: "alpha"},
137+
{Name: "beta", SourceType: "beta"},
138+
},
148139
}))
149140
})
150141

151-
it("returns dataflow decoded contents handling trailing comma correctly", func() {
142+
it("handles missing names properly", func() {
152143
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
153-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
154-
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta" } ] }`), 0644))
155-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
156-
[]byte("configuration-properties.classes=alpha,"), 0644))
144+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
145+
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
146+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
147+
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())
157148

158149
cm, err := boot.NewConfigurationMetadataFromPath(path)
159150
Expect(err).NotTo(HaveOccurred())
@@ -163,19 +154,17 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
163154
}))
164155
})
165156

166-
it("returns dataflow decoded contents", func() {
157+
it("handles empty names gracefully", func() {
167158
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
168-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
169-
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
170-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
171-
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
159+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
160+
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
161+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
162+
[]byte("configuration-properties.names="), 0644)).To(Succeed())
172163

173164
cm, err := boot.NewConfigurationMetadataFromPath(path)
174165
Expect(err).NotTo(HaveOccurred())
175166

176-
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
177-
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
178-
}))
167+
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{}))
179168
})
180169
})
181170

@@ -203,14 +192,14 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
203192

204193
it("returns true if the file does exist", func() {
205194
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
206-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
195+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
207196
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
208197
Expect(boot.DataFlowConfigurationExists(path)).To(BeFalse())
209198
})
210199

211200
it("return false and the error if the file cannot be read", func() {
212201
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
213-
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
202+
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
214203
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
215204

216205
Expect(os.Chmod(filepath.Join(path, "META-INF"), 0000)).To(Succeed())

0 commit comments

Comments
 (0)