github.com/pivotal-cf/go-pivnet/v6@v6.0.2/sha256sum/sha256_test.go (about)

     1  package sha256sum_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/pivotal-cf/go-pivnet/v6/sha256sum"
    11  )
    12  
    13  var _ = Describe("SHA256", func() {
    14  	Describe("FileSummer", func() {
    15  		var (
    16  			tempFilePath string
    17  			tempDir      string
    18  			fileContents []byte
    19  
    20  			fileSummer *sha256sum.FileSummer
    21  		)
    22  
    23  		BeforeEach(func() {
    24  			var err error
    25  			tempDir, err = ioutil.TempDir("", "")
    26  			Expect(err).NotTo(HaveOccurred())
    27  
    28  			fileContents = []byte("foobar contents")
    29  
    30  			tempFilePath = filepath.Join(tempDir, "foobar")
    31  
    32  			ioutil.WriteFile(tempFilePath, fileContents, os.ModePerm)
    33  
    34  			fileSummer = sha256sum.NewFileSummer()
    35  		})
    36  
    37  		AfterEach(func() {
    38  			err := os.RemoveAll(tempDir)
    39  			Expect(err).NotTo(HaveOccurred())
    40  		})
    41  
    42  		It("returns the SHA256 of a file without error", func() {
    43  			sha256, err := fileSummer.SumFile(tempFilePath)
    44  			Expect(err).NotTo(HaveOccurred())
    45  
    46  			// Expected sha256 of 'foobar contents'
    47  			Expect(sha256).To(Equal("070a103eb906d53a5933d96f3301635d6c416491d6a0ebd0bf4d4e448af5762d"))
    48  		})
    49  
    50  		Context("when there is an error reading the file", func() {
    51  			BeforeEach(func() {
    52  				tempFilePath = "/not/a/valid/file"
    53  			})
    54  
    55  			It("returns the error", func() {
    56  				_, err := fileSummer.SumFile(tempFilePath)
    57  				Expect(err).To(HaveOccurred())
    58  			})
    59  		})
    60  	})
    61  })