github.com/szyn/goreleaser@v0.76.1-0.20180517112710-333da09a1297/pipeline/changelog/changelog_test.go (about)

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