Skip to content

Commit

Permalink
🚧 Add hook uninstall function
Browse files Browse the repository at this point in the history
Add function to delete the hook. This checks for the file existing as
well as determining if the ownership marker is in the first line.
  • Loading branch information
mikelorant committed Feb 17, 2023
1 parent bb47733 commit f105611
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Hook struct {
Action Action
Creator Creator
Locater Locater
Deleter Deleter
Opener Opener
Runner Runner
Stater Stater
Expand All @@ -31,6 +32,7 @@ type Options struct {

type (
Creator func(name string, flag int, perm os.FileMode) (*os.File, error)
Deleter func(string) error
Opener func(string) (*os.File, error)
Locater func(run Runner) (string, error)
Runner func(io.Writer, string, []string) error
Expand Down Expand Up @@ -66,6 +68,7 @@ func New(opts Options) Hook {
return Hook{
Action: action(opts),
Creator: os.OpenFile,
Deleter: os.Remove,
Opener: os.Open,
Locater: Locate,
Runner: shell.Run,
Expand Down
47 changes: 47 additions & 0 deletions internal/hook/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package hook

import (
"fmt"
"path"
)

func (h *Hook) Uninstall() error {
loc, err := h.Locater(h.Runner)
if err != nil {
return fmt.Errorf("unable to determine hook location: %w", err)
}

if loc == "" {
return ErrLocation
}

h.Location = path.Join(loc, GitHook)

manage, err := h.unmanage()
if err != nil {
return fmt.Errorf("unable to determine managed state: %w", err)
}

if !manage {
return ErrUnmanaged
}

return nil
}

func (h *Hook) unmanage() (bool, error) {
managed, err := h.isManaged()
if err != nil {
return false, err
}

if !managed {
return false, nil
}

if err := h.Deleter(h.Location); err != nil {
return false, fmt.Errorf("unable to delete file: %w", err)
}

return true, nil
}
135 changes: 135 additions & 0 deletions internal/hook/uninstall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package hook_test

import (
"path"
"testing"

"github.com/mikelorant/committed/internal/hook"
"github.com/stretchr/testify/assert"
)

type MockDelete struct {
delFile string
err error
}

func (d *MockDelete) Delete() func(string) error {
return func(file string) error {
d.delFile = file

if d.err != nil {
return d.err
}

return nil
}
}

func TestUninstall(t *testing.T) {
type args struct {
data string
emptyLoc bool
openErr error
locErr error
delErr error
runErr error
}

type want struct {
delFile string
err string
}

tests := []struct {
name string
args args
want want
}{
{
name: "default",
want: want{
delFile: "prepare-commit-msg",
},
},
{
name: "unmanaged",
args: args{
data: "unmanaged",
},
want: want{
err: "hook file unmanaged",
},
},
{
name: "managed",
args: args{
data: hook.Marker,
},
want: want{
delFile: "prepare-commit-msg",
},
},
{
name: "no_location",
args: args{
emptyLoc: true,
},
want: want{
err: "no hook location found",
},
},
{
name: "locate_error",
args: args{
locErr: errMock,
},
want: want{
err: "unable to determine hook location: error",
},
},
{
name: "delete_error",
args: args{
delErr: errMock,
},
want: want{
err: "unable to determine managed state: unable to delete file: error",
},
},
{
name: "open_error",
args: args{
openErr: errMock,
data: hook.Marker,
},
want: want{
err: "unable to determine managed state: unable to open file: error",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
del := MockDelete{
err: tt.args.delErr,
}

h := hook.Hook{
Deleter: del.Delete(),
Locater: MockLocater(t, tt.args.emptyLoc, tt.args.data, tt.args.locErr),
Opener: MockOpen(tt.args.openErr),
Runner: MockRun(tt.args.data, tt.args.runErr),
Stater: MockStat(),
}

err := h.Uninstall()
if tt.want.err != "" {
assert.Error(t, err)
assert.ErrorContains(t, err, tt.want.err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want.delFile, path.Base(del.delFile))
})
}
}

0 comments on commit f105611

Please sign in to comment.