github.com/triarius/goreleaser@v1.12.5/internal/client/mock.go (about) 1 package client 2 3 import ( 4 "errors" 5 "fmt" 6 "io" 7 "os" 8 "sync" 9 10 "github.com/triarius/goreleaser/internal/artifact" 11 "github.com/triarius/goreleaser/pkg/config" 12 "github.com/triarius/goreleaser/pkg/context" 13 ) 14 15 var ( 16 _ Client = &Mock{} 17 _ GitHubClient = &Mock{} 18 ) 19 20 func NewMock() *Mock { 21 return &Mock{} 22 } 23 24 type Mock struct { 25 CreatedFile bool 26 Content string 27 Path string 28 FailToCreateRelease bool 29 FailToUpload bool 30 CreatedRelease bool 31 UploadedFile bool 32 UploadedFileNames []string 33 UploadedFilePaths map[string]string 34 FailFirstUpload bool 35 Lock sync.Mutex 36 ClosedMilestone string 37 FailToCloseMilestone bool 38 Changes string 39 ReleaseNotes string 40 } 41 42 func (c *Mock) Changelog(ctx *context.Context, repo Repo, prev, current string) (string, error) { 43 if c.Changes != "" { 44 return c.Changes, nil 45 } 46 return "", ErrNotImplemented 47 } 48 49 func (c *Mock) GenerateReleaseNotes(ctx *context.Context, repo Repo, prev, current string) (string, error) { 50 if c.ReleaseNotes != "" { 51 return c.ReleaseNotes, nil 52 } 53 return "", ErrNotImplemented 54 } 55 56 func (c *Mock) CloseMilestone(ctx *context.Context, repo Repo, title string) error { 57 if c.FailToCloseMilestone { 58 return errors.New("milestone failed") 59 } 60 61 c.ClosedMilestone = title 62 63 return nil 64 } 65 66 func (c *Mock) GetDefaultBranch(ctx *context.Context, repo Repo) (string, error) { 67 return "", ErrNotImplemented 68 } 69 70 func (c *Mock) CreateRelease(ctx *context.Context, body string) (string, error) { 71 if c.FailToCreateRelease { 72 return "", errors.New("release failed") 73 } 74 c.CreatedRelease = true 75 return "", nil 76 } 77 78 func (c *Mock) ReleaseURLTemplate(ctx *context.Context) (string, error) { 79 return "https://dummyhost/download/{{ .Tag }}/{{ .ArtifactName }}", nil 80 } 81 82 func (c *Mock) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo Repo, content []byte, path, msg string) error { 83 c.CreatedFile = true 84 c.Content = string(content) 85 c.Path = path 86 return nil 87 } 88 89 func (c *Mock) Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) error { 90 c.Lock.Lock() 91 defer c.Lock.Unlock() 92 if c.UploadedFilePaths == nil { 93 c.UploadedFilePaths = map[string]string{} 94 } 95 // ensure file is read to better mimic real behavior 96 _, err := io.ReadAll(file) 97 if err != nil { 98 return fmt.Errorf("unexpected error: %w", err) 99 } 100 if c.FailToUpload { 101 return errors.New("upload failed") 102 } 103 if c.FailFirstUpload { 104 c.FailFirstUpload = false 105 return RetriableError{Err: errors.New("upload failed, should retry")} 106 } 107 c.UploadedFile = true 108 c.UploadedFileNames = append(c.UploadedFileNames, artifact.Name) 109 c.UploadedFilePaths[artifact.Name] = artifact.Path 110 return nil 111 }