github.com/goreleaser/goreleaser@v1.25.1/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/goreleaser/goreleaser/internal/artifact"
    11  	"github.com/goreleaser/goreleaser/pkg/config"
    12  	"github.com/goreleaser/goreleaser/pkg/context"
    13  )
    14  
    15  var (
    16  	_ Client                = &Mock{}
    17  	_ ReleaseNotesGenerator = &Mock{}
    18  	_ PullRequestOpener     = &Mock{}
    19  	_ ForkSyncer            = &Mock{}
    20  )
    21  
    22  func NewMock() *Mock {
    23  	return &Mock{}
    24  }
    25  
    26  type Mock struct {
    27  	CreatedFile          bool
    28  	Content              string
    29  	Path                 string
    30  	Messages             []string
    31  	FailToCreateRelease  bool
    32  	FailToUpload         bool
    33  	CreatedRelease       bool
    34  	UploadedFile         bool
    35  	ReleasePublished     bool
    36  	UploadedFileNames    []string
    37  	UploadedFilePaths    map[string]string
    38  	FailFirstUpload      bool
    39  	Lock                 sync.Mutex
    40  	ClosedMilestone      string
    41  	FailToCloseMilestone bool
    42  	Changes              string
    43  	ReleaseNotes         string
    44  	ReleaseNotesParams   []string
    45  	OpenedPullRequest    bool
    46  	SyncedFork           bool
    47  }
    48  
    49  func (c *Mock) SyncFork(_ *context.Context, _ Repo, _ Repo) error {
    50  	c.SyncedFork = true
    51  	return nil
    52  }
    53  
    54  func (c *Mock) OpenPullRequest(_ *context.Context, _, _ Repo, _ string, _ bool) error {
    55  	c.OpenedPullRequest = true
    56  	return nil
    57  }
    58  
    59  func (c *Mock) Changelog(_ *context.Context, _ Repo, _, _ string) (string, error) {
    60  	if c.Changes != "" {
    61  		return c.Changes, nil
    62  	}
    63  	return "", ErrNotImplemented
    64  }
    65  
    66  func (c *Mock) GenerateReleaseNotes(_ *context.Context, _ Repo, prev, current string) (string, error) {
    67  	if c.ReleaseNotes != "" {
    68  		c.ReleaseNotesParams = []string{prev, current}
    69  		return c.ReleaseNotes, nil
    70  	}
    71  	return "", ErrNotImplemented
    72  }
    73  
    74  func (c *Mock) CloseMilestone(_ *context.Context, _ Repo, title string) error {
    75  	if c.FailToCloseMilestone {
    76  		return errors.New("milestone failed")
    77  	}
    78  
    79  	c.ClosedMilestone = title
    80  
    81  	return nil
    82  }
    83  
    84  func (c *Mock) CreateRelease(_ *context.Context, _ string) (string, error) {
    85  	if c.FailToCreateRelease {
    86  		return "", errors.New("release failed")
    87  	}
    88  	c.CreatedRelease = true
    89  	return "", nil
    90  }
    91  
    92  func (c *Mock) PublishRelease(_ *context.Context, _ string /* releaseID */) (err error) {
    93  	c.ReleasePublished = true
    94  	return nil
    95  }
    96  
    97  func (c *Mock) ReleaseURLTemplate(_ *context.Context) (string, error) {
    98  	return "https://dummyhost/download/{{ .Tag }}/{{ .ArtifactName }}", nil
    99  }
   100  
   101  func (c *Mock) CreateFile(_ *context.Context, _ config.CommitAuthor, _ Repo, content []byte, path, msg string) error {
   102  	c.CreatedFile = true
   103  	c.Content = string(content)
   104  	c.Path = path
   105  	c.Messages = append(c.Messages, msg)
   106  	return nil
   107  }
   108  
   109  func (c *Mock) Upload(_ *context.Context, _ string, artifact *artifact.Artifact, file *os.File) error {
   110  	c.Lock.Lock()
   111  	defer c.Lock.Unlock()
   112  	if c.UploadedFilePaths == nil {
   113  		c.UploadedFilePaths = map[string]string{}
   114  	}
   115  	// ensure file is read to better mimic real behavior
   116  	_, err := io.ReadAll(file)
   117  	if err != nil {
   118  		return fmt.Errorf("unexpected error: %w", err)
   119  	}
   120  	if c.FailToUpload {
   121  		return errors.New("upload failed")
   122  	}
   123  	if c.FailFirstUpload {
   124  		c.FailFirstUpload = false
   125  		return RetriableError{Err: errors.New("upload failed, should retry")}
   126  	}
   127  	c.UploadedFile = true
   128  	c.UploadedFileNames = append(c.UploadedFileNames, artifact.Name)
   129  	c.UploadedFilePaths[artifact.Name] = artifact.Path
   130  	return nil
   131  }