github.com/paketo-buildpacks/libpak@v1.70.0/buildpack_plan_test.go (about)

     1  /*
     2   * Copyright 2018-2020 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package libpak_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/buildpacks/libcnb"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/sclevine/spec"
    25  
    26  	"github.com/paketo-buildpacks/libpak"
    27  )
    28  
    29  func testBuildpackPlan(t *testing.T, context spec.G, it spec.S) {
    30  	var (
    31  		Expect = NewWithT(t).Expect
    32  	)
    33  
    34  	context("ShallowMerge", func() {
    35  
    36  		it("merges with empty", func() {
    37  			a := libcnb.BuildpackPlanEntry{}
    38  			b := libcnb.BuildpackPlanEntry{Name: "test-name"}
    39  
    40  			expected := libcnb.BuildpackPlanEntry{Name: "test-name"}
    41  
    42  			Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    43  		})
    44  
    45  		context("metadata", func() {
    46  			it("keeps a keys", func() {
    47  				a := libcnb.BuildpackPlanEntry{
    48  					Name:     "test-name",
    49  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
    50  				}
    51  				b := libcnb.BuildpackPlanEntry{Name: "test-name"}
    52  
    53  				expected := libcnb.BuildpackPlanEntry{
    54  					Name:     "test-name",
    55  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
    56  				}
    57  
    58  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    59  			})
    60  
    61  			it("keeps b keys", func() {
    62  				a := libcnb.BuildpackPlanEntry{Name: "test-name"}
    63  				b := libcnb.BuildpackPlanEntry{
    64  					Name:     "test-name",
    65  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
    66  				}
    67  
    68  				expected := libcnb.BuildpackPlanEntry{
    69  					Name:     "test-name",
    70  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
    71  				}
    72  
    73  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    74  			})
    75  
    76  			it("combines a and b keys", func() {
    77  				a := libcnb.BuildpackPlanEntry{
    78  					Name:     "test-name",
    79  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
    80  				}
    81  				b := libcnb.BuildpackPlanEntry{
    82  					Name:     "test-name",
    83  					Metadata: map[string]interface{}{"test-key-2": "test-value-2"},
    84  				}
    85  
    86  				expected := libcnb.BuildpackPlanEntry{
    87  					Name:     "test-name",
    88  					Metadata: map[string]interface{}{"test-key-1": "test-value-1", "test-key-2": "test-value-2"},
    89  				}
    90  
    91  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    92  			})
    93  
    94  			it("overwrites a keys with b keys", func() {
    95  				a := libcnb.BuildpackPlanEntry{
    96  					Name:     "test-name",
    97  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
    98  				}
    99  				b := libcnb.BuildpackPlanEntry{
   100  					Name:     "test-name",
   101  					Metadata: map[string]interface{}{"test-key-1": "test-value-2"},
   102  				}
   103  
   104  				expected := libcnb.BuildpackPlanEntry{
   105  					Name:     "test-name",
   106  					Metadata: map[string]interface{}{"test-key-1": "test-value-2"},
   107  				}
   108  
   109  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
   110  			})
   111  		})
   112  
   113  	})
   114  
   115  	context("PlanEntryResolver", func() {
   116  
   117  		context("ResolveWithMerge", func() {
   118  			var (
   119  				resolver = libpak.PlanEntryResolver{}
   120  			)
   121  
   122  			it.Before(func() {
   123  				resolver.Plan = libcnb.BuildpackPlan{
   124  					Entries: []libcnb.BuildpackPlanEntry{
   125  						{
   126  							Name: "test-name-1",
   127  						},
   128  						{
   129  							Name: "test-name-2",
   130  						},
   131  						{
   132  							Name: "test-name-2",
   133  						},
   134  					},
   135  				}
   136  			})
   137  
   138  			var f = func(a, b libcnb.BuildpackPlanEntry) (libcnb.BuildpackPlanEntry, error) {
   139  				return b, nil
   140  			}
   141  
   142  			it("returns false with no matches", func() {
   143  				_, ok, err := resolver.ResolveWithMerge("test-name-0", f)
   144  				Expect(err).NotTo(HaveOccurred())
   145  				Expect(ok).To(BeFalse())
   146  			})
   147  
   148  			it("returns merged with single match", func() {
   149  				e, ok, err := resolver.ResolveWithMerge("test-name-1", f)
   150  				Expect(err).NotTo(HaveOccurred())
   151  				Expect(ok).To(BeTrue())
   152  				Expect(e).To(Equal(libcnb.BuildpackPlanEntry{
   153  					Name: "test-name-1",
   154  				}))
   155  			})
   156  
   157  			it("returns merged with multiple matches", func() {
   158  				e, ok, err := resolver.ResolveWithMerge("test-name-2", f)
   159  				Expect(err).NotTo(HaveOccurred())
   160  				Expect(ok).To(BeTrue())
   161  				Expect(e).To(Equal(libcnb.BuildpackPlanEntry{
   162  					Name: "test-name-2",
   163  				}))
   164  			})
   165  		})
   166  
   167  		context("Resolve", func() {
   168  
   169  			it("merges with empty", func() {
   170  				a := libcnb.BuildpackPlanEntry{}
   171  				b := libcnb.BuildpackPlanEntry{Name: "test-name"}
   172  
   173  				resolver := libpak.PlanEntryResolver{
   174  					Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   175  				}
   176  				expected := libcnb.BuildpackPlanEntry{Name: "test-name"}
   177  
   178  				e, ok, err := resolver.Resolve("test-name")
   179  				Expect(err).NotTo(HaveOccurred())
   180  				Expect(ok).To(BeTrue())
   181  				Expect(e).To(Equal(expected))
   182  			})
   183  
   184  			context("metadata", func() {
   185  				it("keeps a keys", func() {
   186  					a := libcnb.BuildpackPlanEntry{
   187  						Name:     "test-name",
   188  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   189  					}
   190  					b := libcnb.BuildpackPlanEntry{Name: "test-name"}
   191  
   192  					resolver := libpak.PlanEntryResolver{
   193  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   194  					}
   195  					expected := libcnb.BuildpackPlanEntry{
   196  						Name:     "test-name",
   197  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   198  					}
   199  
   200  					e, ok, err := resolver.Resolve("test-name")
   201  					Expect(err).NotTo(HaveOccurred())
   202  					Expect(ok).To(BeTrue())
   203  					Expect(e).To(Equal(expected))
   204  				})
   205  
   206  				it("keeps b keys", func() {
   207  					a := libcnb.BuildpackPlanEntry{Name: "test-name"}
   208  					b := libcnb.BuildpackPlanEntry{
   209  						Name:     "test-name",
   210  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   211  					}
   212  
   213  					resolver := libpak.PlanEntryResolver{
   214  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   215  					}
   216  					expected := libcnb.BuildpackPlanEntry{
   217  						Name:     "test-name",
   218  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   219  					}
   220  
   221  					e, ok, err := resolver.Resolve("test-name")
   222  					Expect(err).NotTo(HaveOccurred())
   223  					Expect(ok).To(BeTrue())
   224  					Expect(e).To(Equal(expected))
   225  				})
   226  
   227  				it("combines a and b keys", func() {
   228  					a := libcnb.BuildpackPlanEntry{
   229  						Name:     "test-name",
   230  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   231  					}
   232  					b := libcnb.BuildpackPlanEntry{
   233  						Name:     "test-name",
   234  						Metadata: map[string]interface{}{"test-key-2": "test-value-2"},
   235  					}
   236  
   237  					resolver := libpak.PlanEntryResolver{
   238  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   239  					}
   240  					expected := libcnb.BuildpackPlanEntry{
   241  						Name:     "test-name",
   242  						Metadata: map[string]interface{}{"test-key-1": "test-value-1", "test-key-2": "test-value-2"},
   243  					}
   244  
   245  					e, ok, err := resolver.Resolve("test-name")
   246  					Expect(err).NotTo(HaveOccurred())
   247  					Expect(ok).To(BeTrue())
   248  					Expect(e).To(Equal(expected))
   249  				})
   250  
   251  				it("overwrites a keys with b keys", func() {
   252  					a := libcnb.BuildpackPlanEntry{
   253  						Name:     "test-name",
   254  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   255  					}
   256  					b := libcnb.BuildpackPlanEntry{
   257  						Name:     "test-name",
   258  						Metadata: map[string]interface{}{"test-key-1": "test-value-2"},
   259  					}
   260  
   261  					resolver := libpak.PlanEntryResolver{
   262  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   263  					}
   264  					expected := libcnb.BuildpackPlanEntry{
   265  						Name:     "test-name",
   266  						Metadata: map[string]interface{}{"test-key-1": "test-value-2"},
   267  					}
   268  
   269  					e, ok, err := resolver.Resolve("test-name")
   270  					Expect(err).NotTo(HaveOccurred())
   271  					Expect(ok).To(BeTrue())
   272  					Expect(e).To(Equal(expected))
   273  				})
   274  			})
   275  		})
   276  	})
   277  }