github.com/paketo-buildpacks/libpak/v2@v2.0.0-alpha.3.0.20231023030503-8365f81de65a/detect_test.go (about) 1 /* 2 * Copyright 2018-2023 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 "fmt" 21 "os" 22 "path/filepath" 23 "testing" 24 25 "github.com/buildpacks/libcnb/v2" 26 "github.com/buildpacks/libcnb/v2/mocks" 27 . "github.com/onsi/gomega" 28 "github.com/sclevine/spec" 29 "github.com/stretchr/testify/mock" 30 31 "github.com/paketo-buildpacks/libpak/v2" 32 "github.com/paketo-buildpacks/libpak/v2/log" 33 ) 34 35 func testDetect(t *testing.T, context spec.G, it spec.S) { 36 var ( 37 Expect = NewWithT(t).Expect 38 39 applicationPath string 40 buildpackPath string 41 buildPlanPath string 42 commandPath string 43 exitHandler *mocks.ExitHandler 44 layersPath string 45 platformPath string 46 tomlWriter *mocks.TOMLWriter 47 48 workingDir string 49 ) 50 51 it.Before(func() { 52 var err error 53 54 applicationPath = t.TempDir() 55 applicationPath, err = filepath.EvalSymlinks(applicationPath) 56 Expect(err).NotTo(HaveOccurred()) 57 58 buildpackPath = t.TempDir() 59 60 f, err := os.CreateTemp("", "detect-buildplan-path") 61 Expect(err).NotTo(HaveOccurred()) 62 Expect(f.Close()).NotTo(HaveOccurred()) 63 buildPlanPath = f.Name() 64 65 Expect(os.Setenv("CNB_BUILD_PLAN_PATH", buildPlanPath)).To(Succeed()) 66 67 commandPath = filepath.Join(buildpackPath, "bin", "detect") 68 69 exitHandler = &mocks.ExitHandler{} 70 exitHandler.On("Error", mock.Anything) 71 exitHandler.On("Fail") 72 exitHandler.On("Pass") 73 74 Expect(os.Setenv("CNB_STACK_ID", "test-stack-id")).To(Succeed()) 75 76 layersPath = t.TempDir() 77 Expect(err).NotTo(HaveOccurred()) 78 79 Expect(os.Setenv("CNB_LAYERS_DIR", layersPath)).To(Succeed()) 80 81 platformPath = t.TempDir() 82 Expect(err).NotTo(HaveOccurred()) 83 84 Expect(os.Setenv("CNB_PLATFORM_DIR", platformPath)).To(Succeed()) 85 86 tomlWriter = &mocks.TOMLWriter{} 87 tomlWriter.On("Write", mock.Anything, mock.Anything).Return(nil) 88 89 Expect(os.Setenv("CNB_STACK_ID", "test-stack-id")).To(Succeed()) 90 91 buildpackPath = t.TempDir() 92 Expect(err).NotTo(HaveOccurred()) 93 94 Expect(os.Setenv("CNB_BUILDPACK_DIR", buildpackPath)).To(Succeed()) 95 96 workingDir, err = os.Getwd() 97 Expect(err).NotTo(HaveOccurred()) 98 Expect(os.Chdir(applicationPath)).To(Succeed()) 99 }) 100 101 it.After(func() { 102 Expect(os.Chdir(workingDir)).To(Succeed()) 103 Expect(os.Unsetenv("CNB_BINDINGS")).To(Succeed()) 104 Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed()) 105 Expect(os.Unsetenv("CNB_BUILDPACK_DIR")).To(Succeed()) 106 Expect(os.Unsetenv("CNB_LAYERS_DIR")).To(Succeed()) 107 Expect(os.Unsetenv("CNB_PLATFORM_DIR")).To(Succeed()) 108 Expect(os.Unsetenv("CNB_BUILD_PLAN_PATH")).To(Succeed()) 109 110 Expect(os.RemoveAll(applicationPath)).To(Succeed()) 111 Expect(os.RemoveAll(buildpackPath)).To(Succeed()) 112 Expect(os.RemoveAll(buildPlanPath)).To(Succeed()) 113 Expect(os.RemoveAll(layersPath)).To(Succeed()) 114 Expect(os.RemoveAll(platformPath)).To(Succeed()) 115 Expect(os.RemoveAll(buildpackPath)).To(Succeed()) 116 }) 117 118 it("handles error from Detector", func() { 119 Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(` 120 api = "0.8" 121 122 [buildpack] 123 name = "test-name" 124 version = "test-version"`), 125 0644)).To(Succeed()) 126 127 libpak.Detect(func(ctx libcnb.DetectContext) (libcnb.DetectResult, error) { 128 return libcnb.DetectResult{}, fmt.Errorf("test-error") 129 }, 130 libcnb.WithArguments([]string{commandPath, platformPath, buildPlanPath}), 131 libcnb.WithExitHandler(exitHandler), 132 ) 133 134 Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(log.IdentifiableError{ 135 Name: "test-name", 136 Description: "test-version", 137 Err: fmt.Errorf("test-error"), 138 })) 139 }) 140 }