github.com/BarDweller/libpak@v0.0.0-20230630201634-8dd5cfc15ec9/carton/package_dependency_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 carton_test 18 19 import ( 20 "os" 21 "testing" 22 23 "github.com/buildpacks/libcnb/mocks" 24 . "github.com/onsi/gomega" 25 "github.com/sclevine/spec" 26 "github.com/stretchr/testify/mock" 27 28 "github.com/BarDweller/libpak/carton" 29 "github.com/BarDweller/libpak/internal" 30 ) 31 32 func testPackageDependency(t *testing.T, context spec.G, it spec.S) { 33 var ( 34 Expect = NewWithT(t).Expect 35 36 exitHandler *mocks.ExitHandler 37 path string 38 ) 39 40 it.Before(func() { 41 var err error 42 43 exitHandler = &mocks.ExitHandler{} 44 exitHandler.On("Error", mock.Anything) 45 46 f, err := os.CreateTemp("", "carton-package-dependency") 47 Expect(err).NotTo(HaveOccurred()) 48 Expect(f.Close()).To(Succeed()) 49 path = f.Name() 50 }) 51 52 it.After(func() { 53 Expect(os.RemoveAll(path)).To(Succeed()) 54 }) 55 56 it("updates paketo-buildpacks dependency without losing other fields", func() { 57 Expect(os.WriteFile(path, []byte(`# it should preserve 58 # these comments 59 # exactly 60 61 api = "0.6" 62 [buildpack] 63 id = "some-id" 64 name = "some-name" 65 66 [[order]] 67 group = [ 68 { id = "paketo-buildpacks/test-1", version="test-version-1" }, 69 { id = "paketo-buildpacks/test-2", version="test-version-2" }, 70 ] 71 [metadata] 72 include-files = [ 73 "LICENSE", 74 "README.md", 75 "buildpack.toml", 76 ]`), 0644)).To(Succeed()) 77 78 p := carton.PackageDependency{ 79 BuildpackPath: path, 80 ID: "gcr.io/paketo-buildpacks/test-1", 81 Version: "test-version-3", 82 } 83 84 p.Update(carton.WithExitHandler(exitHandler)) 85 86 body, err := os.ReadFile(path) 87 Expect(err).NotTo(HaveOccurred()) 88 Expect(string(body)).To(HavePrefix(`# it should preserve 89 # these comments 90 # exactly 91 92 api = "0.6"`)) 93 94 Expect(body).To(internal.MatchTOML(`api = "0.6" 95 [buildpack] 96 id = "some-id" 97 name = "some-name" 98 99 [[order]] 100 [[order.group]] 101 id = "paketo-buildpacks/test-1" 102 version="test-version-3" 103 [[order.group]] 104 id = "paketo-buildpacks/test-2" 105 version="test-version-2" 106 [metadata] 107 include-files = ["LICENSE","README.md","buildpack.toml"] 108 `)) 109 }) 110 111 it("updates paketo-buildpacks dependency id partial id", func() { 112 Expect(os.WriteFile(path, []byte(` 113 [[order]] 114 group = [ 115 { id = "paketo-buildpacks/test-1", version="test-version-1" }, 116 { id = "paketo-buildpacks/test-2", version="test-version-2" }, 117 ]`), 0644)).To(Succeed()) 118 119 p := carton.PackageDependency{ 120 BuildpackPath: path, 121 ID: "paketo-buildpacks/test-1", 122 Version: "test-version-3", 123 } 124 125 p.Update(carton.WithExitHandler(exitHandler)) 126 127 Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[order]] 128 [[order.group]] 129 id = "paketo-buildpacks/test-1" 130 version="test-version-3" 131 [[order.group]] 132 id = "paketo-buildpacks/test-2" 133 version="test-version-2"`)) 134 }) 135 136 it("updates paketocommunity dependency", func() { 137 Expect(os.WriteFile(path, []byte(`[[order]] 138 group = [ 139 { id = "paketocommunity/test-1", version="test-version-1" }, 140 { id = "paketocommunity/test-2", version="test-version-2" }, 141 ]`), 0644)).To(Succeed()) 142 143 p := carton.PackageDependency{ 144 BuildpackPath: path, 145 ID: "docker.io/paketocommunity/test-1", 146 Version: "test-version-3", 147 } 148 149 p.Update(carton.WithExitHandler(exitHandler)) 150 151 Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[order]] 152 [[order.group]] 153 id = "paketocommunity/test-1" 154 version="test-version-3" 155 [[order.group]] 156 id = "paketocommunity/test-2" 157 version="test-version-2"`)) 158 }) 159 160 it("updates builder dependency", func() { 161 Expect(os.WriteFile(path, []byte(`buildpacks = [ 162 { id = "paketo-buildpacks/test-1", uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-1" }, 163 { id = "paketo-buildpacks/test-2", uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2" }, 164 ]`), 0644)).To(Succeed()) 165 166 p := carton.PackageDependency{ 167 BuilderPath: path, 168 ID: "gcr.io/paketo-buildpacks/test-1", 169 Version: "test-version-3", 170 } 171 172 p.Update(carton.WithExitHandler(exitHandler)) 173 174 Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[buildpacks]] 175 id = "paketo-buildpacks/test-1" 176 uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-3" 177 178 [[buildpacks]] 179 id = "paketo-buildpacks/test-2" 180 uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2"`)) 181 }) 182 183 it("updates paketo-buildpacks package dependency", func() { 184 Expect(os.WriteFile(path, []byte(`dependencies = [ 185 { uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-1" }, 186 { uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2" }, 187 ]`), 0644)).To(Succeed()) 188 189 p := carton.PackageDependency{ 190 PackagePath: path, 191 ID: "gcr.io/paketo-buildpacks/test-1", 192 Version: "test-version-3", 193 } 194 195 p.Update(carton.WithExitHandler(exitHandler)) 196 197 Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[dependencies]] 198 uri = "docker://gcr.io/paketo-buildpacks/test-1:test-version-3" 199 200 [[dependencies]] 201 uri = "docker://gcr.io/paketo-buildpacks/test-2:test-version-2"`)) 202 }) 203 204 it("updates paketocommunity package dependency", func() { 205 Expect(os.WriteFile(path, []byte(`dependencies = [ 206 { uri = "docker://docker.io/paketocommunity/test-1:test-version-1" }, 207 { uri = "docker://docker.io/paketocommunity/test-2:test-version-2" }, 208 ]`), 0644)).To(Succeed()) 209 210 p := carton.PackageDependency{ 211 PackagePath: path, 212 ID: "docker.io/paketocommunity/test-1", 213 Version: "test-version-3", 214 } 215 216 p.Update(carton.WithExitHandler(exitHandler)) 217 218 Expect(os.ReadFile(path)).To(internal.MatchTOML(`[[dependencies]] 219 uri = "docker://docker.io/paketocommunity/test-1:test-version-3" 220 221 [[dependencies]] 222 uri = "docker://docker.io/paketocommunity/test-2:test-version-2"`)) 223 }) 224 225 }