-
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 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
1 parent
bb47733
commit f105611
Showing
3 changed files
with
185 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
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,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 | ||
} |
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,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)) | ||
}) | ||
} | ||
} |