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