forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package_manager_configuration_manager_test.go
121 lines (105 loc) · 3.77 KB
/
package_manager_configuration_manager_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package npminstall_test
import (
"bytes"
"errors"
"path/filepath"
"testing"
npminstall "github.com/paketo-buildpacks/npm-install"
"github.com/paketo-buildpacks/npm-install/fakes"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/paketo-buildpacks/packit/v2/servicebindings"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testPackageManagerConfigurationManager(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
buffer *bytes.Buffer
bindingResolver *fakes.BindingResolver
packageManagerConfigurationManager npminstall.PackageManagerConfigurationManager
)
it.Before(func() {
bindingResolver = &fakes.BindingResolver{}
buffer = bytes.NewBuffer(nil)
packageManagerConfigurationManager = npminstall.NewPackageManagerConfigurationManager(bindingResolver, scribe.NewEmitter(buffer))
})
context("DeterminePath", func() {
context("when there is a configuration binding set", func() {
it.Before(func() {
bindingResolver.ResolveCall.Returns.BindingSlice = []servicebindings.Binding{
{
Name: "first",
Type: "some-typ",
Path: "some-binding-path",
Entries: map[string]*servicebindings.Entry{
"some-entry": servicebindings.NewEntry("some-path"),
},
},
}
})
it("returns a path to the configuration file", func() {
path, err := packageManagerConfigurationManager.DeterminePath("some-typ", "platform-dir", "some-entry")
Expect(err).NotTo(HaveOccurred())
Expect(path).To(Equal(filepath.Join("some-binding-path", "some-entry")))
Expect(bindingResolver.ResolveCall.Receives.Typ).To(Equal("some-typ"))
Expect(bindingResolver.ResolveCall.Receives.PlatformDir).To(Equal("platform-dir"))
Expect(buffer.String()).To(ContainSubstring("Loading service binding of type 'some-typ'"))
})
})
context("failure cases", func() {
context("when the binding resolver fails", func() {
it.Before(func() {
bindingResolver.ResolveCall.Returns.Error = errors.New("failed to resolve binding")
})
it("returns an error", func() {
_, err := packageManagerConfigurationManager.DeterminePath("some-typ", "platform-dir", "some-entry")
Expect(err).To(MatchError("failed to resolve binding"))
})
})
context("when more than one binding is found", func() {
it.Before(func() {
bindingResolver.ResolveCall.Returns.BindingSlice = []servicebindings.Binding{
{
Name: "first",
Type: "some-typ",
Path: "some-binding-path",
Entries: map[string]*servicebindings.Entry{
"some-entry": servicebindings.NewEntry("some-path"),
},
},
{
Name: "second",
Type: "some-typ",
Path: "some-binding-path",
Entries: map[string]*servicebindings.Entry{
"another-entry": servicebindings.NewEntry("some-path"),
},
},
}
})
it("returns an error", func() {
_, err := packageManagerConfigurationManager.DeterminePath("some-typ", "platform-dir", "some-entry")
Expect(err).To(MatchError("failed: binding resolver found more than one binding of type 'some-typ'"))
})
})
context("when the binding is missing the required entry", func() {
it.Before(func() {
bindingResolver.ResolveCall.Returns.BindingSlice = []servicebindings.Binding{
{
Name: "first",
Type: "some-typ",
Path: "some-binding-path",
Entries: map[string]*servicebindings.Entry{
"other-entry": servicebindings.NewEntry("some-path"),
},
},
}
})
it("returns an error", func() {
_, err := packageManagerConfigurationManager.DeterminePath("some-typ", "platform-dir", "some-entry")
Expect(err).To(MatchError("failed: binding of type 'some-typ' does not contain required entry 'some-entry'"))
})
})
})
})
}