github.com/sergiusens/goreleaser@v0.34.3-0.20171009111917-ae6f7c157c5c/pipeline/git/git_test.go (about)

     1  package git
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/goreleaser/goreleaser/config"
    10  	"github.com/goreleaser/goreleaser/context"
    11  	"github.com/goreleaser/goreleaser/internal/testlib"
    12  	"github.com/goreleaser/goreleaser/pipeline/defaults"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestDescription(t *testing.T) {
    17  	assert.NotEmpty(t, Pipe{}.Description())
    18  }
    19  
    20  func TestNotAGitFolder(t *testing.T) {
    21  	_, back := testlib.Mktmp(t)
    22  	defer back()
    23  	var ctx = &context.Context{
    24  		Config: config.Project{},
    25  	}
    26  	assert.Error(t, Pipe{}.Run(ctx))
    27  }
    28  
    29  func TestSingleCommit(t *testing.T) {
    30  	_, back := testlib.Mktmp(t)
    31  	defer back()
    32  	testlib.GitInit(t)
    33  	testlib.GitCommit(t, "commit1")
    34  	testlib.GitTag(t, "v0.0.1")
    35  	var ctx = &context.Context{
    36  		Config: config.Project{},
    37  	}
    38  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    39  	assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
    40  }
    41  
    42  func TestNewRepository(t *testing.T) {
    43  	_, back := testlib.Mktmp(t)
    44  	defer back()
    45  	testlib.GitInit(t)
    46  	var ctx = &context.Context{
    47  		Config: config.Project{},
    48  	}
    49  	assert.Error(t, Pipe{}.Run(ctx))
    50  }
    51  
    52  func TestNoTagsSnapshot(t *testing.T) {
    53  	_, back := testlib.Mktmp(t)
    54  	defer back()
    55  	testlib.GitInit(t)
    56  	testlib.GitCommit(t, "first")
    57  	var ctx = &context.Context{
    58  		Config: config.Project{
    59  			Snapshot: config.Snapshot{
    60  				NameTemplate: defaults.SnapshotNameTemplate,
    61  			},
    62  		},
    63  		Snapshot: true,
    64  		Publish:  false,
    65  	}
    66  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    67  	assert.Contains(t, ctx.Version, "SNAPSHOT-")
    68  }
    69  
    70  func TestNoTagsSnapshotInvalidTemplate(t *testing.T) {
    71  	_, back := testlib.Mktmp(t)
    72  	defer back()
    73  	testlib.GitInit(t)
    74  	testlib.GitCommit(t, "first")
    75  	var ctx = &context.Context{
    76  		Config: config.Project{
    77  			Snapshot: config.Snapshot{
    78  				NameTemplate: "{{",
    79  			},
    80  		},
    81  		Snapshot: true,
    82  		Publish:  false,
    83  	}
    84  	assert.Error(t, Pipe{}.Run(ctx))
    85  }
    86  
    87  // TestNoTagsNoSnapshot covers the situation where a repository
    88  // only contains simple commits and no tags. In this case you have
    89  // to set the --snapshot flag otherwise an error is returned.
    90  func TestNoTagsNoSnapshot(t *testing.T) {
    91  	_, back := testlib.Mktmp(t)
    92  	defer back()
    93  	testlib.GitInit(t)
    94  	testlib.GitCommit(t, "first")
    95  	var ctx = &context.Context{
    96  		Config: config.Project{
    97  			Snapshot: config.Snapshot{
    98  				NameTemplate: defaults.SnapshotNameTemplate,
    99  			},
   100  		},
   101  		Snapshot: false,
   102  		Publish:  false,
   103  	}
   104  	assert.Error(t, Pipe{}.Run(ctx))
   105  }
   106  
   107  func TestInvalidTagFormat(t *testing.T) {
   108  	_, back := testlib.Mktmp(t)
   109  	defer back()
   110  	testlib.GitInit(t)
   111  	testlib.GitCommit(t, "commit2")
   112  	testlib.GitTag(t, "sadasd")
   113  	var ctx = &context.Context{
   114  		Config:   config.Project{},
   115  		Validate: true,
   116  	}
   117  	assert.EqualError(t, Pipe{}.Run(ctx), "sadasd is not in a valid version format")
   118  	assert.Equal(t, "sadasd", ctx.Git.CurrentTag)
   119  }
   120  
   121  func TestDirty(t *testing.T) {
   122  	folder, back := testlib.Mktmp(t)
   123  	defer back()
   124  	testlib.GitInit(t)
   125  	dummy, err := os.Create(filepath.Join(folder, "dummy"))
   126  	assert.NoError(t, err)
   127  	testlib.GitAdd(t)
   128  	testlib.GitCommit(t, "commit2")
   129  	testlib.GitTag(t, "v0.0.1")
   130  	assert.NoError(t, ioutil.WriteFile(dummy.Name(), []byte("lorem ipsum"), 0644))
   131  	var ctx = &context.Context{
   132  		Config:   config.Project{},
   133  		Validate: true,
   134  	}
   135  	err = Pipe{}.Run(ctx)
   136  	assert.Error(t, err)
   137  	assert.Contains(t, err.Error(), "git is currently in a dirty state:")
   138  }
   139  
   140  func TestTagIsNotLastCommit(t *testing.T) {
   141  	_, back := testlib.Mktmp(t)
   142  	defer back()
   143  	testlib.GitInit(t)
   144  	testlib.GitCommit(t, "commit3")
   145  	testlib.GitTag(t, "v0.0.1")
   146  	testlib.GitCommit(t, "commit4")
   147  	var ctx = &context.Context{
   148  		Config:   config.Project{},
   149  		Validate: true,
   150  	}
   151  	err := Pipe{}.Run(ctx)
   152  	assert.Error(t, err)
   153  	assert.Contains(t, err.Error(), "git tag v0.0.1 was not made against commit")
   154  }
   155  
   156  func TestValidState(t *testing.T) {
   157  	_, back := testlib.Mktmp(t)
   158  	defer back()
   159  	testlib.GitInit(t)
   160  	testlib.GitCommit(t, "commit3")
   161  	testlib.GitTag(t, "v0.0.1")
   162  	testlib.GitCommit(t, "commit4")
   163  	testlib.GitTag(t, "v0.0.2")
   164  	var ctx = &context.Context{
   165  		Config:   config.Project{},
   166  		Validate: true,
   167  	}
   168  	assert.NoError(t, Pipe{}.Run(ctx))
   169  	assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
   170  	assert.NotContains(t, "commit4", ctx.ReleaseNotes)
   171  	assert.NotContains(t, "commit3", ctx.ReleaseNotes)
   172  }
   173  
   174  func TestNoValidate(t *testing.T) {
   175  	_, back := testlib.Mktmp(t)
   176  	defer back()
   177  	testlib.GitInit(t)
   178  	testlib.GitAdd(t)
   179  	testlib.GitCommit(t, "commit5")
   180  	testlib.GitTag(t, "v0.0.1")
   181  	testlib.GitCommit(t, "commit6")
   182  	var ctx = &context.Context{
   183  		Config:   config.Project{},
   184  		Validate: false,
   185  	}
   186  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   187  }
   188  
   189  func TestChangelog(t *testing.T) {
   190  	_, back := testlib.Mktmp(t)
   191  	defer back()
   192  	testlib.GitInit(t)
   193  	testlib.GitCommit(t, "first")
   194  	testlib.GitTag(t, "v0.0.1")
   195  	testlib.GitCommit(t, "added feature 1")
   196  	testlib.GitCommit(t, "fixed bug 2")
   197  	testlib.GitTag(t, "v0.0.2")
   198  	var ctx = &context.Context{
   199  		Config: config.Project{},
   200  	}
   201  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   202  	assert.Equal(t, "v0.0.2", ctx.Git.CurrentTag)
   203  	assert.Contains(t, ctx.ReleaseNotes, "## Changelog")
   204  	assert.NotContains(t, ctx.ReleaseNotes, "first")
   205  	assert.Contains(t, ctx.ReleaseNotes, "added feature 1")
   206  	assert.Contains(t, ctx.ReleaseNotes, "fixed bug 2")
   207  }
   208  
   209  func TestChangelogOfFirstRelease(t *testing.T) {
   210  	_, back := testlib.Mktmp(t)
   211  	defer back()
   212  	testlib.GitInit(t)
   213  	var msgs = []string{
   214  		"initial commit",
   215  		"another one",
   216  		"one more",
   217  		"and finally this one",
   218  	}
   219  	for _, msg := range msgs {
   220  		testlib.GitCommit(t, msg)
   221  	}
   222  	testlib.GitTag(t, "v0.0.1")
   223  	var ctx = &context.Context{
   224  		Config: config.Project{},
   225  	}
   226  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   227  	assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
   228  	assert.Contains(t, ctx.ReleaseNotes, "## Changelog")
   229  	for _, msg := range msgs {
   230  		assert.Contains(t, ctx.ReleaseNotes, msg)
   231  	}
   232  }
   233  
   234  func TestCustomReleaseNotes(t *testing.T) {
   235  	_, back := testlib.Mktmp(t)
   236  	defer back()
   237  	testlib.GitInit(t)
   238  	testlib.GitCommit(t, "first")
   239  	testlib.GitTag(t, "v0.0.1")
   240  	var ctx = &context.Context{
   241  		Config:       config.Project{},
   242  		ReleaseNotes: "custom",
   243  	}
   244  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
   245  	assert.Equal(t, "v0.0.1", ctx.Git.CurrentTag)
   246  	assert.Equal(t, ctx.ReleaseNotes, "custom")
   247  }