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

     1  package chglog
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestCommitParserParse(t *testing.T) {
    15  	assert := assert.New(t)
    16  	assert.True(true)
    17  
    18  	mock := &mockClient{
    19  		ReturnExec: func(subcmd string, args ...string) (string, error) {
    20  			if subcmd != "log" {
    21  				return "", errors.New("")
    22  			}
    23  
    24  			bytes, _ := ioutil.ReadFile(filepath.Join("testdata", "gitlog.txt"))
    25  
    26  			return string(bytes), nil
    27  		},
    28  	}
    29  
    30  	parser := newCommitParser(mock, &Config{
    31  		Options: &Options{
    32  			CommitFilters: map[string][]string{
    33  				"Type": []string{
    34  					"feat",
    35  					"fix",
    36  					"perf",
    37  					"refactor",
    38  				},
    39  			},
    40  			HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
    41  			HeaderPatternMaps: []string{
    42  				"Type",
    43  				"Scope",
    44  				"Subject",
    45  			},
    46  			IssuePrefix: []string{
    47  				"#",
    48  				"gh-",
    49  			},
    50  			RefActions: []string{
    51  				"close",
    52  				"closes",
    53  				"closed",
    54  				"fix",
    55  				"fixes",
    56  				"fixed",
    57  				"resolve",
    58  				"resolves",
    59  				"resolved",
    60  			},
    61  			MergePattern: "^Merge pull request #(\\d+) from (.*)$",
    62  			MergePatternMaps: []string{
    63  				"Ref",
    64  				"Source",
    65  			},
    66  			RevertPattern: "^Revert \"([\\s\\S]*)\"$",
    67  			RevertPatternMaps: []string{
    68  				"Header",
    69  			},
    70  			NoteKeywords: []string{
    71  				"BREAKING CHANGE",
    72  			},
    73  		},
    74  	})
    75  
    76  	commits, err := parser.Parse("HEAD")
    77  	assert.Nil(err)
    78  	assert.Equal([]*Commit{
    79  		&Commit{
    80  			Hash: &Hash{
    81  				Long:  "65cf1add9735dcc4810dda3312b0792236c97c4e",
    82  				Short: "65cf1add",
    83  			},
    84  			Author: &Author{
    85  				Name:  "tsuyoshi wada",
    86  				Email: "mail@example.com",
    87  				Date:  time.Unix(int64(1514808000), 0),
    88  			},
    89  			Committer: &Committer{
    90  				Name:  "tsuyoshi wada",
    91  				Email: "mail@example.com",
    92  				Date:  time.Unix(int64(1514808000), 0),
    93  			},
    94  			Merge:  nil,
    95  			Revert: nil,
    96  			Refs: []*Ref{
    97  				&Ref{
    98  					Action: "",
    99  					Ref:    "123",
   100  					Source: "",
   101  				},
   102  			},
   103  			Notes:    []*Note{},
   104  			Mentions: []string{},
   105  			Header:   "feat(*): Add new feature #123",
   106  			Type:     "feat",
   107  			Scope:    "*",
   108  			Subject:  "Add new feature #123",
   109  			Body:     "",
   110  		},
   111  		&Commit{
   112  			Hash: &Hash{
   113  				Long:  "14ef0b6d386c5432af9292eab3c8314fa3001bc7",
   114  				Short: "14ef0b6d",
   115  			},
   116  			Author: &Author{
   117  				Name:  "tsuyoshi wada",
   118  				Email: "mail@example.com",
   119  				Date:  time.Unix(int64(1515153600), 0),
   120  			},
   121  			Committer: &Committer{
   122  				Name:  "tsuyoshi wada",
   123  				Email: "mail@example.com",
   124  				Date:  time.Unix(int64(1515153600), 0),
   125  			},
   126  			Merge: &Merge{
   127  				Ref:    "3",
   128  				Source: "username/branchname",
   129  			},
   130  			Revert: nil,
   131  			Refs: []*Ref{
   132  				&Ref{
   133  					Action: "",
   134  					Ref:    "3",
   135  					Source: "",
   136  				},
   137  				&Ref{
   138  					Action: "Fixes",
   139  					Ref:    "3",
   140  					Source: "",
   141  				},
   142  				&Ref{
   143  					Action: "Closes",
   144  					Ref:    "1",
   145  					Source: "",
   146  				},
   147  			},
   148  			Notes: []*Note{
   149  				&Note{
   150  					Title: "BREAKING CHANGE",
   151  					Body:  "This is breaking point message.",
   152  				},
   153  			},
   154  			Mentions: []string{},
   155  			Header:   "Merge pull request #3 from username/branchname",
   156  			Type:     "",
   157  			Scope:    "",
   158  			Subject:  "",
   159  			Body: `This is body message.
   160  
   161  Fixes #3
   162  
   163  Closes #1
   164  
   165  BREAKING CHANGE: This is breaking point message.`,
   166  		},
   167  		&Commit{
   168  			Hash: &Hash{
   169  				Long:  "809a8280ffd0dadb0f4e7ba9fc835e63c37d6af6",
   170  				Short: "809a8280",
   171  			},
   172  			Author: &Author{
   173  				Name:  "tsuyoshi wada",
   174  				Email: "mail@example.com",
   175  				Date:  time.Unix(int64(1517486400), 0),
   176  			},
   177  			Committer: &Committer{
   178  				Name:  "tsuyoshi wada",
   179  				Email: "mail@example.com",
   180  				Date:  time.Unix(int64(1517486400), 0),
   181  			},
   182  			Merge:  nil,
   183  			Revert: nil,
   184  			Refs:   []*Ref{},
   185  			Notes:  []*Note{},
   186  			Mentions: []string{
   187  				"tsuyoshiwada",
   188  				"hogefuga",
   189  				"FooBarBaz",
   190  			},
   191  			Header:  "fix(controller): Fix cors configure",
   192  			Type:    "fix",
   193  			Scope:   "controller",
   194  			Subject: "Fix cors configure",
   195  			Body: `Has mention body
   196  
   197  @tsuyoshiwada
   198  @hogefuga
   199  @FooBarBaz`,
   200  		},
   201  		&Commit{
   202  			Hash: &Hash{
   203  				Long:  "74824d6bd1470b901ec7123d13a76a1b8938d8d0",
   204  				Short: "74824d6b",
   205  			},
   206  			Author: &Author{
   207  				Name:  "tsuyoshi wada",
   208  				Email: "mail@example.com",
   209  				Date:  time.Unix(int64(1517488587), 0),
   210  			},
   211  			Committer: &Committer{
   212  				Name:  "tsuyoshi wada",
   213  				Email: "mail@example.com",
   214  				Date:  time.Unix(int64(1517488587), 0),
   215  			},
   216  			Merge:  nil,
   217  			Revert: nil,
   218  			Refs: []*Ref{
   219  				&Ref{
   220  					Action: "Fixes",
   221  					Ref:    "123",
   222  					Source: "",
   223  				},
   224  				&Ref{
   225  					Action: "Closes",
   226  					Ref:    "456",
   227  					Source: "username/repository",
   228  				},
   229  			},
   230  			Notes: []*Note{
   231  				&Note{
   232  					Title: "BREAKING CHANGE",
   233  					Body: fmt.Sprintf(`This is multiline breaking change note.
   234  It is treated as the body of the Note until a mention or reference appears.
   235  
   236  We also allow blank lines :)
   237  
   238  Example:
   239  
   240  %sjavascript
   241  import { Controller } from 'hoge-fuga';
   242  
   243  @autobind
   244  class MyController extends Controller {
   245    constructor() {
   246      super();
   247    }
   248  }
   249  %s`, "```", "```"),
   250  				},
   251  			},
   252  			Mentions: []string{},
   253  			Header:   "fix(model): Remove hoge attributes",
   254  			Type:     "fix",
   255  			Scope:    "model",
   256  			Subject:  "Remove hoge attributes",
   257  			Body: fmt.Sprintf(`This mixed body message.
   258  
   259  BREAKING CHANGE:
   260  This is multiline breaking change note.
   261  It is treated as the body of the Note until a mention or reference appears.
   262  
   263  We also allow blank lines :)
   264  
   265  Example:
   266  
   267  %sjavascript
   268  import { Controller } from 'hoge-fuga';
   269  
   270  @autobind
   271  class MyController extends Controller {
   272    constructor() {
   273      super();
   274    }
   275  }
   276  %s
   277  
   278  Fixes #123
   279  Closes username/repository#456`, "```", "```"),
   280  		},
   281  		&Commit{
   282  			Hash: &Hash{
   283  				Long:  "123456789735dcc4810dda3312b0792236c97c4e",
   284  				Short: "12345678",
   285  			},
   286  			Author: &Author{
   287  				Name:  "tsuyoshi wada",
   288  				Email: "mail@example.com",
   289  				Date:  time.Unix(int64(1517488587), 0),
   290  			},
   291  			Committer: &Committer{
   292  				Name:  "tsuyoshi wada",
   293  				Email: "mail@example.com",
   294  				Date:  time.Unix(int64(1517488587), 0),
   295  			},
   296  			Merge: nil,
   297  			Revert: &Revert{
   298  				Header: "fix(core): commit message",
   299  			},
   300  			Refs:     []*Ref{},
   301  			Notes:    []*Note{},
   302  			Mentions: []string{},
   303  			Header:   "Revert \"fix(core): commit message\"",
   304  			Type:     "",
   305  			Scope:    "",
   306  			Subject:  "",
   307  			Body:     "This reverts commit f755db78dcdf461dc42e709b3ab728ceba353d1d.",
   308  		},
   309  	}, commits)
   310  }