github.com/paketoio/libpak@v1.3.1/internal/toml_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/buildpacks/libcnb"
    27  	. "github.com/onsi/gomega"
    28  	"github.com/paketoio/libpak/bard"
    29  	"github.com/paketoio/libpak/internal"
    30  	"github.com/sclevine/spec"
    31  )
    32  
    33  func testTOMLWriter(t *testing.T, context spec.G, it spec.S) {
    34  	var (
    35  		Expect = NewWithT(t).Expect
    36  
    37  		parent     string
    38  		path       string
    39  		tomlWriter internal.TOMLWriter
    40  	)
    41  
    42  	it.Before(func() {
    43  		var err error
    44  		parent, err = ioutil.TempDir("", "toml-writer")
    45  		Expect(err).NotTo(HaveOccurred())
    46  
    47  		path = filepath.Join(parent, "text.toml")
    48  	})
    49  
    50  	it.After(func() {
    51  		Expect(os.RemoveAll(parent)).To(Succeed())
    52  	})
    53  
    54  	it("writes the contents of a given object out to a .toml file", func() {
    55  		err := tomlWriter.Write(path, map[string]string{
    56  			"some-field":  "some-value",
    57  			"other-field": "other-value",
    58  		})
    59  		Expect(err).NotTo(HaveOccurred())
    60  
    61  		Expect(ioutil.ReadFile(path)).To(internal.MatchTOML(`
    62  some-field = "some-value"
    63  other-field = "other-value"`))
    64  	})
    65  
    66  	context("Logging", func() {
    67  		var (
    68  			b *bytes.Buffer
    69  		)
    70  
    71  		it.Before(func() {
    72  			b = bytes.NewBuffer(nil)
    73  			tomlWriter = internal.NewTOMLWriter(internal.WithTOMLWriterLogger(bard.NewLogger(b)))
    74  		})
    75  
    76  		it("does not log for uninteresting types", func() {
    77  			err := tomlWriter.Write(path, map[string]string{
    78  				"some-field":  "some-value",
    79  				"other-field": "other-value",
    80  			})
    81  			Expect(err).NotTo(HaveOccurred())
    82  			Expect(b.String()).To(Equal(""))
    83  		})
    84  
    85  		it("logs libcnb.Launch", func() {
    86  			err := tomlWriter.Write(path, libcnb.Launch{
    87  				Slices: []libcnb.Slice{{}, {}},
    88  			})
    89  			Expect(err).NotTo(HaveOccurred())
    90  			Expect(b.String()).To(Equal("  2 application slices\n"))
    91  		})
    92  
    93  		it("logs libcnb.Store", func() {
    94  			err := tomlWriter.Write(path, libcnb.Store{
    95  				Metadata: map[string]interface{}{
    96  					"test-key-1": "test-value-1",
    97  					"test-key-2": "test-value-2",
    98  				},
    99  			})
   100  			Expect(err).NotTo(HaveOccurred())
   101  			Expect(b.String()).To(Equal("  Writing persistent metadata: test-key-1, test-key-2\n"))
   102  		})
   103  	})
   104  }