github.com/buildpacks/pack@v0.33.3-0.20240516162812-884dd1837311/pkg/dist/dist_test.go (about)

     1  package dist_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/buildpacks/lifecycle/api"
     7  	"github.com/heroku/color"
     8  	"github.com/sclevine/spec"
     9  	"github.com/sclevine/spec/report"
    10  
    11  	"github.com/buildpacks/pack/pkg/dist"
    12  	h "github.com/buildpacks/pack/testhelpers"
    13  )
    14  
    15  func TestDist(t *testing.T) {
    16  	color.Disable(true)
    17  	defer color.Disable(false)
    18  	spec.Run(t, "testDist", testDist, spec.Parallel(), spec.Report(report.Terminal{}))
    19  }
    20  
    21  func testDist(t *testing.T, when spec.G, it spec.S) {
    22  	when("ModuleLayers", func() {
    23  		when("Get", func() {
    24  			var (
    25  				buildpackLayers dist.ModuleLayers
    26  				apiVersion      *api.Version
    27  			)
    28  			it.Before(func() {
    29  				var err error
    30  				apiVersion, err = api.NewVersion("0.0")
    31  				h.AssertNil(t, err)
    32  
    33  				buildpackLayers = dist.ModuleLayers{
    34  					"buildpack": {
    35  						"version1": {
    36  							API:         apiVersion,
    37  							LayerDiffID: "buildpack-v1-diff",
    38  						},
    39  					},
    40  					"other-buildpack": {
    41  						"version1": {
    42  							API:         apiVersion,
    43  							LayerDiffID: "other-buildpack-v2-diff",
    44  						},
    45  						"version2": {
    46  							API:         apiVersion,
    47  							LayerDiffID: "other-buildpack-v2-diff",
    48  						},
    49  					},
    50  				}
    51  			})
    52  
    53  			when("ID and Version are provided and present", func() {
    54  				it("succeeds", func() {
    55  					out, ok := buildpackLayers.Get("buildpack", "version1")
    56  					h.AssertEq(t, ok, true)
    57  					h.AssertEq(t, out, dist.ModuleLayerInfo{
    58  						API:         apiVersion,
    59  						LayerDiffID: "buildpack-v1-diff",
    60  					})
    61  				})
    62  			})
    63  
    64  			when("ID is present, Version is left empty, but can be inferred", func() {
    65  				it("succeeds", func() {
    66  					out, ok := buildpackLayers.Get("buildpack", "")
    67  					h.AssertEq(t, ok, true)
    68  					h.AssertEq(t, out, dist.ModuleLayerInfo{
    69  						API:         apiVersion,
    70  						LayerDiffID: "buildpack-v1-diff",
    71  					})
    72  				})
    73  			})
    74  
    75  			when("ID is present, Version is left empty and cannot be inferred", func() {
    76  				it("fails", func() {
    77  					_, ok := buildpackLayers.Get("other-buildpack", "")
    78  					h.AssertEq(t, ok, false)
    79  				})
    80  			})
    81  
    82  			when("ID is NOT provided", func() {
    83  				it("fails", func() {
    84  					_, ok := buildpackLayers.Get("missing-buildpack", "")
    85  					h.AssertEq(t, ok, false)
    86  				})
    87  			})
    88  		})
    89  
    90  		when("Add", func() {
    91  			when("a new buildpack is added", func() {
    92  				it("succeeds", func() {
    93  					layers := dist.ModuleLayers{}
    94  					apiVersion, _ := api.NewVersion("0.0")
    95  					descriptor := dist.BuildpackDescriptor{WithAPI: apiVersion, WithInfo: dist.ModuleInfo{ID: "test", Name: "test", Version: "1.0"}}
    96  					dist.AddToLayersMD(layers, &descriptor, "")
    97  					layerInfo, ok := layers.Get(descriptor.Info().ID, descriptor.Info().Version)
    98  					h.AssertEq(t, ok, true)
    99  					h.AssertEq(t, layerInfo.Name, descriptor.Info().Name)
   100  				})
   101  			})
   102  		})
   103  	})
   104  
   105  	when("ImageOrURI", func() {
   106  		when("DisplayString", func() {
   107  			when("uri", func() {
   108  				when("blank", func() {
   109  					it("returns image", func() {
   110  						toTest := dist.ImageOrURI{
   111  							ImageRef: dist.ImageRef{
   112  								ImageName: "some-image-name",
   113  							},
   114  						}
   115  						h.AssertEq(t, toTest.DisplayString(), "some-image-name")
   116  					})
   117  				})
   118  
   119  				when("not blank", func() {
   120  					it("returns uri", func() {
   121  						toTest := dist.ImageOrURI{
   122  							BuildpackURI: dist.BuildpackURI{
   123  								URI: "some-uri",
   124  							},
   125  							ImageRef: dist.ImageRef{
   126  								ImageName: "some-image-name",
   127  							},
   128  						}
   129  						h.AssertEq(t, toTest.DisplayString(), "some-uri")
   130  					})
   131  				})
   132  			})
   133  		})
   134  	})
   135  }