github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/translatableerror/translate_test.go (about)

     1  package translatableerror_test
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"text/template"
     7  
     8  	. "github.com/liamawhite/cli-with-i18n/command/translatableerror"
     9  
    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.(TranslatableError)
    35  			Expect(ok).To(BeTrue())
    36  			err.Translate(translateFunc)
    37  		},
    38  
    39  		Entry("AddPluginRepositoryError", AddPluginRepositoryError{}),
    40  		Entry("APINotFoundError", APINotFoundError{}),
    41  		Entry("APIRequestError", APIRequestError{}),
    42  		Entry("ApplicationNotFoundError", ApplicationNotFoundError{}),
    43  		Entry("AppNotFoundInManifestError", AppNotFoundInManifestError{}),
    44  		Entry("ArgumentCombinationError", ArgumentCombinationError{}),
    45  		Entry("AssignDropletError", AssignDropletError{}),
    46  		Entry("BadCredentialsError", BadCredentialsError{}),
    47  		Entry("CFNetworkingEndpointNotFoundError", CFNetworkingEndpointNotFoundError{}),
    48  		Entry("CommandLineArgsWithMultipleAppsError", CommandLineArgsWithMultipleAppsError{}),
    49  		Entry("DockerPasswordNotSetError", DockerPasswordNotSetError{}),
    50  		Entry("DownloadPluginHTTPError", DownloadPluginHTTPError{}),
    51  		Entry("EmptyDirectoryError", EmptyDirectoryError{}),
    52  		Entry("FetchingPluginInfoFromRepositoriesError", FetchingPluginInfoFromRepositoriesError{}),
    53  		Entry("FileChangedError", FileChangedError{}),
    54  		Entry("FileNotFoundError", FileNotFoundError{}),
    55  		Entry("GettingPluginRepositoryError", GettingPluginRepositoryError{}),
    56  		Entry("HealthCheckTypeUnsupportedError", HealthCheckTypeUnsupportedError{SupportedTypes: []string{"some-type", "another-type"}}),
    57  		Entry("HTTPHealthCheckInvalidError", HTTPHealthCheckInvalidError{}),
    58  		Entry("InvalidSSLCertError", InvalidSSLCertError{}),
    59  		Entry("IsolationSegmentNotFoundError", IsolationSegmentNotFoundError{}),
    60  		Entry("JobFailedError", JobFailedError{}),
    61  		Entry("JobTimeoutError", JobTimeoutError{}),
    62  		Entry("JSONSyntaxError", JSONSyntaxError{Err: errors.New("some-error")}),
    63  		Entry("LifecycleMinimumAPIVersionNotMetError", LifecycleMinimumAPIVersionNotMetError{}),
    64  		Entry("MinimumAPIVersionNotMetError", MinimumAPIVersionNotMetError{}),
    65  		Entry("NetworkPolicyProtocolOrPortNotProvidedError", NetworkPolicyProtocolOrPortNotProvidedError{}),
    66  		Entry("NoAPISetError", NoAPISetError{}),
    67  		Entry("NoCompatibleBinaryError", NoCompatibleBinaryError{}),
    68  		Entry("NoDomainsFoundError", NoDomainsFoundError{}),
    69  		Entry("NoMatchingDomainError", NoMatchingDomainError{}),
    70  		Entry("NoOrganizationTargetedError", NoOrganizationTargetedError{}),
    71  		Entry("NoPluginRepositoriesError", NoPluginRepositoriesError{}),
    72  		Entry("NoSpaceTargetedError", NoSpaceTargetedError{}),
    73  		Entry("NotLoggedInError", NotLoggedInError{}),
    74  		Entry("OrgNotFoundError", OrganizationNotFoundError{}),
    75  		Entry("ParseArgumentError", ParseArgumentError{}),
    76  		Entry("PluginAlreadyInstalledError", PluginAlreadyInstalledError{}),
    77  		Entry("PluginBinaryRemoveFailedError", PluginBinaryRemoveFailedError{}),
    78  		Entry("PluginBinaryUninstallError", PluginBinaryUninstallError{}),
    79  		Entry("PluginCommandsConflictError", PluginCommandsConflictError{}),
    80  		Entry("PluginInvalidError", PluginInvalidError{Err: errors.New("invalid error")}),
    81  		Entry("PluginInvalidError", PluginInvalidError{}),
    82  		Entry("PluginNotFoundError", PluginNotFoundError{}),
    83  		Entry("PluginNotFoundInRepositoryError", PluginNotFoundInRepositoryError{}),
    84  		Entry("PluginNotFoundOnDiskOrInAnyRepositoryError", PluginNotFoundOnDiskOrInAnyRepositoryError{}),
    85  		Entry("PortNotAllowedWithHTTPDomainError", PortNotAllowedWithHTTPDomainError{}),
    86  		Entry("RepositoryNameTakenError", RepositoryNameTakenError{}),
    87  		Entry("RequiredArgumentError", RequiredArgumentError{}),
    88  		Entry("RequiredFlagsError", RequiredFlagsError{}),
    89  		Entry("RequiredNameForPushError", RequiredNameForPushError{}),
    90  		Entry("RouteInDifferentSpaceError", RouteInDifferentSpaceError{}),
    91  		Entry("RunTaskError", RunTaskError{}),
    92  		Entry("SecurityGroupNotFoundError", SecurityGroupNotFoundError{}),
    93  		Entry("ServiceInstanceNotFoundError", ServiceInstanceNotFoundError{}),
    94  		Entry("SpaceNotFoundError", SpaceNotFoundError{}),
    95  		Entry("SSLCertError", SSLCertError{}),
    96  		Entry("StackNotFoundError with name", SpaceNotFoundError{Name: "steve"}),
    97  		Entry("StackNotFoundError without name", SpaceNotFoundError{}),
    98  		Entry("StagingFailedError", StagingFailedError{}),
    99  		Entry("StagingFailedNoAppDetectedError", StagingFailedNoAppDetectedError{}),
   100  		Entry("StagingTimeoutError", StagingTimeoutError{}),
   101  		Entry("StartupTimeoutError", StartupTimeoutError{}),
   102  		Entry("ThreeRequiredArgumentsError", ThreeRequiredArgumentsError{}),
   103  		Entry("UnsuccessfulStartError", UnsuccessfulStartError{}),
   104  		Entry("UnsupportedURLSchemeError", UnsupportedURLSchemeError{}),
   105  		Entry("UploadFailedError", UploadFailedError{Err: JobFailedError{}}),
   106  		Entry("V3APIDoesNotExistError", V3APIDoesNotExistError{}),
   107  	)
   108  
   109  	Describe("PluginInvalidError", func() {
   110  		Context("when the wrapped error is nil", func() {
   111  			It("does not concatenate the nil error in the returned Error()", func() {
   112  				err := PluginInvalidError{}
   113  				Expect(err.Error()).To(Equal("File is not a valid cf CLI plugin binary."))
   114  			})
   115  		})
   116  
   117  		Context("when the wrapped error is not nil", func() {
   118  			It("does prepends the error message in the returned Error()", func() {
   119  				err := PluginInvalidError{Err: errors.New("ello")}
   120  				Expect(err.Error()).To(Equal("ello\nFile is not a valid cf CLI plugin binary."))
   121  			})
   122  		})
   123  	})
   124  })