github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/command/plugin/shared/errors_test.go (about)

     1  package shared_test
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"text/template"
     7  
     8  	. "code.cloudfoundry.org/cli/command/plugin/shared"
     9  	"code.cloudfoundry.org/cli/util/ui"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/ginkgo/extensions/table"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Translatable Errors", func() {
    16  	translateFunc := func(s string, vars ...interface{}) string {
    17  		formattedTemplate, err := template.New("test template").Parse(s)
    18  		Expect(err).ToNot(HaveOccurred())
    19  		formattedTemplate.Option("missingkey=error")
    20  
    21  		var buffer bytes.Buffer
    22  		if len(vars) > 0 {
    23  			err = formattedTemplate.Execute(&buffer, vars[0])
    24  			Expect(err).ToNot(HaveOccurred())
    25  
    26  			return buffer.String()
    27  		} else {
    28  			return s
    29  		}
    30  	}
    31  
    32  	DescribeTable("translates error",
    33  		func(e error) {
    34  			err, ok := e.(ui.TranslatableError)
    35  			Expect(ok).To(BeTrue())
    36  			err.Translate(translateFunc)
    37  		},
    38  
    39  		Entry("JSONSyntaxError", JSONSyntaxError{Err: errors.New("some-error")}),
    40  		Entry("PluginBinaryRemoveFailedError", PluginBinaryRemoveFailedError{}),
    41  		Entry("PluginBinaryUninstallError", PluginBinaryUninstallError{}),
    42  		Entry("PluginNotFoundError", PluginNotFoundError{}),
    43  		Entry("PluginNotFoundInRepositoryError", PluginNotFoundInRepositoryError{}),
    44  		Entry("PluginNotFoundOnDiskOrInAnyRepositoryError", PluginNotFoundOnDiskOrInAnyRepositoryError{}),
    45  		Entry("NoPluginRepositoriesError", NoPluginRepositoriesError{}),
    46  		Entry("NoCompatibleBinaryError", NoCompatibleBinaryError{}),
    47  		Entry("RepositoryNameTakenError", RepositoryNameTakenError{}),
    48  		Entry("AddPluginRepositoryError", AddPluginRepositoryError{}),
    49  		Entry("GettingPluginRepositoryError", GettingPluginRepositoryError{}),
    50  		Entry("FileNotFoundError", FileNotFoundError{}),
    51  		Entry("PluginInvalidError", PluginInvalidError{}),
    52  		Entry("PluginInvalidError", PluginInvalidError{Err: errors.New("invalid error")}),
    53  		Entry("PluginCommandsConflictError", PluginCommandsConflictError{}),
    54  		Entry("PluginAlreadyInstalledError", PluginAlreadyInstalledError{}),
    55  		Entry("DownloadPluginHTTPError", DownloadPluginHTTPError{}),
    56  		Entry("FetchingPluginInfoFromRepositoriesError", FetchingPluginInfoFromRepositoriesError{}),
    57  	)
    58  
    59  	Describe("PluginInvalidError", func() {
    60  		Context("when the wrapped error is nil", func() {
    61  			It("does not concatenate the nil error in the returned Error()", func() {
    62  				err := PluginInvalidError{}
    63  				Expect(err.Error()).To(Equal("File is not a valid cf CLI plugin binary."))
    64  			})
    65  		})
    66  
    67  		Context("when the wrapped error is not nil", func() {
    68  			It("does prepends the error message in the returned Error()", func() {
    69  				err := PluginInvalidError{Err: errors.New("ello")}
    70  				Expect(err.Error()).To(Equal("ello\nFile is not a valid cf CLI plugin binary."))
    71  			})
    72  		})
    73  	})
    74  })