-
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.
Write hook message to file that can be read by Git.
- Loading branch information
1 parent
f105611
commit b7de666
Showing
3 changed files
with
202 additions
and
21 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 |
---|---|---|
|
@@ -3,6 +3,8 @@ package repository_test | |
import ( | ||
"errors" | ||
"io" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/mikelorant/committed/internal/repository" | ||
|
@@ -29,21 +31,52 @@ func (r *MockShell) Run() func(w io.Writer, command string, args []string) error | |
} | ||
} | ||
|
||
type MockOpenFile struct { | ||
filename string | ||
mockFilename string | ||
err error | ||
close bool | ||
} | ||
|
||
func (o *MockOpenFile) OpenFile() func(string, int, os.FileMode) (*os.File, error) { | ||
return func(filename string, flag int, perm os.FileMode) (*os.File, error) { | ||
if o.err != nil { | ||
return nil, o.err | ||
} | ||
|
||
fh, _ := os.CreateTemp("", "") | ||
|
||
o.filename = filename | ||
o.mockFilename = fh.Name() | ||
|
||
if o.close { | ||
fh.Close() | ||
} | ||
|
||
return fh, nil | ||
} | ||
} | ||
|
||
var errMock = errors.New("error") | ||
|
||
func TestApply(t *testing.T) { | ||
t.Parallel() | ||
|
||
type args struct { | ||
commit repository.Commit | ||
opts []func(c *repository.Commit) | ||
err error | ||
commit repository.Commit | ||
opts []func(c *repository.Commit) | ||
filename string | ||
runErr error | ||
openFileErr error | ||
close bool | ||
} | ||
|
||
type want struct { | ||
cmd string | ||
args []string | ||
err error | ||
cmd string | ||
args []string | ||
data string | ||
mockFilename string | ||
err string | ||
} | ||
|
||
tests := []struct { | ||
|
@@ -149,12 +182,100 @@ func TestApply(t *testing.T) { | |
}, | ||
}, | ||
{ | ||
name: "error", | ||
name: "hook", | ||
args: args{ | ||
commit: repository.Commit{ | ||
Hook: true, | ||
MessageFile: "test", | ||
}, | ||
}, | ||
want: want{ | ||
mockFilename: "test", | ||
}, | ||
}, | ||
{ | ||
name: "hook_summary", | ||
args: args{ | ||
commit: repository.Commit{ | ||
Subject: "summary", | ||
Hook: true, | ||
}, | ||
}, | ||
want: want{ | ||
data: "summary", | ||
}, | ||
}, | ||
{ | ||
name: "hook_summary_body", | ||
args: args{ | ||
commit: repository.Commit{ | ||
Subject: "summary", | ||
Body: "body", | ||
Hook: true, | ||
}, | ||
}, | ||
want: want{ | ||
data: "summary\n\nbody", | ||
}, | ||
}, | ||
{ | ||
name: "hook_summary_footer", | ||
args: args{ | ||
commit: repository.Commit{ | ||
Subject: "summary", | ||
Footer: "Signed-off-by: John Doe <[email protected]>", | ||
Hook: true, | ||
}, | ||
}, | ||
want: want{ | ||
data: "summary\n\nSigned-off-by: John Doe <[email protected]>", | ||
}, | ||
}, | ||
{ | ||
name: "hook_summary_body_footer", | ||
args: args{ | ||
commit: repository.Commit{ | ||
Subject: "summary", | ||
Body: "body", | ||
Footer: "Signed-off-by: John Doe <[email protected]>", | ||
Hook: true, | ||
}, | ||
}, | ||
want: want{ | ||
data: "summary\n\nbody\n\nSigned-off-by: John Doe <[email protected]>", | ||
}, | ||
}, | ||
{ | ||
name: "run_error", | ||
args: args{ | ||
runErr: errMock, | ||
}, | ||
want: want{ | ||
err: "unable to run command: error", | ||
}, | ||
}, | ||
{ | ||
name: "hook_error", | ||
args: args{ | ||
commit: repository.Commit{ | ||
Hook: true, | ||
}, | ||
openFileErr: errMock, | ||
}, | ||
want: want{ | ||
err: "unble to open file: error", | ||
}, | ||
}, | ||
{ | ||
name: "hook_error_close", | ||
args: args{ | ||
err: errMock, | ||
commit: repository.Commit{ | ||
Hook: true, | ||
}, | ||
close: true, | ||
}, | ||
want: want{ | ||
err: errMock, | ||
err: "unable to write file: unable to close file: close", | ||
}, | ||
}, | ||
} | ||
|
@@ -165,23 +286,37 @@ func TestApply(t *testing.T) { | |
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var repo repository.Repository | ||
|
||
shell := MockShell{ | ||
err: tt.want.err, | ||
err: tt.args.runErr, | ||
} | ||
|
||
repo.Runner = shell.Run() | ||
openFile := MockOpenFile{ | ||
filename: tt.args.filename, | ||
err: tt.args.openFileErr, | ||
close: tt.args.close, | ||
} | ||
|
||
repo := repository.Repository{ | ||
Runner: shell.Run(), | ||
OpenFiler: openFile.OpenFile(), | ||
} | ||
|
||
err := repo.Apply(tt.args.commit) | ||
if tt.want.err != nil { | ||
if tt.want.err != "" { | ||
assert.NotNil(t, err) | ||
assert.ErrorContains(t, err, tt.want.err.Error()) | ||
assert.ErrorContains(t, err, tt.want.err) | ||
return | ||
} | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, tt.want.cmd, shell.command) | ||
assert.Equal(t, tt.want.args, shell.args) | ||
|
||
if tt.args.commit.Hook { | ||
out, _ := os.ReadFile(openFile.mockFilename) | ||
assert.Equal(t, tt.want.mockFilename, openFile.filename) | ||
assert.Equal(t, tt.want.data, strings.TrimSpace(string(out))) | ||
} | ||
}) | ||
} | ||
} |
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