github.com/BrandonManuel/git-chglog@v0.0.0-20200903004639-7a62fa08787a/chglog_test.go (about)

     1  package chglog
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	gitcmd "github.com/tsuyoshiwada/go-gitcmd"
    13  )
    14  
    15  var (
    16  	cwd                string
    17  	testRepoRoot       = ".tmp"
    18  	internalTimeFormat = "2006-01-02 15:04:05"
    19  )
    20  
    21  type commitFunc = func(date, subject, body string)
    22  type tagFunc = func(name string)
    23  
    24  func TestMain(m *testing.M) {
    25  	cwd, _ = os.Getwd()
    26  	cleanup()
    27  	code := m.Run()
    28  	cleanup()
    29  	os.Exit(code)
    30  }
    31  
    32  func setup(dir string, setupRepo func(commitFunc, tagFunc, gitcmd.Client)) {
    33  	testDir := filepath.Join(cwd, testRepoRoot, dir)
    34  
    35  	os.RemoveAll(testDir)
    36  	os.MkdirAll(testDir, os.ModePerm)
    37  	os.Chdir(testDir)
    38  
    39  	loc, _ := time.LoadLocation("UTC")
    40  	time.Local = loc
    41  
    42  	git := gitcmd.New(nil)
    43  	git.Exec("init")
    44  	git.Exec("config", "user.name", "test_user")
    45  	git.Exec("config", "user.email", "test@example.com")
    46  
    47  	var commit = func(date, subject, body string) {
    48  		msg := subject
    49  		if body != "" {
    50  			msg += "\n\n" + body
    51  		}
    52  		t, _ := time.Parse(internalTimeFormat, date)
    53  		d := t.Format("Mon Jan 2 15:04:05 2006 +0000")
    54  		git.Exec("commit", "--allow-empty", "--date", d, "-m", msg)
    55  	}
    56  
    57  	var tag = func(name string) {
    58  		git.Exec("tag", name)
    59  	}
    60  
    61  	setupRepo(commit, tag, git)
    62  
    63  	os.Chdir(cwd)
    64  }
    65  
    66  func cleanup() {
    67  	os.Chdir(cwd)
    68  	os.RemoveAll(filepath.Join(cwd, testRepoRoot))
    69  }
    70  
    71  func TestGeneratorNotFoundTags(t *testing.T) {
    72  	assert := assert.New(t)
    73  	testName := "not_found"
    74  
    75  	setup(testName, func(commit commitFunc, _ tagFunc, _ gitcmd.Client) {
    76  		commit("2018-01-01 00:00:00", "feat(*): New feature", "")
    77  	})
    78  
    79  	gen := NewGenerator(&Config{
    80  		Bin:        "git",
    81  		WorkingDir: filepath.Join(testRepoRoot, testName),
    82  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
    83  		Info: &Info{
    84  			RepositoryURL: "https://github.com/BrandonManuel/git-chglog",
    85  		},
    86  		Options: &Options{},
    87  	})
    88  
    89  	buf := &bytes.Buffer{}
    90  	err := gen.Generate(buf, "")
    91  	assert.Error(err)
    92  	assert.Contains(err.Error(), "git-tag does not exist")
    93  	assert.Equal("", buf.String())
    94  }
    95  
    96  func TestGeneratorNotFoundCommits(t *testing.T) {
    97  	assert := assert.New(t)
    98  	testName := "not_found"
    99  
   100  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   101  		commit("2018-01-01 00:00:00", "feat(*): New feature", "")
   102  		tag("1.0.0")
   103  	})
   104  
   105  	gen := NewGenerator(&Config{
   106  		Bin:        "git",
   107  		WorkingDir: filepath.Join(testRepoRoot, testName),
   108  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   109  		Info: &Info{
   110  			RepositoryURL: "https://github.com/BrandonManuel/git-chglog",
   111  		},
   112  		Options: &Options{},
   113  	})
   114  
   115  	buf := &bytes.Buffer{}
   116  	err := gen.Generate(buf, "foo")
   117  	assert.Error(err)
   118  	assert.Equal("", buf.String())
   119  }
   120  
   121  func TestGeneratorNotFoundCommitsOne(t *testing.T) {
   122  	assert := assert.New(t)
   123  	testName := "not_found"
   124  
   125  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   126  		commit("2018-01-01 00:00:00", "chore(*): First commit", "")
   127  		tag("1.0.0")
   128  	})
   129  
   130  	gen := NewGenerator(&Config{
   131  		Bin:        "git",
   132  		WorkingDir: filepath.Join(testRepoRoot, testName),
   133  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   134  		Info: &Info{
   135  			RepositoryURL: "https://github.com/BrandonManuel/git-chglog",
   136  		},
   137  		Options: &Options{
   138  			CommitFilters:        map[string][]string{},
   139  			CommitSortBy:         "Scope",
   140  			CommitGroupBy:        "Type",
   141  			CommitGroupSortBy:    "Title",
   142  			CommitGroupTitleMaps: map[string]string{},
   143  			HeaderPattern:        "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
   144  			HeaderPatternMaps: []string{
   145  				"Type",
   146  				"Scope",
   147  				"Subject",
   148  			},
   149  			IssuePrefix: []string{
   150  				"#",
   151  				"gh-",
   152  			},
   153  			RefActions:   []string{},
   154  			MergePattern: "^Merge pull request #(\\d+) from (.*)$",
   155  			MergePatternMaps: []string{
   156  				"Ref",
   157  				"Source",
   158  			},
   159  			RevertPattern: "^Revert \"([\\s\\S]*)\"$",
   160  			RevertPatternMaps: []string{
   161  				"Header",
   162  			},
   163  			NoteKeywords: []string{
   164  				"BREAKING CHANGE",
   165  			},
   166  		},
   167  	})
   168  
   169  	buf := &bytes.Buffer{}
   170  	err := gen.Generate(buf, "foo")
   171  	assert.Error(err)
   172  	assert.Contains(err.Error(), "\"foo\" was not found")
   173  	assert.Equal("", buf.String())
   174  }
   175  
   176  func TestGeneratorWithTypeScopeSubject(t *testing.T) {
   177  	assert := assert.New(t)
   178  	testName := "type_scope_subject"
   179  
   180  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   181  		commit("2018-01-01 00:00:00", "chore(*): First commit", "")
   182  		commit("2018-01-01 00:01:00", "feat(core): Add foo bar", "")
   183  		commit("2018-01-01 00:02:00", "docs(readme): Update usage #123", "")
   184  		tag("1.0.0")
   185  
   186  		commit("2018-01-02 00:00:00", "feat(parser): New some super options #333", "")
   187  		commit("2018-01-02 00:01:00", "Merge pull request #999 from tsuyoshiwada/patch-1", "")
   188  		commit("2018-01-02 00:02:00", "Merge pull request #1000 from tsuyoshiwada/patch-1", "")
   189  		commit("2018-01-02 00:03:00", "Revert \"feat(core): Add foo bar @mention and issue #987\"", "")
   190  		tag("1.1.0")
   191  
   192  		commit("2018-01-03 00:00:00", "feat(context): Online breaking change", "BREAKING CHANGE: Online breaking change message.")
   193  		commit("2018-01-03 00:01:00", "feat(router): Muliple breaking change", `This is body,
   194  
   195  BREAKING CHANGE:
   196  Multiple
   197  breaking
   198  change message.`)
   199  		tag("2.0.0-beta.0")
   200  
   201  		commit("2018-01-04 00:00:00", "refactor(context): gofmt", "")
   202  		commit("2018-01-04 00:01:00", "fix(core): Fix commit\n\nThis is body message.", "")
   203  	})
   204  
   205  	gen := NewGenerator(&Config{
   206  		Bin:        "git",
   207  		WorkingDir: filepath.Join(testRepoRoot, testName),
   208  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   209  		Info: &Info{
   210  			Title:         "CHANGELOG Example",
   211  			RepositoryURL: "https://github.com/BrandonManuel/git-chglog",
   212  		},
   213  		Options: &Options{
   214  			CommitFilters: map[string][]string{
   215  				"Type": []string{
   216  					"feat",
   217  					"fix",
   218  				},
   219  			},
   220  			CommitSortBy:      "Scope",
   221  			CommitGroupBy:     "Type",
   222  			CommitGroupSortBy: "Title",
   223  			CommitGroupTitleMaps: map[string]string{
   224  				"feat": "Features",
   225  				"fix":  "Bug Fixes",
   226  			},
   227  			HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
   228  			HeaderPatternMaps: []string{
   229  				"Type",
   230  				"Scope",
   231  				"Subject",
   232  			},
   233  			IssuePrefix: []string{
   234  				"#",
   235  				"gh-",
   236  			},
   237  			RefActions:   []string{},
   238  			MergePattern: "^Merge pull request #(\\d+) from (.*)$",
   239  			MergePatternMaps: []string{
   240  				"Ref",
   241  				"Source",
   242  			},
   243  			RevertPattern: "^Revert \"([\\s\\S]*)\"$",
   244  			RevertPatternMaps: []string{
   245  				"Header",
   246  			},
   247  			NoteKeywords: []string{
   248  				"BREAKING CHANGE",
   249  			},
   250  		},
   251  	})
   252  
   253  	buf := &bytes.Buffer{}
   254  	err := gen.Generate(buf, "")
   255  	output := strings.Replace(strings.TrimSpace(buf.String()), "\r\n", "\n", -1)
   256  
   257  	assert.Nil(err)
   258  	assert.Equal(`<a name="unreleased"></a>
   259  ## [Unreleased]
   260  
   261  ### Bug Fixes
   262  - **core:** Fix commit
   263  
   264  
   265  <a name="2.0.0-beta.0"></a>
   266  ## [2.0.0-beta.0] - 2018-01-03
   267  ### Features
   268  - **context:** Online breaking change
   269  - **router:** Muliple breaking change
   270  
   271  ### BREAKING CHANGE
   272  
   273  Multiple
   274  breaking
   275  change message.
   276  
   277  Online breaking change message.
   278  
   279  
   280  <a name="1.1.0"></a>
   281  ## [1.1.0] - 2018-01-02
   282  ### Features
   283  - **parser:** New some super options #333
   284  
   285  ### Reverts
   286  - feat(core): Add foo bar @mention and issue #987
   287  
   288  ### Puewtwetll Requests
   289  - Merge pull request #1000 from tsuyoshiwada/patch-1
   290  - Merge pull request #999 from tsuyoshiwada/patch-1
   291  
   292  
   293  <a name="1.0.0"></a>
   294  ## 1.0.0 - 2018-01-01
   295  ### Features
   296  - **core:** Add foo bar
   297  
   298  
   299  [Unreleased]: https://github.com/BrandonManuel/git-chglog/compare/2.0.0-beta.0...HEAD
   300  [2.0.0-beta.0]: https://github.com/BrandonManuel/git-chglog/compare/1.1.0...2.0.0-beta.0
   301  [1.1.0]: https://github.com/BrandonManuel/git-chglog/compare/1.0.0...1.1.0`, output)
   302  }
   303  
   304  func TestGeneratorWithNextTag(t *testing.T) {
   305  	assert := assert.New(t)
   306  	testName := "type_scope_subject"
   307  
   308  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   309  		commit("2018-01-01 00:00:00", "feat(core): version 1.0.0", "")
   310  		tag("1.0.0")
   311  
   312  		commit("2018-02-01 00:00:00", "feat(core): version 2.0.0", "")
   313  		tag("2.0.0")
   314  
   315  		commit("2018-03-01 00:00:00", "feat(core): version 3.0.0", "")
   316  	})
   317  
   318  	gen := NewGenerator(&Config{
   319  		Bin:        "git",
   320  		WorkingDir: filepath.Join(testRepoRoot, testName),
   321  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   322  		Info: &Info{
   323  			Title:         "CHANGELOG Example",
   324  			RepositoryURL: "https://github.com/BrandonManuel/git-chglog",
   325  		},
   326  		Options: &Options{
   327  			NextTag: "3.0.0",
   328  			CommitFilters: map[string][]string{
   329  				"Type": []string{
   330  					"feat",
   331  				},
   332  			},
   333  			CommitSortBy:      "Scope",
   334  			CommitGroupBy:     "Type",
   335  			CommitGroupSortBy: "Title",
   336  			CommitGroupTitleMaps: map[string]string{
   337  				"feat": "Features",
   338  			},
   339  			HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
   340  			HeaderPatternMaps: []string{
   341  				"Type",
   342  				"Scope",
   343  				"Subject",
   344  			},
   345  		},
   346  	})
   347  
   348  	buf := &bytes.Buffer{}
   349  	err := gen.Generate(buf, "")
   350  	output := strings.Replace(strings.TrimSpace(buf.String()), "\r\n", "\n", -1)
   351  
   352  	assert.Nil(err)
   353  	assert.Equal(`<a name="unreleased"></a>
   354  ## [Unreleased]
   355  
   356  
   357  <a name="3.0.0"></a>
   358  ## [3.0.0] - 2018-03-01
   359  ### Features
   360  - **core:** version 3.0.0
   361  
   362  
   363  <a name="2.0.0"></a>
   364  ## [2.0.0] - 2018-02-01
   365  ### Features
   366  - **core:** version 2.0.0
   367  
   368  
   369  <a name="1.0.0"></a>
   370  ## 1.0.0 - 2018-01-01
   371  ### Features
   372  - **core:** version 1.0.0
   373  
   374  
   375  [Unreleased]: https://github.com/BrandonManuel/git-chglog/compare/3.0.0...HEAD
   376  [3.0.0]: https://github.com/BrandonManuel/git-chglog/compare/2.0.0...3.0.0
   377  [2.0.0]: https://github.com/BrandonManuel/git-chglog/compare/1.0.0...2.0.0`, output)
   378  
   379  	buf = &bytes.Buffer{}
   380  	err = gen.Generate(buf, "3.0.0")
   381  	output = strings.Replace(strings.TrimSpace(buf.String()), "\r\n", "\n", -1)
   382  
   383  	assert.Nil(err)
   384  	assert.Equal(`<a name="unreleased"></a>
   385  ## [Unreleased]
   386  
   387  
   388  <a name="3.0.0"></a>
   389  ## [3.0.0] - 2018-03-01
   390  ### Features
   391  - **core:** version 3.0.0
   392  
   393  
   394  [Unreleased]: https://github.com/BrandonManuel/git-chglog/compare/3.0.0...HEAD
   395  [3.0.0]: https://github.com/BrandonManuel/git-chglog/compare/2.0.0...3.0.0`, output)
   396  }
   397  
   398  func TestGeneratorWithTagFiler(t *testing.T) {
   399  	assert := assert.New(t)
   400  	testName := "type_scope_subject"
   401  
   402  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   403  		commit("2018-01-01 00:00:00", "feat(core): version dev-1.0.0", "")
   404  		tag("dev-1.0.0")
   405  
   406  		commit("2018-02-01 00:00:00", "feat(core): version v1.0.0", "")
   407  		tag("v1.0.0")
   408  	})
   409  
   410  	gen := NewGenerator(&Config{
   411  		Bin:        "git",
   412  		WorkingDir: filepath.Join(testRepoRoot, testName),
   413  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   414  		Info: &Info{
   415  			Title:         "CHANGELOG Example",
   416  			RepositoryURL: "https://github.com/BrandonManuel/git-chglog",
   417  		},
   418  		Options: &Options{
   419  			TagFilterPattern: "^v",
   420  			CommitFilters: map[string][]string{
   421  				"Type": []string{
   422  					"feat",
   423  				},
   424  			},
   425  			CommitSortBy:      "Scope",
   426  			CommitGroupBy:     "Type",
   427  			CommitGroupSortBy: "Title",
   428  			CommitGroupTitleMaps: map[string]string{
   429  				"feat": "Features",
   430  			},
   431  			HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
   432  			HeaderPatternMaps: []string{
   433  				"Type",
   434  				"Scope",
   435  				"Subject",
   436  			},
   437  		},
   438  	})
   439  
   440  	buf := &bytes.Buffer{}
   441  	err := gen.Generate(buf, "")
   442  
   443  	assert.Nil(err)
   444  	assert.Equal(`<a name="unreleased"></a>
   445  ## [Unreleased]
   446  
   447  
   448  <a name="v1.0.0"></a>
   449  ## v1.0.0 - 2018-02-01
   450  ### Features
   451  - **core:** version v1.0.0
   452  - **core:** version dev-1.0.0
   453  
   454  
   455  [Unreleased]: https://github.com/BrandonManuel/git-chglog/compare/v1.0.0...HEAD`, strings.TrimSpace(buf.String()))
   456  
   457  }