github.com/cloudfoundry/libcfbuildpack@v1.91.23/helper/read_buildpack_yaml_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 helper_test
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/cloudfoundry/libcfbuildpack/test"
    26  
    27  	"github.com/cloudfoundry/libcfbuildpack/helper"
    28  	"github.com/onsi/gomega"
    29  	"github.com/sclevine/spec"
    30  	"github.com/sclevine/spec/report"
    31  )
    32  
    33  func TestReadBuildpackYaml(t *testing.T) {
    34  	type BuildpackYaml struct {
    35  		Python struct {
    36  			Version string `yaml:"version"`
    37  		} `yaml:"python"`
    38  
    39  		Go struct {
    40  			Version string `yaml:"version"`
    41  		} `yaml:"go"`
    42  	}
    43  
    44  	spec.Run(t, "ReadBuildpackYaml", func(t *testing.T, when spec.G, it spec.S) {
    45  		var (
    46  			pythonVersion     = "1.2.3"
    47  			goVersion         = "4.5.6"
    48  			buildpackYamlPath string
    49  			config            *BuildpackYaml
    50  		)
    51  
    52  		Expect := gomega.NewWithT(t).Expect
    53  
    54  		when("read buildpack yaml version", func() {
    55  			it.Before(func() {
    56  				tmpDir := os.TempDir()
    57  				buildpackYamlPath = filepath.Join(tmpDir, "buildpack.yml")
    58  				buildpackYAMLString := fmt.Sprintf("python:\n  version: %s\ngo:\n version: %s", pythonVersion, goVersion)
    59  				Expect(helper.WriteFile(buildpackYamlPath, 0777, buildpackYAMLString)).To(gomega.Succeed())
    60  				config = &BuildpackYaml{}
    61  			})
    62  
    63  			it.After(func() {
    64  				Expect(os.RemoveAll(buildpackYamlPath)).To(gomega.Succeed())
    65  			})
    66  
    67  			it("unmarshals a user defined config when given a buildpackyml path", func() {
    68  				err := helper.ReadBuildpackYaml(buildpackYamlPath, config)
    69  				Expect(err).NotTo(gomega.HaveOccurred())
    70  				Expect(config.Python.Version).To(gomega.Equal(pythonVersion))
    71  				Expect(config.Go.Version).To(gomega.Equal(goVersion))
    72  			})
    73  
    74  		})
    75  
    76  		when("buildpack yaml file exists but is empty", func() {
    77  			it.Before(func() {
    78  				tmpDir := os.TempDir()
    79  				buildpackYamlPath = filepath.Join(tmpDir, "buildpack.yml")
    80  				test.TouchFile(t, tmpDir, "buildpack.yml")
    81  				config = &BuildpackYaml{}
    82  			})
    83  
    84  			it.After(func() {
    85  				Expect(os.RemoveAll(buildpackYamlPath)).To(gomega.Succeed())
    86  			})
    87  
    88  			it("does not error", func() {
    89  				err := helper.ReadBuildpackYaml(buildpackYamlPath, config)
    90  				Expect(err).NotTo(gomega.HaveOccurred())
    91  				Expect(config).To(gomega.Equal(&BuildpackYaml{}))
    92  			})
    93  
    94  		})
    95  
    96  	}, spec.Report(report.Terminal{}))
    97  }