github.com/paketoio/libpak@v1.3.1/carton/package_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 "io/ioutil" 21 "os" 22 "path/filepath" 23 "testing" 24 25 "github.com/buildpacks/libcnb/mocks" 26 . "github.com/onsi/gomega" 27 "github.com/paketoio/libpak/carton" 28 cMocks "github.com/paketoio/libpak/carton/mocks" 29 "github.com/paketoio/libpak/effect" 30 eMocks "github.com/paketoio/libpak/effect/mocks" 31 "github.com/sclevine/spec" 32 "github.com/stretchr/testify/mock" 33 ) 34 35 func testPackage(t *testing.T, context spec.G, it spec.S) { 36 var ( 37 Expect = NewWithT(t).Expect 38 39 entryWriter *cMocks.EntryWriter 40 executor *eMocks.Executor 41 exitHandler *mocks.ExitHandler 42 p carton.Package 43 path string 44 ) 45 46 it.Before(func() { 47 var err error 48 49 entryWriter = &cMocks.EntryWriter{} 50 entryWriter.On("Write", mock.Anything, mock.Anything).Return(nil) 51 52 executor = &eMocks.Executor{} 53 executor.On("Execute", mock.Anything).Return(nil) 54 55 exitHandler = &mocks.ExitHandler{} 56 exitHandler.On("Error", mock.Anything) 57 58 path, err = ioutil.TempDir("", "carton-package") 59 Expect(err).NotTo(HaveOccurred()) 60 61 Expect(ioutil.WriteFile(filepath.Join(path, "buildpack.toml"), []byte(` 62 api = "0.0.0" 63 64 [buildpack] 65 name = "test-name" 66 version = "{{.Version}}" 67 68 [[metadata.dependencies]] 69 id = "test-id" 70 name = "test-name" 71 version = "1.1.1" 72 uri = "test-uri" 73 sha256 = "test-sha256" 74 stacks = [ "test-stack" ] 75 76 [[metadata.dependencies.licenses]] 77 type = "test-type" 78 uri = "test-uri" 79 80 [metadata] 81 pre-package = "test-pre-package" 82 include-files = [ 83 "test-include-files", 84 "buildpack.toml", 85 ] 86 `), 0644)).To(Succeed()) 87 }) 88 89 it.After(func() { 90 Expect(os.RemoveAll(path)).To(Succeed()) 91 }) 92 93 it("executes pre_package script", func() { 94 p.Source = path 95 96 p.Build( 97 carton.WithEntryWriter(entryWriter), 98 carton.WithExecutor(executor), 99 carton.WithExitHandler(exitHandler)) 100 101 e, ok := executor.Calls[0].Arguments[0].(effect.Execution) 102 Expect(ok).To(BeTrue()) 103 Expect(e.Command).To(Equal("test-pre-package")) 104 Expect(e.Dir).To(Equal(path)) 105 }) 106 107 it("includes include_files", func() { 108 p.Source = path 109 p.Destination = "test-destination" 110 111 p.Build( 112 carton.WithEntryWriter(entryWriter), 113 carton.WithExecutor(executor), 114 carton.WithExitHandler(exitHandler)) 115 116 Expect(entryWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(path, "buildpack.toml"))) 117 Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml"))) 118 Expect(entryWriter.Calls[1].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) 119 Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) 120 }) 121 122 it("replaces .Version in buildpack.toml", func() { 123 p.Source = path 124 p.Destination = "test-destination" 125 p.Version = "2.2.2" 126 127 p.Build( 128 carton.WithEntryWriter(entryWriter), 129 carton.WithExecutor(executor), 130 carton.WithExitHandler(exitHandler)) 131 132 Expect(entryWriter.Calls[0].Arguments[0]).NotTo(Equal(filepath.Join(path, "buildpack.toml"))) 133 Expect(entryWriter.Calls[0].Arguments[1]).To(Equal(filepath.Join("test-destination", "buildpack.toml"))) 134 Expect(entryWriter.Calls[1].Arguments[0]).To(Equal(filepath.Join(path, "test-include-files"))) 135 Expect(entryWriter.Calls[1].Arguments[1]).To(Equal(filepath.Join("test-destination", "test-include-files"))) 136 137 Expect(ioutil.ReadFile(entryWriter.Calls[0].Arguments[0].(string))).To(Equal([]byte(` 138 api = "0.0.0" 139 140 [buildpack] 141 name = "test-name" 142 version = "2.2.2" 143 144 [[metadata.dependencies]] 145 id = "test-id" 146 name = "test-name" 147 version = "1.1.1" 148 uri = "test-uri" 149 sha256 = "test-sha256" 150 stacks = [ "test-stack" ] 151 152 [[metadata.dependencies.licenses]] 153 type = "test-type" 154 uri = "test-uri" 155 156 [metadata] 157 pre-package = "test-pre-package" 158 include-files = [ 159 "test-include-files", 160 "buildpack.toml", 161 ] 162 `))) 163 }) 164 }