forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_test.go
75 lines (62 loc) · 1.78 KB
/
entry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package servicebindings_test
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/packit/v2/servicebindings"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testEntry(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
entry *servicebindings.Entry
tmpDir string
)
it.Before(func() {
var err error
tmpDir, err = os.MkdirTemp("", "entry")
Expect(err).NotTo(HaveOccurred())
entryPath := filepath.Join(tmpDir, "entry")
Expect(os.WriteFile(entryPath, []byte("some data"), os.ModePerm)).To(Succeed())
entry = servicebindings.NewEntry(entryPath)
})
it.After(func() {
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})
context("ReadBytes", func() {
it("returns the raw bytes of the entry", func() {
Expect(entry.ReadBytes()).To(Equal([]byte("some data")))
})
})
context("ReadString", func() {
it("returns the string value of the entry", func() {
Expect(entry.ReadString()).To(Equal("some data"))
})
})
context("usage as an io.ReadCloser", func() {
it("is assignable to io.ReadCloser", func() {
var _ io.ReadCloser = entry
})
it("can be read again after closing", func() {
data, err := io.ReadAll(entry)
Expect(err).NotTo(HaveOccurred())
Expect(entry.Close()).To(Succeed())
Expect(data).To(Equal([]byte("some data")))
data, err = io.ReadAll(entry)
Expect(err).NotTo(HaveOccurred())
Expect(entry.Close()).To(Succeed())
Expect(data).To(Equal([]byte("some data")))
})
it("can be closed multiple times in a row", func() {
_, err := io.ReadAll(entry)
Expect(err).NotTo(HaveOccurred())
Expect(entry.Close()).To(Succeed())
Expect(entry.Close()).To(Succeed())
})
it("can be closed if never read from", func() {
Expect(entry.Close()).To(Succeed())
})
})
}