-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a package that will handle creation, removal and commiting when a Git prepare message hook exists.
- Loading branch information
1 parent
348b3e2
commit cee22d8
Showing
2 changed files
with
154 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,58 @@ | ||
package hook | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
type Hook struct { | ||
Action Action | ||
} | ||
|
||
type Options struct { | ||
Install bool | ||
Uninstall bool | ||
Commit bool | ||
} | ||
|
||
type ( | ||
Action int | ||
) | ||
|
||
var ErrAction = errors.New("invalid hook action") | ||
|
||
const ( | ||
ActionUnset Action = iota | ||
ActionInstall | ||
ActionUninstall | ||
ActionCommit | ||
) | ||
|
||
func New(opts Options) Hook { | ||
return Hook{ | ||
Action: action(opts), | ||
} | ||
} | ||
|
||
func (h *Hook) Do() error { | ||
switch h.Action { | ||
case ActionInstall: | ||
return nil | ||
case ActionUninstall: | ||
return nil | ||
} | ||
|
||
return ErrAction | ||
} | ||
|
||
func action(opts Options) Action { | ||
switch { | ||
case opts.Install: | ||
return ActionInstall | ||
case opts.Uninstall: | ||
return ActionUninstall | ||
case opts.Commit: | ||
return ActionCommit | ||
default: | ||
return ActionUnset | ||
} | ||
} |
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,96 @@ | ||
package hook_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mikelorant/committed/internal/hook" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
input hook.Options | ||
output hook.Hook | ||
}{ | ||
{ | ||
name: "empty", | ||
output: hook.Hook{}, | ||
}, | ||
{ | ||
name: "install", | ||
input: hook.Options{Install: true}, | ||
output: hook.Hook{Action: hook.ActionInstall}, | ||
}, | ||
{ | ||
name: "uninstall", | ||
input: hook.Options{Uninstall: true}, | ||
output: hook.Hook{Action: hook.ActionUninstall}, | ||
}, | ||
{ | ||
name: "commit", | ||
input: hook.Options{Commit: true}, | ||
output: hook.Hook{Action: hook.ActionCommit}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := hook.New(tt.input) | ||
|
||
assert.Equal(t, tt.output, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestDo(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
input hook.Hook | ||
err error | ||
}{ | ||
{ | ||
name: "empty", | ||
input: hook.Hook{}, | ||
err: hook.ErrAction, | ||
}, | ||
{ | ||
name: "install", | ||
input: hook.Hook{Action: hook.ActionInstall}, | ||
}, | ||
{ | ||
name: "uninstall", | ||
input: hook.Hook{Action: hook.ActionUninstall}, | ||
}, | ||
{ | ||
name: "commit", | ||
input: hook.Hook{Action: hook.ActionCommit}, | ||
err: hook.ErrAction, | ||
}, | ||
{ | ||
name: "nil", | ||
input: hook.Hook{}, | ||
err: hook.ErrAction, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := tt.input.Do() | ||
|
||
assert.ErrorIs(t, err, tt.err) | ||
}) | ||
} | ||
} |