Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.1.0 #8

Merged
merged 6 commits into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ clean:
rm -rf ./dist/
rm -rf ./git-chglog
rm -rf $(GOPATH)/bin/git-chglog
rm -rf cover.out

.PHONY: bulid
build:
Expand All @@ -20,10 +21,12 @@ build:
test:
go test -v `go list ./... | grep -v /vendor/`

.PHONY: coverage
coverage:
goverage -coverprofile=cover.out `go list ./... | grep -v /vendor/`
go tool cover -func=cover.out
@rm -rf cover.out

.PHONY: install
install:
go install ./cmd/git-chglog

.PHONY: chglog
chglog:
git-chglog -c ./.chglog/config.yml
6 changes: 6 additions & 0 deletions cmd/git-chglog/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

// Builder ...
type Builder interface {
Build(*Answer) (string, error)
}
5 changes: 2 additions & 3 deletions cmd/git-chglog/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// CLI ...
type CLI struct {
ctx *Context
ctx *CLIContext
fs FileSystem
logger *Logger
configLoader ConfigLoader
Expand All @@ -22,8 +22,7 @@ type CLI struct {

// NewCLI ...
func NewCLI(
ctx *Context,
fs FileSystem,
ctx *CLIContext, fs FileSystem,
configLoader ConfigLoader,
generator Generator,
) *CLI {
Expand Down
4 changes: 2 additions & 2 deletions cmd/git-chglog/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCLIForStdout(t *testing.T) {
}

c := NewCLI(
&Context{
&CLIContext{
WorkingDir: "/",
ConfigPath: "/.chglog/config.yml",
OutputPath: "",
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestCLIForFile(t *testing.T) {
}

c := NewCLI(
&Context{
&CLIContext{
WorkingDir: "/",
ConfigPath: "/.chglog/config.yml",
OutputPath: "/dir/to/CHANGELOG.tpl",
Expand Down
4 changes: 2 additions & 2 deletions cmd/git-chglog/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Config struct {
}

// Normalize ...
func (config *Config) Normalize(ctx *Context) error {
func (config *Config) Normalize(ctx *CLIContext) error {
err := mergo.Merge(config, &Config{
Bin: "git",
Template: "CHANGELOG.tpl.md",
Expand Down Expand Up @@ -148,7 +148,7 @@ func (config *Config) normalizeStyleOfGitHub() {
}

// Convert ...
func (config *Config) Convert(ctx *Context) *chglog.Config {
func (config *Config) Convert(ctx *CLIContext) *chglog.Config {
info := config.Info
opts := config.Options

Expand Down
73 changes: 73 additions & 0 deletions cmd/git-chglog/config_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"strings"
)

// ConfigBuilder ...
type ConfigBuilder interface {
Builder
}

type configBuilderImpl struct{}

// NewConfigBuilder ...
func NewConfigBuilder() ConfigBuilder {
return &configBuilderImpl{}
}

// Build ...
func (*configBuilderImpl) Build(ans *Answer) (string, error) {
var msgFormat *CommitMessageFormat

for _, f := range formats {
if f.Display == ans.CommitMessageFormat {
msgFormat = f
break
}
}

if msgFormat == nil {
return "", fmt.Errorf("\"%s\" is an invalid commit message format", ans.CommitMessageFormat)
}

repoURL := strings.TrimRight(ans.RepositoryURL, "/")
if repoURL == "" {
repoURL = "\"\""
}

config := fmt.Sprintf(`style: %s
template: %s
info:
title: CHANGELOG
repository_url: %s
options:
commits:
# filters:
# Type:
# - feat
# - fix
# - perf
# - refactor
commit_groups:
# title_maps:
# feat: Features
# fix: Bug Fixes
# perf: Performance Improvements
# refactor: Code Refactoring
header:
pattern: "%s"
pattern_maps:%s
notes:
keywords:
- BREAKING CHANGE`,
ans.Style,
defaultTemplateFilename,
repoURL,
msgFormat.Pattern,
msgFormat.PatternMapString(),
)

return config, nil
}
9 changes: 9 additions & 0 deletions cmd/git-chglog/config_builder_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

type mockConfigBuilderImpl struct {
ReturnBuild func(*Answer) (string, error)
}

func (m *mockConfigBuilderImpl) Build(ans *Answer) (string, error) {
return m.ReturnBuild(ans)
}
62 changes: 62 additions & 0 deletions cmd/git-chglog/config_builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfigBulider(t *testing.T) {
assert := assert.New(t)
builder := NewConfigBuilder()

out, err := builder.Build(&Answer{
RepositoryURL: "https://github.com/git-chglog/git-chglog/git-chglog/",
Style: styleNone,
CommitMessageFormat: fmtGitBasic.Display,
Template: tplStandard,
})

assert.Nil(err)
assert.Contains(out, "style: none")
assert.Contains(out, "template: CHANGELOG.tpl.md")
assert.Contains(out, " repository_url: https://github.com/git-chglog/git-chglog/git-chglog")
assert.Contains(out, fmt.Sprintf(" pattern: \"%s\"", fmtGitBasic.Pattern))
assert.Contains(out, fmt.Sprintf(
` pattern_maps:
- %s
- %s`,
fmtGitBasic.PatternMaps[0],
fmtGitBasic.PatternMaps[1],
))
}

func TestConfigBuliderEmptyRepoURL(t *testing.T) {
assert := assert.New(t)
builder := NewConfigBuilder()

out, err := builder.Build(&Answer{
RepositoryURL: "",
Style: styleNone,
CommitMessageFormat: fmtGitBasic.Display,
Template: tplStandard,
})

assert.Nil(err)
assert.Contains(out, " repository_url: \"\"")
}

func TestConfigBuliderInvalidFormat(t *testing.T) {
assert := assert.New(t)
builder := NewConfigBuilder()

_, err := builder.Build(&Answer{
RepositoryURL: "",
Style: styleNone,
CommitMessageFormat: "",
Template: tplStandard,
})

assert.Contains(err.Error(), "invalid commit message format")
}
4 changes: 2 additions & 2 deletions cmd/git-chglog/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestConfigNormalize(t *testing.T) {
},
}

err := config.Normalize(&Context{
err := config.Normalize(&CLIContext{
ConfigPath: "/test/config.yml",
})

Expand All @@ -30,7 +30,7 @@ func TestConfigNormalize(t *testing.T) {
Template: "/CHANGELOG.tpl.md",
}

err = config.Normalize(&Context{
err = config.Normalize(&CLIContext{
ConfigPath: "/test/config.yml",
})

Expand Down
11 changes: 9 additions & 2 deletions cmd/git-chglog/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"io"
)

// Context ...
type Context struct {
// CLIContext ...
type CLIContext struct {
WorkingDir string
Stdout io.Writer
Stderr io.Writer
Expand All @@ -16,3 +16,10 @@ type Context struct {
NoEmoji bool
Query string
}

// InitContext ...
type InitContext struct {
WorkingDir string
Stdout io.Writer
Stderr io.Writer
}
12 changes: 12 additions & 0 deletions cmd/git-chglog/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package main

import (
"io"
"io/ioutil"
"os"
)

// FileSystem ...
type FileSystem interface {
Exists(path string) bool
MkdirP(path string) error
Create(name string) (File, error)
WriteFile(path string, content []byte) error
}

// File ...
Expand All @@ -25,6 +28,11 @@ var fs = &osFileSystem{}

type osFileSystem struct{}

func (*osFileSystem) Exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}

func (*osFileSystem) MkdirP(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.MkdirAll(path, os.ModePerm)
Expand All @@ -35,3 +43,7 @@ func (*osFileSystem) MkdirP(path string) error {
func (*osFileSystem) Create(name string) (File, error) {
return os.Create(name)
}

func (*osFileSystem) WriteFile(path string, content []byte) error {
return ioutil.WriteFile(path, content, os.ModePerm)
}
14 changes: 12 additions & 2 deletions cmd/git-chglog/fs_mock.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package main

type mockFileSystem struct {
ReturnMkdirP func(string) error
ReturnCreate func(string) (File, error)
ReturnExists func(string) bool
ReturnMkdirP func(string) error
ReturnCreate func(string) (File, error)
ReturnWriteFile func(string, []byte) error
}

func (m *mockFileSystem) Exists(path string) bool {
return m.ReturnExists(path)
}

func (m *mockFileSystem) MkdirP(path string) error {
Expand All @@ -13,6 +19,10 @@ func (m *mockFileSystem) Create(name string) (File, error) {
return m.ReturnCreate(name)
}

func (m *mockFileSystem) WriteFile(path string, content []byte) error {
return m.ReturnWriteFile(path, content)
}

type mockFile struct {
File
ReturnWrite func([]byte) (int, error)
Expand Down
Loading