github.com/rivy-go/git-changelog@v0.0.0-20240424224517-b86e6ab57773/internal/changelog/changelog_test.go (about)

     1  package changelog
     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/rivy-go/git-changelog",
    85  		},
    86  		Options: &Options{},
    87  	})
    88  
    89  	buf := &bytes.Buffer{}
    90  	err := gen.Generate(buf, "")
    91  	assert.Nil(err)
    92  	assert.Equal("", buf.String())
    93  }
    94  
    95  func TestGeneratorNotFoundCommits(t *testing.T) {
    96  	assert := assert.New(t)
    97  	testName := "not_found"
    98  
    99  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   100  		commit("2018-01-01 00:00:00", "feat(*): New feature", "")
   101  		tag("1.0.0")
   102  	})
   103  
   104  	gen := NewGenerator(&Config{
   105  		Bin:        "git",
   106  		WorkingDir: filepath.Join(testRepoRoot, testName),
   107  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   108  		Info: &Info{
   109  			RepositoryURL: "https://github.com/rivy-go/git-changelog",
   110  		},
   111  		Options: &Options{},
   112  	})
   113  
   114  	buf := &bytes.Buffer{}
   115  	err := gen.Generate(buf, "foo")
   116  	assert.Error(err)
   117  	assert.Equal("", buf.String())
   118  }
   119  
   120  func TestGeneratorNotFoundCommitsOne(t *testing.T) {
   121  	assert := assert.New(t)
   122  	testName := "not_found"
   123  
   124  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   125  		commit("2018-01-01 00:00:00", "chore(*): First commit", "")
   126  		tag("1.0.0")
   127  	})
   128  
   129  	gen := NewGenerator(&Config{
   130  		Bin:        "git",
   131  		WorkingDir: filepath.Join(testRepoRoot, testName),
   132  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   133  		Info: &Info{
   134  			RepositoryURL: "https://github.com/rivy-go/git-changelog",
   135  		},
   136  		Options: &Options{
   137  			CommitFilters:        map[string][]string{},
   138  			CommitSortBy:         "Scope",
   139  			CommitGroupBy:        "Type",
   140  			CommitGroupSortBy:    "Title",
   141  			CommitGroupTitleMaps: map[string]string{},
   142  			HeaderPattern:        "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
   143  			HeaderPatternMaps: []string{
   144  				"Type",
   145  				"Scope",
   146  				"Subject",
   147  			},
   148  			IssuePrefix: []string{
   149  				"#",
   150  				"gh-",
   151  			},
   152  			RefActions:   []string{},
   153  			MergePattern: "^Merge pull request #(\\d+) from (.*)$",
   154  			MergePatternMaps: []string{
   155  				"Ref",
   156  				"Source",
   157  			},
   158  			RevertPattern: "^Revert \"([\\s\\S]*)\"$",
   159  			RevertPatternMaps: []string{
   160  				"Header",
   161  			},
   162  			NoteKeywords: []string{
   163  				"BREAKING CHANGE",
   164  			},
   165  		},
   166  	})
   167  
   168  	buf := &bytes.Buffer{}
   169  	err := gen.Generate(buf, "foo")
   170  	assert.Error(err)
   171  	assert.Contains(err.Error(), "\"foo\" was not found")
   172  	assert.Equal("", buf.String())
   173  }
   174  
   175  // func TestGeneratorWithTypeScopeSubject(t *testing.T) {
   176  //  assert := assert.New(t)
   177  //  testName := "type_scope_subject"
   178  
   179  //  setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   180  //      commit("2018-01-01 00:00:00", "chore(*): First commit", "")
   181  //      commit("2018-01-01 00:01:00", "feat(core): Add foo bar", "")
   182  //      commit("2018-01-01 00:02:00", "docs(readme): Update usage #123", "")
   183  //      tag("1.0.0")
   184  
   185  //      commit("2018-01-02 00:00:00", "feat(parser): New some super options #333", "")
   186  //      commit("2018-01-02 00:01:00", "Merge pull request #999 from tsuyoshiwada/patch-1", "")
   187  //      commit("2018-01-02 00:02:00", "Merge pull request #1000 from tsuyoshiwada/patch-1", "")
   188  //      commit("2018-01-02 00:03:00", "Revert \"feat(core): Add foo bar @mention and issue #987\"", "")
   189  //      tag("1.1.0")
   190  
   191  //      commit("2018-01-03 00:00:00", "feat(context): Online breaking change", "BREAKING CHANGE: Online breaking change message.")
   192  //      commit("2018-01-03 00:01:00", "feat(router): Muliple breaking change", `This is body,
   193  
   194  // BREAKING CHANGE:
   195  // Multiple
   196  // breaking
   197  // change message.`)
   198  //      tag("2.0.0-beta.0")
   199  
   200  //      commit("2018-01-04 00:00:00", "refactor(context): gofmt", "")
   201  //      commit("2018-01-04 00:01:00", "fix(core): Fix commit\n\nThis is body message.", "")
   202  //  })
   203  
   204  //  gen := NewGenerator(&Config{
   205  //      Bin:        "git",
   206  //      WorkingDir: filepath.Join(testRepoRoot, testName),
   207  //      Template:   filepath.Join(cwd, "testdata", testName+".md"),
   208  //      Info: &Info{
   209  //          Title:         "CHANGELOG Example",
   210  //          RepositoryURL: "https://github.com/rivy-go/git-changelog",
   211  //      },
   212  //      Options: &Options{
   213  //          CommitFilters: map[string][]string{
   214  //              "Type": []string{
   215  //                  "feat",
   216  //                  "fix",
   217  //              },
   218  //          },
   219  //          CommitSortBy:      "Scope",
   220  //          CommitGroupBy:     "Type",
   221  //          CommitGroupSortBy: "Title",
   222  //          CommitGroupTitleMaps: map[string]string{
   223  //              "feat": "Features",
   224  //              "fix":  "Bug Fixes",
   225  //          },
   226  //          HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
   227  //          HeaderPatternMaps: []string{
   228  //              "Type",
   229  //              "Scope",
   230  //              "Subject",
   231  //          },
   232  //          IssuePrefix: []string{
   233  //              "#",
   234  //              "gh-",
   235  //          },
   236  //          RefActions:   []string{},
   237  //          MergePattern: "^Merge pull request #(\\d+) from (.*)$",
   238  //          MergePatternMaps: []string{
   239  //              "Ref",
   240  //              "Source",
   241  //          },
   242  //          RevertPattern: "^Revert \"([\\s\\S]*)\"$",
   243  //          RevertPatternMaps: []string{
   244  //              "Header",
   245  //          },
   246  //          NoteKeywords: []string{
   247  //              "BREAKING CHANGE",
   248  //          },
   249  //      },
   250  //  })
   251  
   252  //  buf := &bytes.Buffer{}
   253  //  err := gen.Generate(buf, "")
   254  //  output := strings.Replace(strings.TrimSpace(buf.String()), "\r\n", "\n", -1)
   255  
   256  //  assert.Nil(err)
   257  //  assert.Equal(`<a name="unreleased"></a>
   258  // ## [Unreleased]
   259  
   260  // ### Bug Fixes
   261  // - **core:** Fix commit
   262  
   263  // <a name="2.0.0-beta.0"></a>
   264  // ## [2.0.0-beta.0] - 2018-01-03
   265  // ### Features
   266  // - **context:** Online breaking change
   267  // - **router:** Muliple breaking change
   268  
   269  // ### BREAKING CHANGE
   270  
   271  // Multiple
   272  // breaking
   273  // change message.
   274  
   275  // Online breaking change message.
   276  
   277  // <a name="1.1.0"></a>
   278  // ## [1.1.0] - 2018-01-02
   279  // ### Features
   280  // - **parser:** New some super options #333
   281  
   282  // ### Reverts
   283  // - feat(core): Add foo bar @mention and issue #987
   284  
   285  // ### Pull Requests
   286  // - Merge pull request #1000 from tsuyoshiwada/patch-1
   287  // - Merge pull request #999 from tsuyoshiwada/patch-1
   288  
   289  // <a name="1.0.0"></a>
   290  // ## 1.0.0 - 2018-01-01
   291  // ### Features
   292  // - **core:** Add foo bar
   293  
   294  // [Unreleased]: https://github.com/rivy-go/git-changelog/compare/2.0.0-beta.0...HEAD
   295  // [2.0.0-beta.0]: https://github.com/rivy-go/git-changelog/compare/1.1.0...2.0.0-beta.0
   296  // [1.1.0]: https://github.com/rivy-go/git-changelog/compare/1.0.0...1.1.0`, output)
   297  // }
   298  
   299  func TestGeneratorWithNextTag(t *testing.T) {
   300  	assert := assert.New(t)
   301  	testName := "type_scope_subject"
   302  
   303  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   304  		commit("2018-01-01 00:00:00", "feat(core): version 1.0.0", "")
   305  		tag("1.0.0")
   306  
   307  		commit("2018-02-01 00:00:00", "feat(core): version 2.0.0", "")
   308  		tag("2.0.0")
   309  
   310  		commit("2018-03-01 00:00:00", "feat(core): version 3.0.0", "")
   311  	})
   312  
   313  	gen := NewGenerator(&Config{
   314  		Bin:        "git",
   315  		WorkingDir: filepath.Join(testRepoRoot, testName),
   316  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   317  		Info: &Info{
   318  			Title:         "CHANGELOG Example",
   319  			RepositoryURL: "https://github.com/rivy-go/git-changelog",
   320  		},
   321  		Options: &Options{
   322  			NextTag: "3.0.0",
   323  			CommitFilters: map[string][]string{
   324  				"Type": []string{
   325  					"feat",
   326  				},
   327  			},
   328  			CommitSortBy:      "Scope",
   329  			CommitGroupBy:     "Type",
   330  			CommitGroupSortBy: "Title",
   331  			CommitGroupTitleMaps: map[string]string{
   332  				"feat": "Features",
   333  			},
   334  			HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
   335  			HeaderPatternMaps: []string{
   336  				"Type",
   337  				"Scope",
   338  				"Subject",
   339  			},
   340  		},
   341  	})
   342  
   343  	buf := &bytes.Buffer{}
   344  	err := gen.Generate(buf, "")
   345  	output := strings.Replace(strings.TrimSpace(buf.String()), "\r\n", "\n", -1)
   346  
   347  	assert.Nil(err)
   348  	assert.Equal(`<a name="unreleased"></a>
   349  ## [Unreleased]
   350  
   351  
   352  <a name="3.0.0"></a>
   353  ## [3.0.0] - 2018-03-01
   354  ### Features
   355  - **core:** version 3.0.0
   356  
   357  
   358  <a name="2.0.0"></a>
   359  ## [2.0.0] - 2018-02-01
   360  ### Features
   361  - **core:** version 2.0.0
   362  
   363  
   364  <a name="1.0.0"></a>
   365  ## 1.0.0 - 2018-01-01
   366  ### Features
   367  - **core:** version 1.0.0
   368  
   369  
   370  [Unreleased]: https://github.com/rivy-go/git-changelog/compare/3.0.0...HEAD
   371  [3.0.0]: https://github.com/rivy-go/git-changelog/compare/2.0.0...3.0.0
   372  [2.0.0]: https://github.com/rivy-go/git-changelog/compare/1.0.0...2.0.0`, output)
   373  
   374  	buf = &bytes.Buffer{}
   375  	err = gen.Generate(buf, "3.0.0")
   376  	output = strings.Replace(strings.TrimSpace(buf.String()), "\r\n", "\n", -1)
   377  
   378  	assert.Nil(err)
   379  	assert.Equal(`<a name="unreleased"></a>
   380  ## [Unreleased]
   381  
   382  
   383  <a name="3.0.0"></a>
   384  ## [3.0.0] - 2018-03-01
   385  ### Features
   386  - **core:** version 3.0.0
   387  
   388  
   389  [Unreleased]: https://github.com/rivy-go/git-changelog/compare/3.0.0...HEAD
   390  [3.0.0]: https://github.com/rivy-go/git-changelog/compare/2.0.0...3.0.0`, output)
   391  }
   392  
   393  func TestGeneratorWithTagFiler(t *testing.T) {
   394  	assert := assert.New(t)
   395  	testName := "type_scope_subject"
   396  
   397  	setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
   398  		commit("2018-01-01 00:00:00", "feat(core): version dev-1.0.0", "")
   399  		tag("dev-1.0.0")
   400  
   401  		commit("2018-02-01 00:00:00", "feat(core): version v1.0.0", "")
   402  		tag("v1.0.0")
   403  	})
   404  
   405  	gen := NewGenerator(&Config{
   406  		Bin:        "git",
   407  		WorkingDir: filepath.Join(testRepoRoot, testName),
   408  		Template:   filepath.Join(cwd, "testdata", testName+".md"),
   409  		Info: &Info{
   410  			Title:         "CHANGELOG Example",
   411  			RepositoryURL: "https://github.com/rivy-go/git-changelog",
   412  		},
   413  		Options: &Options{
   414  			TagFilterPattern: "^v",
   415  			CommitFilters: map[string][]string{
   416  				"Type": []string{
   417  					"feat",
   418  				},
   419  			},
   420  			CommitSortBy:      "Scope",
   421  			CommitGroupBy:     "Type",
   422  			CommitGroupSortBy: "Title",
   423  			CommitGroupTitleMaps: map[string]string{
   424  				"feat": "Features",
   425  			},
   426  			HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
   427  			HeaderPatternMaps: []string{
   428  				"Type",
   429  				"Scope",
   430  				"Subject",
   431  			},
   432  		},
   433  	})
   434  
   435  	buf := &bytes.Buffer{}
   436  	err := gen.Generate(buf, "")
   437  
   438  	assert.Nil(err)
   439  	assert.Equal(`<a name="unreleased"></a>
   440  ## [Unreleased]
   441  
   442  
   443  <a name="v1.0.0"></a>
   444  ## v1.0.0 - 2018-02-01
   445  ### Features
   446  - **core:** version v1.0.0
   447  - **core:** version dev-1.0.0
   448  
   449  
   450  [Unreleased]: https://github.com/rivy-go/git-changelog/compare/v1.0.0...HEAD`, strings.TrimSpace(buf.String()))
   451  
   452  }