Skip to content

Commit

Permalink
🚧 Create config file
Browse files Browse the repository at this point in the history
A new config file will be created if one is not found. This is a
preliminary step before being able to change configuration options
within the interface.
  • Loading branch information
mikelorant committed Feb 2, 2023
1 parent d454f5c commit 7caccce
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 16 deletions.
23 changes: 23 additions & 0 deletions internal/commit/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ type Commit struct {
Configer Configer
Opener Opener
Repoer Repoer
Creator Creator
}

type (
Applier func(repository.Commit, ...func(c *repository.Commit)) error
Emojier func(...func(*emoji.Set)) *emoji.Set
Opener func(string) (io.Reader, error)
Creator func(string) (io.WriteCloser, error)
)

type Repoer interface {
Expand All @@ -31,6 +33,7 @@ type Repoer interface {

type Configer interface {
Load(io.Reader) (config.Config, error)
Save(io.WriteCloser, config.Config) error
}

type Options struct {
Expand All @@ -55,6 +58,7 @@ func New() Commit {
Repoer: repository.New(),
Configer: new(config.Config),
Opener: FileOpen(),
Creator: FileCreate(),
}
}

Expand All @@ -71,6 +75,12 @@ func (c *Commit) Configure(opts Options) (*State, error) {
return nil, fmt.Errorf("unable to get config: %w", err)
}

if !FileExists(opts.ConfigFile) {
if err := setConfig(c.Creator, c.Configer, opts.ConfigFile, cfg); err != nil {
return nil, fmt.Errorf("unable to set config: %w", err)
}
}

return &State{
Placeholders: placeholders(),
Emojis: getEmojis(c.Emojier, cfg),
Expand Down Expand Up @@ -135,6 +145,19 @@ func getConfig(open Opener, configer Configer, file string) (config.Config, erro
return cfg, nil
}

func setConfig(create Creator, configer Configer, file string, cfg config.Config) error {
w, err := create(file)
if err != nil {
return fmt.Errorf("unable to create config: %w", err)
}

if err := configer.Save(w, cfg); err != nil {
return fmt.Errorf("unable to save config: %w", err)
}

return nil
}

func getEmojis(emojier Emojier, cfg config.Config) *emoji.Set {
prof := EmojiConfigToEmojiProfile(cfg.View.EmojiSet)
fn := emoji.WithEmojiSet(prof)
Expand Down
150 changes: 134 additions & 16 deletions internal/commit/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,29 @@ func (r *MockRepository) Apply(c repository.Commit, opts ...func(c *repository.C
}

type MockConfig struct {
err error
cfg config.Config
file config.Config

loadErr error
saveErr error
}

func (c *MockConfig) Load(fh io.Reader) (config.Config, error) {
return config.Config{}, c.err
return c.cfg, c.loadErr
}

func MockNewRepository(err error) func() (*repository.Repository, error) {
return func() (*repository.Repository, error) {
return nil, err
}
func (c *MockConfig) Save(fh io.WriteCloser, cfg config.Config) error {
c.file = cfg

return c.saveErr
}

type DiscardCloser struct {
io.Writer
}

func (d DiscardCloser) Close() error {
return nil
}

func MockNewEmoji(opts ...func(*emoji.Set)) *emoji.Set {
Expand All @@ -68,20 +80,34 @@ func MockOpen(err error) func(string) (io.Reader, error) {
}
}

func MockCreate(err error) func(file string) (io.WriteCloser, error) {
return func(file string) (io.WriteCloser, error) {
if err != nil {
return DiscardCloser{}, err
}

return DiscardCloser{}, nil
}
}

var errMock = errors.New("error")

func TestConfigure(t *testing.T) {
type args struct {
opts commit.Options
cfg config.Config
repoOpenErr error
repoDescErr error
configErr error
openErr error
createErr error
loadErr error
saveErr error
}

type want struct {
state commit.State
cfg config.Config
err string
}

Expand Down Expand Up @@ -118,6 +144,50 @@ func TestConfigure(t *testing.T) {
},
},
},
{
name: "dryrun",
args: args{
opts: commit.Options{
Amend: true,
},
},
want: want{
state: commit.State{
Placeholders: testPlaceholders(),
Config: config.Config{},
Emojis: &emoji.Set{},
Options: commit.Options{
Amend: true,
},
},
},
},
{
name: "save",
args: args{
cfg: config.Config{
View: config.View{
Focus: config.FocusAuthor,
},
},
},
want: want{
state: commit.State{
Placeholders: testPlaceholders(),
Config: config.Config{
View: config.View{
Focus: config.FocusAuthor,
},
},
Emojis: &emoji.Set{},
},
cfg: config.Config{
View: config.View{
Focus: config.FocusAuthor,
},
},
},
},
{
name: "config_file",
args: args{
Expand Down Expand Up @@ -175,12 +245,32 @@ func TestConfigure(t *testing.T) {
err: "unable to get config: unable to load config file: error",
},
},
{
name: "create_error",
args: args{
createErr: errMock,
},
want: want{
err: "unable to set config: unable to create config: error",
},
},
{
name: "save_error",
args: args{
saveErr: errMock,
},
want: want{
err: "unable to set config: unable to save config: error",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := MockConfig{
err: tt.args.configErr,
cfg: tt.args.cfg,
loadErr: tt.args.configErr,
saveErr: tt.args.saveErr,
}

repo := MockRepository{
Expand All @@ -192,6 +282,7 @@ func TestConfigure(t *testing.T) {
Repoer: &repo,
Configer: &cfg,
Emojier: MockNewEmoji,
Creator: MockCreate(tt.args.createErr),
Opener: MockOpen(tt.args.openErr),
}

Expand All @@ -203,21 +294,25 @@ func TestConfigure(t *testing.T) {
}
assert.Nil(t, err)
assert.Equal(t, &tt.want.state, state)
assert.Equal(t, tt.want.cfg, cfg.file)
})
}
}

func TestApply(t *testing.T) {
type args struct {
apply bool
emoji string
summary string
body string
footer string
author repository.User
amend bool
options commit.Options
applyErr error
apply bool
emoji string
summary string
body string
footer string
author repository.User
amend bool
options commit.Options
createErr error
saveErr error
applyErr error
nilReq bool
}

type want struct {
Expand Down Expand Up @@ -291,6 +386,24 @@ func TestApply(t *testing.T) {
dryRun: true,
},
},
{
name: "save",
args: args{
apply: false,
},
},
{
name: "skip_apply",
args: args{
apply: false,
},
},
{
name: "no_request",
args: args{
nilReq: true,
},
},
{
name: "invalid",
args: args{
Expand Down Expand Up @@ -319,9 +432,14 @@ func TestApply(t *testing.T) {
Amend: tt.args.amend,
}

if tt.args.nilReq {
req = nil
}

c := commit.Commit{
Options: tt.args.options,
Repoer: &repo,
Creator: MockCreate(tt.args.createErr),
}

err := c.Apply(req)
Expand Down
10 changes: 10 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ func (c *Config) Load(fh io.Reader) (Config, error) {

return cfg, nil
}

func (c *Config) Save(fh io.WriteCloser, cfg Config) error {
err := yaml.NewEncoder(fh).Encode(&cfg)
if err != nil {
return fmt.Errorf("unable to encode config: %w", err)
}
defer fh.Close()

return nil
}

0 comments on commit 7caccce

Please sign in to comment.