github.com/BarDweller/libpak@v0.0.0-20230630201634-8dd5cfc15ec9/build_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"
    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 testBuild(t *testing.T, context spec.G, it spec.S) {
    36  	var (
    37  		Expect = NewWithT(t).Expect
    38  
    39  		applicationPath   string
    40  		buildpackPath     string
    41  		buildpackPlanPath string
    42  		commandPath       string
    43  		environmentWriter *mocks.EnvironmentWriter
    44  		exitHandler       *mocks.ExitHandler
    45  		layersPath        string
    46  		platformPath      string
    47  		tomlWriter        *mocks.TOMLWriter
    48  
    49  		workingDir string
    50  	)
    51  
    52  	it.Before(func() {
    53  		var err error
    54  
    55  		applicationPath = t.TempDir()
    56  		Expect(err).NotTo(HaveOccurred())
    57  		applicationPath, err = filepath.EvalSymlinks(applicationPath)
    58  		Expect(err).NotTo(HaveOccurred())
    59  
    60  		buildpackPath = t.TempDir()
    61  		Expect(err).NotTo(HaveOccurred())
    62  
    63  		f, err := os.CreateTemp("", "build-buildpackplan-path")
    64  		Expect(err).NotTo(HaveOccurred())
    65  		Expect(f.Close()).NotTo(HaveOccurred())
    66  		buildpackPlanPath = f.Name()
    67  
    68  		Expect(os.Setenv("CNB_BP_PLAN_PATH", buildpackPlanPath)).To(Succeed())
    69  
    70  		commandPath = filepath.Join(buildpackPath, "bin", "build")
    71  
    72  		environmentWriter = &mocks.EnvironmentWriter{}
    73  		environmentWriter.On("Write", mock.Anything, mock.Anything).Return(nil)
    74  
    75  		exitHandler = &mocks.ExitHandler{}
    76  		exitHandler.On("Error", mock.Anything)
    77  
    78  		layersPath = t.TempDir()
    79  		Expect(err).NotTo(HaveOccurred())
    80  
    81  		Expect(os.Setenv("CNB_LAYERS_DIR", layersPath)).To(Succeed())
    82  
    83  		platformPath = t.TempDir()
    84  		Expect(err).NotTo(HaveOccurred())
    85  
    86  		Expect(os.Setenv("CNB_PLATFORM_DIR", platformPath)).To(Succeed())
    87  
    88  		tomlWriter = &mocks.TOMLWriter{}
    89  		tomlWriter.On("Write", mock.Anything, mock.Anything).Return(nil)
    90  
    91  		Expect(os.Setenv("CNB_STACK_ID", "test-stack-id")).To(Succeed())
    92  
    93  		buildpackPath = t.TempDir()
    94  		Expect(err).NotTo(HaveOccurred())
    95  
    96  		Expect(os.Setenv("CNB_BUILDPACK_DIR", buildpackPath)).To(Succeed())
    97  
    98  		workingDir, err = os.Getwd()
    99  		Expect(err).NotTo(HaveOccurred())
   100  		Expect(os.Chdir(applicationPath)).To(Succeed())
   101  	})
   102  
   103  	it.After(func() {
   104  		Expect(os.Chdir(workingDir)).To(Succeed())
   105  		Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed())
   106  		Expect(os.Unsetenv("CNB_BUILDPACK_DIR")).To(Succeed())
   107  		Expect(os.Unsetenv("CNB_LAYERS_DIR")).To(Succeed())
   108  		Expect(os.Unsetenv("CNB_PLATFORM_DIR")).To(Succeed())
   109  		Expect(os.Unsetenv("CNB_BP_PLAN_PATH")).To(Succeed())
   110  
   111  		Expect(os.RemoveAll(applicationPath)).To(Succeed())
   112  		Expect(os.RemoveAll(buildpackPath)).To(Succeed())
   113  		Expect(os.RemoveAll(buildpackPlanPath)).To(Succeed())
   114  		Expect(os.RemoveAll(layersPath)).To(Succeed())
   115  		Expect(os.RemoveAll(platformPath)).To(Succeed())
   116  		Expect(os.RemoveAll(buildpackPath)).To(Succeed())
   117  	})
   118  
   119  	it("handles error from Builder", func() {
   120  		Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(`
   121  api = "0.8"
   122  
   123  [buildpack]
   124  name    = "test-name"
   125  version = "test-version"`),
   126  			0644)).To(Succeed())
   127  
   128  		libpak.Build(func(ctx libcnb.BuildContext) (libcnb.BuildResult, error) {
   129  			return libcnb.BuildResult{}, fmt.Errorf("test-error")
   130  		},
   131  			libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
   132  			libcnb.WithExitHandler(exitHandler),
   133  		)
   134  
   135  		Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(bard.IdentifiableError{
   136  			Name:        "test-name",
   137  			Description: "test-version",
   138  			Err:         fmt.Errorf("test-error"),
   139  		}))
   140  	})
   141  }