github.1git.de/goreleaser/goreleaser@v0.92.0/internal/pipe/release/release_test.go (about)

     1  package release
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"sync"
    10  	"testing"
    11  
    12  	"github.com/goreleaser/goreleaser/internal/artifact"
    13  	"github.com/goreleaser/goreleaser/internal/testlib"
    14  	"github.com/goreleaser/goreleaser/pkg/config"
    15  	"github.com/goreleaser/goreleaser/pkg/context"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestPipeDescription(t *testing.T) {
    20  	assert.NotEmpty(t, Pipe{}.String())
    21  }
    22  
    23  func TestRunPipe(t *testing.T) {
    24  	folder, err := ioutil.TempDir("", "goreleasertest")
    25  	assert.NoError(t, err)
    26  	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
    27  	assert.NoError(t, err)
    28  	debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
    29  	assert.NoError(t, err)
    30  	var config = config.Project{
    31  		Dist: folder,
    32  		Release: config.Release{
    33  			GitHub: config.Repo{
    34  				Owner: "test",
    35  				Name:  "test",
    36  			},
    37  		},
    38  	}
    39  	var ctx = context.New(config)
    40  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
    41  	ctx.Artifacts.Add(artifact.Artifact{
    42  		Type: artifact.UploadableArchive,
    43  		Name: "bin.tar.gz",
    44  		Path: tarfile.Name(),
    45  	})
    46  	ctx.Artifacts.Add(artifact.Artifact{
    47  		Type: artifact.LinuxPackage,
    48  		Name: "bin.deb",
    49  		Path: debfile.Name(),
    50  	})
    51  	client := &DummyClient{}
    52  	assert.NoError(t, doPublish(ctx, client))
    53  	assert.True(t, client.CreatedRelease)
    54  	assert.True(t, client.UploadedFile)
    55  	assert.Contains(t, client.UploadedFileNames, "bin.deb")
    56  	assert.Contains(t, client.UploadedFileNames, "bin.tar.gz")
    57  }
    58  
    59  func TestRunPipeReleaseCreationFailed(t *testing.T) {
    60  	var config = config.Project{
    61  		Release: config.Release{
    62  			GitHub: config.Repo{
    63  				Owner: "test",
    64  				Name:  "test",
    65  			},
    66  		},
    67  	}
    68  	var ctx = context.New(config)
    69  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
    70  	client := &DummyClient{
    71  		FailToCreateRelease: true,
    72  	}
    73  	assert.Error(t, doPublish(ctx, client))
    74  	assert.False(t, client.CreatedRelease)
    75  	assert.False(t, client.UploadedFile)
    76  }
    77  
    78  func TestRunPipeWithFileThatDontExist(t *testing.T) {
    79  	var config = config.Project{
    80  		Release: config.Release{
    81  			GitHub: config.Repo{
    82  				Owner: "test",
    83  				Name:  "test",
    84  			},
    85  		},
    86  	}
    87  	var ctx = context.New(config)
    88  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
    89  	ctx.Artifacts.Add(artifact.Artifact{
    90  		Type: artifact.UploadableArchive,
    91  		Name: "bin.tar.gz",
    92  		Path: "/nope/nope/nope",
    93  	})
    94  	client := &DummyClient{}
    95  	assert.Error(t, doPublish(ctx, client))
    96  	assert.True(t, client.CreatedRelease)
    97  	assert.False(t, client.UploadedFile)
    98  }
    99  
   100  func TestRunPipeUploadFailure(t *testing.T) {
   101  	folder, err := ioutil.TempDir("", "goreleasertest")
   102  	assert.NoError(t, err)
   103  	tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
   104  	assert.NoError(t, err)
   105  	var config = config.Project{
   106  		Release: config.Release{
   107  			GitHub: config.Repo{
   108  				Owner: "test",
   109  				Name:  "test",
   110  			},
   111  		},
   112  	}
   113  	var ctx = context.New(config)
   114  	ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
   115  	ctx.Artifacts.Add(artifact.Artifact{
   116  		Type: artifact.UploadableArchive,
   117  		Name: "bin.tar.gz",
   118  		Path: tarfile.Name(),
   119  	})
   120  	client := &DummyClient{
   121  		FailToUpload: true,
   122  	}
   123  	assert.Error(t, doPublish(ctx, client))
   124  	assert.True(t, client.CreatedRelease)
   125  	assert.False(t, client.UploadedFile)
   126  }
   127  
   128  func TestPipeDisabled(t *testing.T) {
   129  	var ctx = context.New(config.Project{
   130  		Release: config.Release{
   131  			Disable: true,
   132  		},
   133  	})
   134  	client := &DummyClient{}
   135  	testlib.AssertSkipped(t, doPublish(ctx, client))
   136  	assert.False(t, client.CreatedRelease)
   137  	assert.False(t, client.UploadedFile)
   138  }
   139  
   140  func TestDefault(t *testing.T) {
   141  	_, back := testlib.Mktmp(t)
   142  	defer back()
   143  	testlib.GitInit(t)
   144  	testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
   145  
   146  	var ctx = context.New(config.Project{})
   147  	assert.NoError(t, Pipe{}.Default(ctx))
   148  	assert.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Name)
   149  	assert.Equal(t, "goreleaser", ctx.Config.Release.GitHub.Owner)
   150  }
   151  
   152  func TestDefaultPipeDisabled(t *testing.T) {
   153  	_, back := testlib.Mktmp(t)
   154  	defer back()
   155  	testlib.GitInit(t)
   156  	testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
   157  
   158  	var ctx = context.New(config.Project{
   159  		Release: config.Release{
   160  			Disable: true,
   161  		},
   162  	})
   163  	assert.NoError(t, Pipe{}.Default(ctx))
   164  	assert.Equal(t, "", ctx.Config.Release.GitHub.Name)
   165  	assert.Equal(t, "", ctx.Config.Release.GitHub.Owner)
   166  }
   167  
   168  func TestDefaultFilled(t *testing.T) {
   169  	_, back := testlib.Mktmp(t)
   170  	defer back()
   171  	testlib.GitInit(t)
   172  	testlib.GitRemoteAdd(t, "git@github.com:goreleaser/goreleaser.git")
   173  
   174  	var ctx = &context.Context{
   175  		Config: config.Project{
   176  			Release: config.Release{
   177  				GitHub: config.Repo{
   178  					Name:  "foo",
   179  					Owner: "bar",
   180  				},
   181  			},
   182  		},
   183  	}
   184  	assert.NoError(t, Pipe{}.Default(ctx))
   185  	assert.Equal(t, "foo", ctx.Config.Release.GitHub.Name)
   186  	assert.Equal(t, "bar", ctx.Config.Release.GitHub.Owner)
   187  }
   188  
   189  func TestDefaultNotAGitRepo(t *testing.T) {
   190  	_, back := testlib.Mktmp(t)
   191  	defer back()
   192  	var ctx = &context.Context{
   193  		Config: config.Project{},
   194  	}
   195  	assert.EqualError(t, Pipe{}.Default(ctx), "current folder is not a git repository")
   196  	assert.Empty(t, ctx.Config.Release.GitHub.String())
   197  }
   198  
   199  func TestDefaultGitRepoWithoutOrigin(t *testing.T) {
   200  	_, back := testlib.Mktmp(t)
   201  	defer back()
   202  	var ctx = &context.Context{
   203  		Config: config.Project{},
   204  	}
   205  	testlib.GitInit(t)
   206  	assert.EqualError(t, Pipe{}.Default(ctx), "repository doesn't have an `origin` remote")
   207  	assert.Empty(t, ctx.Config.Release.GitHub.String())
   208  }
   209  
   210  func TestDefaultNotAGitRepoSnapshot(t *testing.T) {
   211  	_, back := testlib.Mktmp(t)
   212  	defer back()
   213  	var ctx = &context.Context{
   214  		Config: config.Project{},
   215  	}
   216  	ctx.Snapshot = true
   217  	assert.NoError(t, Pipe{}.Default(ctx))
   218  	assert.Empty(t, ctx.Config.Release.GitHub.String())
   219  }
   220  
   221  func TestDefaultGitRepoWithoutRemote(t *testing.T) {
   222  	_, back := testlib.Mktmp(t)
   223  	defer back()
   224  	var ctx = &context.Context{
   225  		Config: config.Project{},
   226  	}
   227  	assert.Error(t, Pipe{}.Default(ctx))
   228  	assert.Empty(t, ctx.Config.Release.GitHub.String())
   229  }
   230  
   231  type DummyClient struct {
   232  	FailToCreateRelease bool
   233  	FailToUpload        bool
   234  	CreatedRelease      bool
   235  	UploadedFile        bool
   236  	UploadedFileNames   []string
   237  	Lock                sync.Mutex
   238  }
   239  
   240  func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int64, err error) {
   241  	if client.FailToCreateRelease {
   242  		return 0, errors.New("release failed")
   243  	}
   244  	client.CreatedRelease = true
   245  	return
   246  }
   247  
   248  func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) {
   249  	return
   250  }
   251  
   252  func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) (err error) {
   253  	client.Lock.Lock()
   254  	defer client.Lock.Unlock()
   255  	if client.FailToUpload {
   256  		return errors.New("upload failed")
   257  	}
   258  	client.UploadedFile = true
   259  	client.UploadedFileNames = append(client.UploadedFileNames, name)
   260  	return
   261  }