github.com/BarDweller/libpak@v0.0.0-20230630201634-8dd5cfc15ec9/generate_test.go (about) 1 /* 2 * Copyright 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" 26 "github.com/buildpacks/libcnb/mocks" 27 . "github.com/onsi/gomega" 28 "github.com/sclevine/spec" 29 "github.com/stretchr/testify/mock" 30 31 "github.com/BarDweller/libpak" 32 "github.com/BarDweller/libpak/bard" 33 ) 34 35 func testGenerate(t *testing.T, context spec.G, it spec.S) { 36 var ( 37 Expect = NewWithT(t).Expect 38 39 applicationPath string 40 extensionPath string 41 buildPlanPath string 42 commandPath string 43 exitHandler *mocks.ExitHandler 44 outputPath 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 f, err := os.CreateTemp("", "generate-buildplan-path") 59 Expect(err).NotTo(HaveOccurred()) 60 Expect(f.Close()).NotTo(HaveOccurred()) 61 buildPlanPath = f.Name() 62 63 Expect(os.Setenv("CNB_BP_PLAN_PATH", buildPlanPath)).To(Succeed()) 64 65 extensionPath = t.TempDir() 66 Expect(err).NotTo(HaveOccurred()) 67 68 Expect(os.Setenv("CNB_EXTENSION_DIR", extensionPath)).To(Succeed()) 69 70 outputPath = t.TempDir() 71 Expect(err).NotTo(HaveOccurred()) 72 73 Expect(os.Setenv("CNB_OUTPUT_DIR", outputPath)).To(Succeed()) 74 75 commandPath = filepath.Join(extensionPath, "bin", "generate") 76 77 exitHandler = &mocks.ExitHandler{} 78 exitHandler.On("Error", mock.Anything) 79 80 platformPath = t.TempDir() 81 Expect(err).NotTo(HaveOccurred()) 82 83 Expect(os.Setenv("CNB_PLATFORM_DIR", platformPath)).To(Succeed()) 84 85 tomlWriter = &mocks.TOMLWriter{} 86 tomlWriter.On("Write", mock.Anything, mock.Anything).Return(nil) 87 88 Expect(os.Setenv("CNB_STACK_ID", "test-stack-id")).To(Succeed()) 89 90 exitHandler = &mocks.ExitHandler{} 91 exitHandler.On("Error", mock.Anything) 92 exitHandler.On("Fail") 93 exitHandler.On("Pass") 94 95 workingDir, err = os.Getwd() 96 Expect(err).NotTo(HaveOccurred()) 97 Expect(os.Chdir(applicationPath)).To(Succeed()) 98 }) 99 100 it.After(func() { 101 Expect(os.Chdir(workingDir)).To(Succeed()) 102 Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed()) 103 Expect(os.Unsetenv("CNB_EXTENSION_DIR")).To(Succeed()) 104 Expect(os.Unsetenv("CNB_PLATFORM_DIR")).To(Succeed()) 105 Expect(os.Unsetenv("CNB_BP_PLAN_PATH")).To(Succeed()) 106 Expect(os.Unsetenv("CNB_OUTPUT_PATH")).To(Succeed()) 107 108 Expect(os.RemoveAll(applicationPath)).To(Succeed()) 109 Expect(os.RemoveAll(extensionPath)).To(Succeed()) 110 Expect(os.RemoveAll(buildPlanPath)).To(Succeed()) 111 Expect(os.RemoveAll(platformPath)).To(Succeed()) 112 Expect(os.RemoveAll(outputPath)).To(Succeed()) 113 }) 114 115 it("handles error from Generate", func() { 116 Expect(os.WriteFile(filepath.Join(extensionPath, "extension.toml"), []byte(` 117 api = "0.8" 118 119 [extension] 120 name = "test-name" 121 version = "test-version"`), 122 0644)).To(Succeed()) 123 124 libpak.Generate(func(ctx libcnb.GenerateContext) (libcnb.GenerateResult, error) { 125 return libcnb.GenerateResult{}, fmt.Errorf("test-error") 126 }, 127 libcnb.WithArguments([]string{commandPath, platformPath, buildPlanPath}), 128 libcnb.WithExitHandler(exitHandler), 129 ) 130 131 Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(bard.IdentifiableError{ 132 Name: "test-name", 133 Description: "test-version", 134 Err: fmt.Errorf("test-error"), 135 })) 136 }) 137 }