github.com/pivotal-cf/go-pivnet/v6@v6.0.2/md5sum/md5_test.go (about)

     1  package md5sum_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/md5sum"
    11  )
    12  
    13  var _ = Describe("MD5", func() {
    14  	Describe("FileSummer", func() {
    15  		var (
    16  			tempFilePath string
    17  			tempDir      string
    18  			fileContents []byte
    19  			fileSummer *md5sum.FileSummer
    20  		)
    21  
    22  		BeforeEach(func() {
    23  			var err error
    24  			tempDir, err = ioutil.TempDir("", "")
    25  			Expect(err).NotTo(HaveOccurred())
    26  
    27  			fileContents = []byte("foobar contents")
    28  
    29  			tempFilePath = filepath.Join(tempDir, "foobar")
    30  
    31  			ioutil.WriteFile(tempFilePath, fileContents, os.ModePerm)
    32  
    33  			fileSummer = md5sum.NewFileSummer()
    34  		})
    35  
    36  		AfterEach(func() {
    37  			err := os.RemoveAll(tempDir)
    38  			Expect(err).NotTo(HaveOccurred())
    39  		})
    40  
    41  		It("returns the MD5 of a file without error", func() {
    42  			md5, err := fileSummer.SumFile(tempFilePath)
    43  			Expect(err).NotTo(HaveOccurred())
    44  
    45  			// Expected md5 of 'foobar contents'
    46  			Expect(md5).To(Equal("fdd3d599138fd15d7673f3d3539531c1"))
    47  		})
    48  
    49  		Context("when there is an error reading the file", func() {
    50  			BeforeEach(func() {
    51  				tempFilePath = "/not/a/valid/file"
    52  			})
    53  
    54  			It("returns the error", func() {
    55  				_, err := fileSummer.SumFile(tempFilePath)
    56  				Expect(err).To(HaveOccurred())
    57  			})
    58  		})
    59  	})
    60  })