2019-07-21 15:07:13 +02:00
package changelog_test
import (
"testing"
"github.com/Nightapes/go-semantic-release/internal/analyzer"
"github.com/Nightapes/go-semantic-release/internal/changelog"
"github.com/Nightapes/go-semantic-release/internal/shared"
"github.com/Nightapes/go-semantic-release/pkg/config"
"github.com/stretchr/testify/assert"
"time"
)
func TestChangelog ( t * testing . T ) {
templateConfig := shared . ChangelogTemplateConfig {
CommitURL : "https://commit.url" ,
CompareURL : "https://compare.url" ,
Hash : "hash" ,
Version : "1.0.0" ,
}
testConfigs := [ ] struct {
testCase string
2019-08-11 18:27:52 +02:00
analyzedCommits map [ shared . Release ] [ ] shared . AnalyzedCommit
2019-07-21 15:07:13 +02:00
result * shared . GeneratedChangelog
hasError bool
} {
{
testCase : "feat" ,
2019-08-11 18:27:52 +02:00
analyzedCommits : map [ shared . Release ] [ ] shared . AnalyzedCommit {
2021-01-21 21:41:14 +01:00
"minor" : {
{
2019-08-11 18:27:52 +02:00
Commit : shared . Commit {
2020-01-14 19:35:54 -08:00
Message : "feat(internal/changelog): my first commit" ,
2019-07-21 15:07:13 +02:00
Author : "me" ,
Hash : "12345667" ,
} ,
Scope : "internal/changelog" ,
ParsedMessage : "my first commit" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
2021-02-24 22:41:47 +01:00
Subject : "my first commit" ,
2021-02-16 15:25:34 +01:00
MessageBlocks : map [ string ] [ ] shared . MessageBlock { } ,
2019-07-21 15:07:13 +02:00
} ,
} ,
} ,
result : & shared . GeneratedChangelog {
Title : "v1.0.0 (2019-07-19)" ,
2020-01-26 17:08:57 +01:00
Content : "# v1.0.0 (2019-07-19)\n### Features\n* **`internal/changelog`** my first commit ([1234566](https://commit.url))\n" ,
2020-01-14 19:35:54 -08:00
} ,
hasError : false ,
} ,
{
testCase : "feat no scope" ,
analyzedCommits : map [ shared . Release ] [ ] shared . AnalyzedCommit {
2021-01-21 21:41:14 +01:00
"minor" : {
{
2020-01-14 19:35:54 -08:00
Commit : shared . Commit {
Message : "feat: my first commit" ,
Author : "me" ,
Hash : "12345667" ,
} ,
ParsedMessage : "my first commit" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
2021-02-24 22:41:47 +01:00
Subject : "my first commit" ,
2021-02-16 15:25:34 +01:00
MessageBlocks : map [ string ] [ ] shared . MessageBlock { } ,
2020-01-14 19:35:54 -08:00
} ,
} ,
} ,
result : & shared . GeneratedChangelog {
Title : "v1.0.0 (2019-07-19)" ,
2020-01-26 17:08:57 +01:00
Content : "# v1.0.0 (2019-07-19)\n### Features\n* my first commit ([1234566](https://commit.url))\n" ,
2019-07-21 15:07:13 +02:00
} ,
hasError : false ,
} ,
{
testCase : "feat breaking change" ,
2019-08-11 18:27:52 +02:00
analyzedCommits : map [ shared . Release ] [ ] shared . AnalyzedCommit {
2021-01-21 21:41:14 +01:00
"minor" : {
{
2019-08-11 18:27:52 +02:00
Commit : shared . Commit {
2020-01-14 19:35:54 -08:00
Message : "feat(internal/changelog): my first commit" ,
2019-07-21 15:07:13 +02:00
Author : "me" ,
Hash : "12345667" ,
} ,
Scope : "internal/changelog" ,
ParsedMessage : "my first commit" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
2021-02-24 22:41:47 +01:00
Subject : "my first commit" ,
2021-02-16 15:25:34 +01:00
MessageBlocks : map [ string ] [ ] shared . MessageBlock { } ,
2019-07-21 15:07:13 +02:00
} ,
2021-01-21 21:41:14 +01:00
{
2019-08-11 18:27:52 +02:00
Commit : shared . Commit {
2020-01-14 19:35:54 -08:00
Message : "feat(internal/changelog): my first break: BREAKING CHANGE: change api to v2" ,
2019-07-21 15:07:13 +02:00
Author : "me" ,
Hash : "12345668" ,
} ,
Scope : "internal/changelog" ,
ParsedMessage : "my first break" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
ParsedBreakingChangeMessage : "change api to v2" ,
2021-02-24 22:41:47 +01:00
IsBreaking : true ,
Subject : "my first break" ,
2021-02-16 15:25:34 +01:00
MessageBlocks : map [ string ] [ ] shared . MessageBlock {
2021-02-24 22:41:47 +01:00
"body" : { shared . MessageBlock {
2021-02-16 15:25:34 +01:00
Label : "BREAKING CHANGE" ,
Content : "change api to v2" ,
} ,
} ,
} ,
2019-07-21 15:07:13 +02:00
} ,
} ,
} ,
result : & shared . GeneratedChangelog {
Title : "v1.0.0 (2019-07-19)" ,
2020-01-26 17:08:57 +01:00
Content : "# v1.0.0 (2019-07-19)\n## BREAKING CHANGES\n* **`internal/changelog`** change api to v2 \nintroduced by commit: \nmy first break ([1234566](https://commit.url))\n### Features\n* **`internal/changelog`** my first commit ([1234566](https://commit.url))\n" ,
2019-07-21 15:07:13 +02:00
} ,
hasError : false ,
} ,
2021-01-21 21:41:14 +01:00
{
testCase : "conventional commits" ,
analyzedCommits : map [ shared . Release ] [ ] shared . AnalyzedCommit {
"minor" : {
{
Commit : shared . Commit {
Message : "feat!: my first break \n\nBREAKING CHANGE: hey from the change" ,
Author : "me" ,
Hash : "12345669" ,
} ,
Scope : "" ,
ParsedMessage : "my first break" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
ParsedBreakingChangeMessage : "hey from the change" ,
2021-02-24 22:41:47 +01:00
IsBreaking : true ,
Subject : "my first break" ,
2021-02-16 15:25:34 +01:00
MessageBlocks : map [ string ] [ ] shared . MessageBlock {
2021-02-24 22:41:47 +01:00
"body" : { shared . MessageBlock {
2021-02-16 15:25:34 +01:00
Label : "BREAKING CHANGE" ,
Content : "hey from the change" ,
} ,
} ,
} ,
2021-01-21 21:41:14 +01:00
} ,
{
Commit : shared . Commit {
Message : "feat(internal/changelog): my first commit" ,
Author : "me" ,
Hash : "12345667" ,
} ,
Scope : "internal/changelog" ,
ParsedMessage : "my first commit" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
2021-02-24 22:41:47 +01:00
Subject : "my first commit" ,
2021-02-16 15:25:34 +01:00
MessageBlocks : map [ string ] [ ] shared . MessageBlock { } ,
2021-01-21 21:41:14 +01:00
} ,
{
Commit : shared . Commit {
Message : "feat: my second commit" ,
Author : "me" ,
Hash : "12345667" ,
} ,
Scope : "" ,
ParsedMessage : "my first commit" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
2021-02-24 22:41:47 +01:00
Subject : "my second commit" ,
2021-02-16 15:25:34 +01:00
MessageBlocks : map [ string ] [ ] shared . MessageBlock { } ,
2021-01-21 21:41:14 +01:00
} ,
{
Commit : shared . Commit {
2022-03-04 11:02:52 +01:00
Message : "feat: my first break \n\nBREAKING CHANGE: change api to v2" ,
2021-01-21 21:41:14 +01:00
Author : "me" ,
Hash : "12345668" ,
} ,
Scope : "" ,
ParsedMessage : "my first break" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
ParsedBreakingChangeMessage : "change api to v2" ,
2021-02-24 22:41:47 +01:00
IsBreaking : true ,
2022-03-04 11:02:52 +01:00
Subject : "my first break" ,
2021-02-16 15:25:34 +01:00
MessageBlocks : map [ string ] [ ] shared . MessageBlock {
2021-02-24 22:41:47 +01:00
"body" : { shared . MessageBlock {
2021-02-16 15:25:34 +01:00
Label : "BREAKING CHANGE" ,
Content : "change api to v2" ,
} } ,
} ,
2021-01-21 21:41:14 +01:00
} ,
{
Commit : shared . Commit {
Message : "feat!: my next commit" ,
Author : "me" ,
Hash : "12345668" ,
} ,
Scope : "" ,
ParsedMessage : "" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
ParsedBreakingChangeMessage : "my next commit" ,
2021-02-24 22:41:47 +01:00
IsBreaking : true ,
Subject : "my next commit" ,
MessageBlocks : map [ string ] [ ] shared . MessageBlock { } ,
2021-01-21 21:41:14 +01:00
} ,
} ,
} ,
result : & shared . GeneratedChangelog {
Title : "v1.0.0 (2019-07-19)" ,
2022-03-04 11:02:52 +01:00
Content : "# v1.0.0 (2019-07-19)\n## BREAKING CHANGES\n* hey from the change \nintroduced by commit: \nmy first break ([1234566](https://commit.url))\n* change api to v2 \nintroduced by commit: \nmy first break ([1234566](https://commit.url))\n* my next commit \nintroduced by commit: \nmy next commit ([1234566](https://commit.url))\n### Features\n* **`internal/changelog`** my first commit ([1234566](https://commit.url))\n* my second commit ([1234566](https://commit.url))\n" } ,
2021-01-21 21:41:14 +01:00
hasError : false ,
} ,
2019-07-21 15:07:13 +02:00
}
cl := changelog . New ( & config . ReleaseConfig { } , [ ] analyzer . Rule {
{
Tag : "feat" ,
TagString : "Features" ,
Release : "minor" ,
Changelog : true ,
} ,
{
Tag : "fix" ,
TagString : "Bug fixes" ,
Release : "patch" ,
Changelog : true ,
} ,
{
Tag : "build" ,
TagString : "Build" ,
Release : "none" ,
Changelog : false ,
} ,
} , time . Date ( 2019 , 7 , 19 , 0 , 0 , 0 , 0 , time . UTC ) )
for _ , config := range testConfigs {
2020-01-26 17:08:57 +01:00
t . Run ( config . testCase , func ( t * testing . T ) {
2021-02-22 14:57:50 +01:00
generatedChangelog , err := cl . GenerateChangelog ( templateConfig , config . analyzedCommits )
2020-01-26 17:08:57 +01:00
assert . Equalf ( t , config . hasError , err != nil , "Testcase %s should have error: %t -> %s" , config . testCase , config . hasError , err )
assert . Equalf ( t , config . result , generatedChangelog , "Testcase %s should have generated changelog" , config . testCase )
} )
2019-07-21 15:07:13 +02:00
}
}
2021-02-24 22:41:47 +01:00
func TestChangelogExtensions ( t * testing . T ) {
testConfigs := [ ] struct {
testCase string
result * shared . GeneratedChangelog
releaseConfig * config . ReleaseConfig
} {
{
testCase : "docker" ,
releaseConfig : & config . ReleaseConfig {
Changelog : config . ChangelogConfig {
Docker : config . ChangelogDocker {
Latest : true ,
Repository : "mydocker.de" ,
} ,
NPM : config . ChangelogNPM { } ,
} ,
} ,
result : & shared . GeneratedChangelog { Title : "v1.0.0 (2019-07-19)" , Content : "# v1.0.0 (2019-07-19)\n### Features\n* **`internal/changelog`** my first commit ([1234566](https://commit.url))\n\n## Docker image\n\nNew docker image is released under `mydocker.de:1.0.0`\n\n### Usage\n\n`docker run mydocker.de:1.0.0`\n\nor\n\n`docker run mydocker.de:latest`\n" } ,
} ,
{
testCase : "npm" ,
releaseConfig : & config . ReleaseConfig {
Changelog : config . ChangelogConfig {
Docker : config . ChangelogDocker { } ,
NPM : config . ChangelogNPM {
Repository : "https://github.com/Nightapes/ngx-validators/packages/102720" ,
PackageName : "ngx-validators" ,
} ,
} ,
} ,
result : & shared . GeneratedChangelog { Title : "v1.0.0 (2019-07-19)" , Content : "# v1.0.0 (2019-07-19)\n### Features\n* **`internal/changelog`** my first commit ([1234566](https://commit.url))\n\n## NodeJS Package\n\nNew NodeJS package is released under [ngx-validators](https://github.com/Nightapes/ngx-validators/packages/102720)\n\n### Usage\n\n`yarn add ngx-validators@1.0.0`\n\nor\n\n`npm install -save ngx-validators@1.0.0`\n\n" } ,
} ,
}
analyzedCommits := map [ shared . Release ] [ ] shared . AnalyzedCommit {
"minor" : {
{
Commit : shared . Commit {
Message : "feat(internal/changelog): my first commit" ,
Author : "me" ,
Hash : "12345667" ,
} ,
Scope : "internal/changelog" ,
ParsedMessage : "my first commit" ,
Tag : "feat" ,
TagString : "Features" ,
Print : true ,
Subject : "my first commit" ,
MessageBlocks : map [ string ] [ ] shared . MessageBlock { } ,
} ,
} ,
}
for _ , config := range testConfigs {
t . Run ( config . testCase , func ( t * testing . T ) {
templateConfig := shared . ChangelogTemplateConfig {
CommitURL : "https://commit.url" ,
CompareURL : "https://compare.url" ,
Hash : "hash" ,
Version : "1.0.0" ,
}
cl := changelog . New ( config . releaseConfig , [ ] analyzer . Rule {
{
Tag : "feat" ,
TagString : "Features" ,
Release : "minor" ,
Changelog : true ,
} ,
{
Tag : "fix" ,
TagString : "Bug fixes" ,
Release : "patch" ,
Changelog : true ,
} ,
{
Tag : "build" ,
TagString : "Build" ,
Release : "none" ,
Changelog : false ,
} ,
} , time . Date ( 2019 , 7 , 19 , 0 , 0 , 0 , 0 , time . UTC ) )
generatedChangelog , err := cl . GenerateChangelog ( templateConfig , analyzedCommits )
assert . NoError ( t , err )
assert . Equalf ( t , config . result , generatedChangelog , "Testcase %s should have generated changelog" , config . testCase )
} )
}
}