-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(semantic-release): add tests for WriteChangelog
- Loading branch information
Felix Wiedmann
committed
May 8, 2021
1 parent
749bbf7
commit 7b93801
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package semanticrelease | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Nightapes/go-semantic-release/pkg/config" | ||
) | ||
|
||
func TestSemanticRelease_WriteChangeLog(t *testing.T) { | ||
|
||
type args struct { | ||
changelogContent string | ||
file string | ||
overwrite bool | ||
maxChangelogFileSize int64 | ||
} | ||
tests := []struct { | ||
config *config.ReleaseConfig | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "MoveExisting", | ||
args: args{ | ||
changelogContent: "go-semantic-release-rocks!", | ||
file: "test1.changelog.md", | ||
overwrite: false, | ||
maxChangelogFileSize: 0, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "ValidWithOverwrite", | ||
args: args{ | ||
changelogContent: "go-semantic-release-rocks!", | ||
file: "test2.changelog.md", | ||
overwrite: true, | ||
maxChangelogFileSize: 0, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := os.Create(tt.args.file) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
releaser := &SemanticRelease{} | ||
if err := releaser.WriteChangeLog(tt.args.changelogContent, tt.args.file, tt.args.overwrite, tt.args.maxChangelogFileSize); (err != nil) != tt.wantErr { | ||
t.Errorf("WriteChangeLog() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
name := strings.Join(strings.Split(tt.args.file, ".")[:len(strings.Split(tt.args.file, "."))-1], ".") | ||
|
||
files, err := filepath.Glob("./" + name + "*") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if !tt.wantErr && !tt.args.overwrite && tt.args.maxChangelogFileSize == 0 && len(files) <= 1 { | ||
t.Errorf("WriteChangelog() = should create a copy of the existing changelog file") | ||
} | ||
|
||
if !tt.wantErr && tt.args.overwrite && len(files) > 1 { | ||
t.Errorf("WriteChangelog() = should not create a copy of the changelog file") | ||
} | ||
|
||
for _, i := range files { | ||
err := os.Remove(i) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
}) | ||
} | ||
} |