github.com/droot/goreleaser@v0.66.2-0.20180420030140-c2db5fb17157/pipeline/changelog/changelog_test.go (about)

     1  package changelog
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/apex/log"
     7  	"github.com/goreleaser/goreleaser/config"
     8  	"github.com/goreleaser/goreleaser/context"
     9  	"github.com/goreleaser/goreleaser/internal/testlib"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestDescription(t *testing.T) {
    14  	assert.NotEmpty(t, Pipe{}.String())
    15  }
    16  
    17  func TestChangelogProvidedViaFlag(t *testing.T) {
    18  	var ctx = context.New(config.Project{})
    19  	ctx.ReleaseNotes = "c0ff33 foo bar"
    20  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    21  }
    22  
    23  func TestSnapshot(t *testing.T) {
    24  	var ctx = context.New(config.Project{})
    25  	ctx.Snapshot = true
    26  	testlib.AssertSkipped(t, Pipe{}.Run(ctx))
    27  }
    28  
    29  func TestChangelog(t *testing.T) {
    30  	_, back := testlib.Mktmp(t)
    31  	defer back()
    32  	testlib.GitInit(t)
    33  	testlib.GitCommit(t, "first")
    34  	testlib.GitTag(t, "v0.0.1")
    35  	testlib.GitCommit(t, "added feature 1")
    36  	testlib.GitCommit(t, "fixed bug 2")
    37  	testlib.GitCommit(t, "ignored: whatever")
    38  	testlib.GitCommit(t, "docs: whatever")
    39  	testlib.GitCommit(t, "something about cArs we dont need")
    40  	testlib.GitCommit(t, "feat: added that thing")
    41  	testlib.GitCommit(t, "Merge pull request #999 from goreleaser/some-branch")
    42  	testlib.GitCommit(t, "this is not a Merge pull request")
    43  	testlib.GitTag(t, "v0.0.2")
    44  	var ctx = context.New(config.Project{
    45  		Changelog: config.Changelog{
    46  			Filters: config.Filters{
    47  				Exclude: []string{
    48  					"docs:",
    49  					"ignored:",
    50  					"(?i)cars",
    51  					"^Merge pull request",
    52  				},
    53  			},
    54  		},
    55  	})
    56  	ctx.Git.CurrentTag = "v0.0.2"
    57  	assert.NoError(t, Pipe{}.Run(ctx))
    58  	assert.Contains(t, ctx.ReleaseNotes, "## Changelog")
    59  	assert.NotContains(t, ctx.ReleaseNotes, "first")
    60  	assert.Contains(t, ctx.ReleaseNotes, "added feature 1")
    61  	assert.Contains(t, ctx.ReleaseNotes, "fixed bug 2")
    62  	assert.NotContains(t, ctx.ReleaseNotes, "docs")
    63  	assert.NotContains(t, ctx.ReleaseNotes, "ignored")
    64  	assert.NotContains(t, ctx.ReleaseNotes, "cArs")
    65  	assert.NotContains(t, ctx.ReleaseNotes, "from goreleaser/some-branch")
    66  }
    67  
    68  func TestChangelogSort(t *testing.T) {
    69  	f, back := testlib.Mktmp(t)
    70  	log.Info(f)
    71  	defer back()
    72  	testlib.GitInit(t)
    73  	testlib.GitCommit(t, "whatever")
    74  	testlib.GitTag(t, "v0.9.9")
    75  	testlib.GitCommit(t, "c: commit")
    76  	testlib.GitCommit(t, "a: commit")
    77  	testlib.GitCommit(t, "b: commit")
    78  	testlib.GitTag(t, "v1.0.0")
    79  	var ctx = context.New(config.Project{
    80  		Changelog: config.Changelog{},
    81  	})
    82  	ctx.Git.CurrentTag = "v1.0.0"
    83  
    84  	for _, cfg := range []struct {
    85  		Sort    string
    86  		Entries []string
    87  	}{
    88  		{
    89  			Sort: "",
    90  			Entries: []string{
    91  				"b: commit",
    92  				"a: commit",
    93  				"c: commit",
    94  			},
    95  		},
    96  		{
    97  			Sort: "asc",
    98  			Entries: []string{
    99  				"a: commit",
   100  				"b: commit",
   101  				"c: commit",
   102  			},
   103  		},
   104  		{
   105  			Sort: "desc",
   106  			Entries: []string{
   107  				"c: commit",
   108  				"b: commit",
   109  				"a: commit",
   110  			},
   111  		},
   112  	} {
   113  		t.Run("changelog sort='"+cfg.Sort+"'", func(t *testing.T) {
   114  			ctx.Config.Changelog.Sort = cfg.Sort
   115  			entries, err := buildChangelog(ctx)
   116  			assert.NoError(t, err)
   117  			assert.Len(t, entries, len(cfg.Entries))
   118  			var changes []string
   119  			for _, line := range entries {
   120  				_, msg := extractCommitInfo(line)
   121  				changes = append(changes, msg)
   122  			}
   123  			assert.EqualValues(t, cfg.Entries, changes)
   124  		})
   125  	}
   126  }
   127  
   128  func TestChangelogInvalidSort(t *testing.T) {
   129  	var ctx = context.New(config.Project{
   130  		Changelog: config.Changelog{
   131  			Sort: "dope",
   132  		},
   133  	})
   134  	assert.EqualError(t, Pipe{}.Run(ctx), ErrInvalidSortDirection.Error())
   135  }
   136  
   137  func TestChangelogOfFirstRelease(t *testing.T) {
   138  	_, back := testlib.Mktmp(t)
   139  	defer back()
   140  	testlib.GitInit(t)
   141  	var msgs = []string{
   142  		"initial commit",
   143  		"another one",
   144  		"one more",
   145  		"and finally this one",
   146  	}
   147  	for _, msg := range msgs {
   148  		testlib.GitCommit(t, msg)
   149  	}
   150  	testlib.GitTag(t, "v0.0.1")
   151  	var ctx = context.New(config.Project{})
   152  	ctx.Git.CurrentTag = "v0.0.1"
   153  	assert.NoError(t, Pipe{}.Run(ctx))
   154  	assert.Contains(t, ctx.ReleaseNotes, "## Changelog")
   155  	for _, msg := range msgs {
   156  		assert.Contains(t, ctx.ReleaseNotes, msg)
   157  	}
   158  }
   159  
   160  func TestChangelogFilterInvalidRegex(t *testing.T) {
   161  	_, back := testlib.Mktmp(t)
   162  	defer back()
   163  	testlib.GitInit(t)
   164  	testlib.GitCommit(t, "commitssss")
   165  	testlib.GitTag(t, "v0.0.3")
   166  	testlib.GitCommit(t, "commitzzz")
   167  	testlib.GitTag(t, "v0.0.4")
   168  	var ctx = context.New(config.Project{
   169  		Changelog: config.Changelog{
   170  			Filters: config.Filters{
   171  				Exclude: []string{
   172  					"(?iasdr4qasd)not a valid regex i guess",
   173  				},
   174  			},
   175  		},
   176  	})
   177  	ctx.Git.CurrentTag = "v0.0.4"
   178  	assert.EqualError(t, Pipe{}.Run(ctx), "error parsing regexp: invalid or unsupported Perl syntax: `(?ia`")
   179  }
   180  
   181  func TestChangelogNoTags(t *testing.T) {
   182  	_, back := testlib.Mktmp(t)
   183  	defer back()
   184  	testlib.GitInit(t)
   185  	testlib.GitCommit(t, "first")
   186  	var ctx = context.New(config.Project{})
   187  	assert.Error(t, Pipe{}.Run(ctx))
   188  	assert.Empty(t, ctx.ReleaseNotes)
   189  }