github.com/paketoio/libpak@v1.3.1/internal/environment_writer_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 internal_test
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	. "github.com/onsi/gomega"
    27  	"github.com/paketoio/libpak/bard"
    28  	"github.com/paketoio/libpak/internal"
    29  	"github.com/sclevine/spec"
    30  )
    31  
    32  func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) {
    33  	var (
    34  		Expect = NewWithT(t).Expect
    35  
    36  		path   string
    37  		writer internal.EnvironmentWriter
    38  	)
    39  
    40  	it.Before(func() {
    41  		var err error
    42  		path, err = ioutil.TempDir("", "environment-writer")
    43  		Expect(err).NotTo(HaveOccurred())
    44  		Expect(os.RemoveAll(path)).To(Succeed())
    45  
    46  		writer = internal.EnvironmentWriter{}
    47  	})
    48  
    49  	it.After(func() {
    50  		Expect(os.RemoveAll(path)).To(Succeed())
    51  	})
    52  
    53  	it("writes the given environment to a directory", func() {
    54  		err := writer.Write(path, map[string]string{
    55  			"some-name":  "some-content",
    56  			"other-name": "other-content",
    57  		})
    58  		Expect(err).NotTo(HaveOccurred())
    59  
    60  		content, err := ioutil.ReadFile(filepath.Join(path, "some-name"))
    61  		Expect(err).NotTo(HaveOccurred())
    62  		Expect(string(content)).To(Equal("some-content"))
    63  
    64  		content, err = ioutil.ReadFile(filepath.Join(path, "other-name"))
    65  		Expect(err).NotTo(HaveOccurred())
    66  		Expect(string(content)).To(Equal("other-content"))
    67  	})
    68  
    69  	it("writes does not create a directory of the env map is empty", func() {
    70  		err := writer.Write(path, map[string]string{})
    71  		Expect(err).NotTo(HaveOccurred())
    72  
    73  		Expect(path).NotTo(BeAnExistingFile())
    74  	})
    75  
    76  	context("Logging", func() {
    77  		var (
    78  			b *bytes.Buffer
    79  		)
    80  
    81  		it.Before(func() {
    82  			b = bytes.NewBuffer(nil)
    83  			writer = internal.NewEnvironmentWriter(internal.WithEnvironmentWriterLogger(bard.NewLogger(b)))
    84  		})
    85  
    86  		it("logs environment", func() {
    87  			err := writer.Write(filepath.Join(path, "env"), map[string]string{
    88  				"some-name":  "some-content",
    89  				"other-name": "other-content",
    90  			})
    91  			Expect(err).NotTo(HaveOccurred())
    92  
    93  			Expect(b.String()).To(Equal("\x1b[2m    Writing env/other-name\n    Writing env/some-name\x1b[0m\n"))
    94  		})
    95  	})
    96  }