github.com/rrashidov/libpak@v0.0.0-20230911084305-75119185bb4d/build_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 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/paketo-buildpacks/libpak"
    32  	"github.com/paketo-buildpacks/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  		builder           *mocks.Builder
    41  		buildpackPath     string
    42  		buildpackPlanPath string
    43  		commandPath       string
    44  		environmentWriter *mocks.EnvironmentWriter
    45  		exitHandler       *mocks.ExitHandler
    46  		layersPath        string
    47  		platformPath      string
    48  		tomlWriter        *mocks.TOMLWriter
    49  
    50  		workingDir string
    51  	)
    52  
    53  	it.Before(func() {
    54  		var err error
    55  
    56  		applicationPath = t.TempDir()
    57  		Expect(err).NotTo(HaveOccurred())
    58  		applicationPath, err = filepath.EvalSymlinks(applicationPath)
    59  		Expect(err).NotTo(HaveOccurred())
    60  
    61  		builder = &mocks.Builder{}
    62  
    63  		buildpackPath = t.TempDir()
    64  		Expect(err).NotTo(HaveOccurred())
    65  
    66  		f, err := os.CreateTemp("", "build-buildpackplan-path")
    67  		Expect(err).NotTo(HaveOccurred())
    68  		Expect(f.Close()).NotTo(HaveOccurred())
    69  		buildpackPlanPath = f.Name()
    70  
    71  		commandPath = filepath.Join(buildpackPath, "bin", "build")
    72  
    73  		environmentWriter = &mocks.EnvironmentWriter{}
    74  		environmentWriter.On("Write", mock.Anything, mock.Anything).Return(nil)
    75  
    76  		exitHandler = &mocks.ExitHandler{}
    77  		exitHandler.On("Error", mock.Anything)
    78  
    79  		layersPath = t.TempDir()
    80  		Expect(err).NotTo(HaveOccurred())
    81  
    82  		platformPath = t.TempDir()
    83  		Expect(err).NotTo(HaveOccurred())
    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  		workingDir, err = os.Getwd()
    91  		Expect(err).NotTo(HaveOccurred())
    92  		Expect(os.Chdir(applicationPath)).To(Succeed())
    93  	})
    94  
    95  	it.After(func() {
    96  		Expect(os.Chdir(workingDir)).To(Succeed())
    97  		Expect(os.Unsetenv("CNB_STACK_ID")).To(Succeed())
    98  
    99  		Expect(os.RemoveAll(applicationPath)).To(Succeed())
   100  		Expect(os.RemoveAll(buildpackPath)).To(Succeed())
   101  		Expect(os.RemoveAll(buildpackPlanPath)).To(Succeed())
   102  		Expect(os.RemoveAll(layersPath)).To(Succeed())
   103  		Expect(os.RemoveAll(platformPath)).To(Succeed())
   104  	})
   105  
   106  	it("handles error from Builder", func() {
   107  		Expect(os.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), []byte(`
   108  api = "0.6"
   109  
   110  [buildpack]
   111  name    = "test-name"
   112  version = "test-version"`),
   113  			0644)).To(Succeed())
   114  		builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), fmt.Errorf("test-error"))
   115  
   116  		libpak.Build(builder,
   117  			libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
   118  			libcnb.WithExitHandler(exitHandler),
   119  		)
   120  
   121  		Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(bard.IdentifiableError{
   122  			Name:        "test-name",
   123  			Description: "test-version",
   124  			Err:         fmt.Errorf("test-error"),
   125  		}))
   126  	})
   127  }