github.com/cloudfoundry/libcfbuildpack@v1.91.23/packager/main_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 main
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	. "github.com/onsi/gomega"
    27  	"github.com/sclevine/spec"
    28  	"github.com/sclevine/spec/report"
    29  )
    30  
    31  func TestIntegrationPackager(t *testing.T) {
    32  	spec.Run(t, "Package Integration", testPackagerIntegration, spec.Report(report.Terminal{}))
    33  }
    34  
    35  func testPackagerIntegration(t *testing.T, when spec.G, it spec.S) {
    36  	var (
    37  		Expect                                                              func(interface{}, ...interface{}) Assertion
    38  		cnbDir, outputDir, tempDir, buildpackTomlPath, oldBPToml, newBPToml string
    39  		err                                                                 error
    40  		newVersion                                                          string
    41  	)
    42  
    43  	it.Before(func() {
    44  		Expect = NewWithT(t).Expect
    45  
    46  		tempDir, err = ioutil.TempDir("", "")
    47  		Expect(err).NotTo(HaveOccurred())
    48  
    49  		cnbDir = filepath.Join(tempDir, "cnb")
    50  		Expect(os.MkdirAll(cnbDir, 0777))
    51  		newVersion = "newVersion"
    52  		buildpackTomlPath = filepath.Join(cnbDir, "buildpack.toml")
    53  		baseBuildpackTOML := `[buildpack]
    54  id = "buildpack-id"
    55  name = "buildpack-name"
    56  version = "%s"
    57  
    58  [metadata]
    59  include_files = ["buildpack.toml"]`
    60  		oldBPToml = fmt.Sprintf(baseBuildpackTOML, "{{ .Version }}")
    61  		newBPToml = fmt.Sprintf(baseBuildpackTOML, newVersion)
    62  
    63  		outputDir = filepath.Join(tempDir, "output")
    64  	})
    65  
    66  	it.After(func() {
    67  		os.RemoveAll(tempDir)
    68  	})
    69  
    70  	when("packaging", func() {
    71  		it("does it in a temp directory", func() {
    72  			Expect(ioutil.WriteFile(buildpackTomlPath, []byte(oldBPToml), 0666)).To(Succeed())
    73  
    74  			// Set first arg equal to command name, per: https://stackoverflow.com/questions/33723300/how-to-test-the-passing-of-arguments-in-golang
    75  			os.Args = []string{"cmd", "-version=" + newVersion, outputDir}
    76  			Expect(os.Chdir(cnbDir)).To(Succeed())
    77  			main()
    78  			originalOutput, err := ioutil.ReadFile(buildpackTomlPath)
    79  			Expect(err).NotTo(HaveOccurred())
    80  			Expect(string(originalOutput)).To(Equal(oldBPToml))
    81  
    82  			newOutput, err := ioutil.ReadFile(filepath.Join(outputDir, "buildpack.toml"))
    83  			Expect(err).NotTo(HaveOccurred())
    84  			Expect(string(newOutput)).To(Equal(newBPToml))
    85  		})
    86  	})
    87  }