@@ -26,6 +26,11 @@ func testNode(t *testing.T, when spec.G, it spec.S) {
2626 f * test.BuildFactory
2727 stubNodeFixture = filepath .Join ("testdata" , "stub-node.tar.gz" )
2828 )
29+ var testPriorityMerge = func (planA , planB , expected buildpackplan.Plan ) {
30+ output , err := node .PriorityPlanMerge (planA , planB )
31+ Expect (err ).NotTo (HaveOccurred ())
32+ Expect (output ).To (Equal (expected ))
33+ }
2934
3035 it .Before (func () {
3136 RegisterTestingT (t )
@@ -186,4 +191,105 @@ export MEMORY_AVAILABLE
186191 })
187192 })
188193 })
194+
195+ when ("PriorityPlanMerge" , func () {
196+ var (
197+ planA , planB , expected buildpackplan.Plan
198+ )
199+
200+ when ("version key is empty for both" , func () {
201+ it ("leaves the version empty and merges metadata" , func () {
202+ planA := createNodeBuildPlan ("" , buildpackplan.Metadata {"key" : "1" })
203+ planB := createNodeBuildPlan ("" , buildpackplan.Metadata {"key" : "2" })
204+ expected := createNodeBuildPlan ("" , buildpackplan.Metadata {"key" : "1,2" })
205+
206+ testPriorityMerge (planA , planB , expected )
207+ })
208+ })
209+
210+ when ("version key is empty for one" , func () {
211+ it ("picks the other version and merges metadata" , func () {
212+ planA := createNodeBuildPlan ("" , buildpackplan.Metadata {"key" : "1" })
213+ planB := createNodeBuildPlan ("1" , buildpackplan.Metadata {"key" : "2" })
214+ expected := createNodeBuildPlan ("1" , buildpackplan.Metadata {"key" : "1,2" })
215+
216+ testPriorityMerge (planA , planB , expected )
217+ })
218+ })
219+
220+ when ("both have versions and VersionSource key is unset for both" , func () {
221+ it ("should pick latest version of the two" , func () {
222+ planA := createNodeBuildPlan ("1.0" , buildpackplan.Metadata {})
223+ planB := createNodeBuildPlan ("2.0" , buildpackplan.Metadata {})
224+ expected := createNodeBuildPlan ("2.0" , buildpackplan.Metadata {})
225+ testPriorityMerge (planA , planB , expected )
226+ testPriorityMerge (planB , planA , expected )
227+
228+ planA = createNodeBuildPlan ("1.0" , buildpackplan.Metadata {node .VersionSource : "" })
229+ planB = createNodeBuildPlan ("2.0" , buildpackplan.Metadata {node .VersionSource : "" })
230+ expected = createNodeBuildPlan ("2.0" , buildpackplan.Metadata {node .VersionSource : "" })
231+ testPriorityMerge (planA , planB , expected )
232+ testPriorityMerge (planB , planA , expected )
233+ })
234+
235+ })
236+
237+ when ("both have versions and VersionSource key is empty for one" , func () {
238+ it .Before (func () {
239+ planA = createNodeBuildPlan ("2.0" , buildpackplan.Metadata {node .VersionSource : "" })
240+ planB = createNodeBuildPlan ("1.0" , buildpackplan.Metadata {node .VersionSource : "package.json" })
241+ expected = createNodeBuildPlan ("1.0" , buildpackplan.Metadata {node .VersionSource : "package.json" })
242+ })
243+ it ("picks the version and source from the second input" , func () {
244+ testPriorityMerge (planA , planB , expected )
245+ })
246+
247+ it ("picks the version and source from the first input" , func () {
248+ testPriorityMerge (planB , planA , expected )
249+ })
250+ })
251+
252+ when ("different priority versions" , func () {
253+ it .Before (func () {
254+ planA = createNodeBuildPlan ("2.0" , buildpackplan.Metadata {node .VersionSource : "package.json" })
255+ planB = createNodeBuildPlan ("1.0" , buildpackplan.Metadata {node .VersionSource : "buildpack.yml" })
256+ expected = createNodeBuildPlan ("1.0" , buildpackplan.Metadata {node .VersionSource : "buildpack.yml" })
257+ })
258+
259+ it ("picks the version and source from the second input" , func () {
260+ testPriorityMerge (planA , planB , expected )
261+ })
262+
263+ it ("picks the version and source from the first input" , func () {
264+ testPriorityMerge (planB , planA , expected )
265+ })
266+ })
267+
268+ when ("they are the same priority" , func () {
269+ it ("picks the higher version" , func () {
270+ planA := createNodeBuildPlan ("2.0" , buildpackplan.Metadata {node .VersionSource : "buildpack.yml" })
271+ planB := createNodeBuildPlan ("1.0" , buildpackplan.Metadata {node .VersionSource : "buildpack.yml" })
272+ expected := createNodeBuildPlan ("2.0" , buildpackplan.Metadata {node .VersionSource : "buildpack.yml" })
273+ testPriorityMerge (planA , planB , expected )
274+ testPriorityMerge (planB , planA , expected )
275+ })
276+ })
277+
278+ when ("different build and launch metadata are set" , func () {
279+ it ("does an or-map of each key" , func () {
280+ planA := createNodeBuildPlan ("" , buildpackplan.Metadata {"build" : true })
281+ planB := createNodeBuildPlan ("" , buildpackplan.Metadata {"launch" : "true" , "build" : false })
282+ expected := createNodeBuildPlan ("" , buildpackplan.Metadata {"build" : true , "launch" : true })
283+ testPriorityMerge (planA , planB , expected )
284+ })
285+ })
286+ })
287+ }
288+
289+ func createNodeBuildPlan (version string , metadata buildpackplan.Metadata ) buildpackplan.Plan {
290+ return buildpackplan.Plan {
291+ Name : node .Dependency ,
292+ Version : version ,
293+ Metadata : metadata ,
294+ }
189295}
0 commit comments