Skip to content

Commit 9a7eff1

Browse files
author
Oppodelldog
committed
make it work
1 parent a388563 commit 9a7eff1

2 files changed

Lines changed: 174 additions & 5 deletions

File tree

cmd/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ import (
88
"github.com/Oppodelldog/git-commit-hook"
99
)
1010

11+
type rewriteCommitMessageFuncDef func(string) error
12+
type exitFuncDef func(code int)
13+
14+
var rewriteCommitMessageFunc = rewriteCommitMessageFuncDef(gitcommithook.RewriteCommitMessage)
15+
var exitFunc = exitFuncDef(os.Exit)
16+
1117
func main() {
12-
commitMessage, err := gitcommithook.ModifyGitCommitMessage(os.Args[1])
18+
commitMessageFile := os.Args[1]
19+
err := rewriteCommitMessageFunc(commitMessageFile)
1320
if err != nil {
14-
fmt.Printf("error in git hook: %s", err.Error())
15-
os.Exit(1)
21+
fmt.Print(err)
22+
exitFunc(1)
23+
return
1624
}
17-
18-
fmt.Print(commitMessage)
25+
exitFunc(0)
1926
}

cmd/main_test.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"os/exec"
9+
"testing"
10+
11+
"reflect"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
const testPath = "/tmp/git-commit-hook"
17+
const featureBranch = "feature/PROJECT-123"
18+
const nonFeatureBranch = "release/v0.1.2"
19+
const commitMessageFile = ".git/COMMIT_EDITMSG"
20+
21+
//originals contains original values might be overwritten in tests
22+
var originals = struct {
23+
osArgs []string
24+
rewriteCommitMessageFunc rewriteCommitMessageFuncDef
25+
exitFunc exitFuncDef
26+
osStdout *os.File
27+
}{
28+
osArgs: os.Args,
29+
rewriteCommitMessageFunc: rewriteCommitMessageFunc,
30+
exitFunc: exitFunc,
31+
osStdout: os.Stdout,
32+
}
33+
34+
//restoreOriginals restores original values
35+
func restoreOriginals() {
36+
os.Args = originals.osArgs
37+
rewriteCommitMessageFunc = originals.rewriteCommitMessageFunc
38+
exitFunc = originals.exitFunc
39+
os.Stdout = originals.osStdout
40+
os.RemoveAll(testPath)
41+
}
42+
43+
func TestMain_HappyPath(t *testing.T) {
44+
defer restoreOriginals()
45+
46+
initGitRepositoryWithBranch(t, featureBranch)
47+
setCommitMessage(t, "initial commit")
48+
prepareGitHookCall()
49+
50+
programExistsWith(t, 0)
51+
52+
main()
53+
54+
expectedCommitMessage := fmt.Sprintf("%s: initial commit", featureBranch)
55+
modifiedCommitMessage := readCommitMessage(t)
56+
assert.Exactly(t, expectedCommitMessage, modifiedCommitMessage)
57+
}
58+
59+
func readCommitMessage(t *testing.T) string {
60+
b, err := ioutil.ReadFile(commitMessageFile)
61+
if err != nil {
62+
t.Fatalf("Did not exepect ioutil.ReadFile to return an error, but got: %v", err)
63+
}
64+
65+
return string(b)
66+
}
67+
68+
func prepareGitHookCall() {
69+
os.Args = []string{"git", commitMessageFile}
70+
}
71+
72+
func setCommitMessage(t *testing.T, commitMessage string) {
73+
err := ioutil.WriteFile(commitMessageFile, []byte(commitMessage), 0777)
74+
if err != nil {
75+
t.Fatalf("Did not exepect ioutil.WriteFile to return an error, but got: %v", err)
76+
}
77+
}
78+
79+
func TestMain_ErrorCase_CommitMessageFileNotFound(t *testing.T) {
80+
defer restoreOriginals()
81+
82+
os.Args = []string{"git", commitMessageFile}
83+
w, stdOutChannel := captureStdOut(t)
84+
85+
programExistsWith(t, 1)
86+
87+
main()
88+
89+
w.Close()
90+
91+
stdOutput := <-stdOutChannel
92+
assert.Exactly(t, "error reading commit message from '.git/COMMIT_EDITMSG': open .git/COMMIT_EDITMSG: no such file or directory", stdOutput)
93+
}
94+
95+
func TestMain_ErrorCase_GitError(t *testing.T) {
96+
defer restoreOriginals()
97+
98+
initGitRepositoryWithBranch(t, nonFeatureBranch)
99+
setCommitMessage(t, "@noissue rc-fix")
100+
os.Args = []string{"git", commitMessageFile}
101+
w, stdOutChannel := captureStdOut(t)
102+
103+
programExistsWith(t, 1)
104+
105+
main()
106+
107+
w.Close()
108+
109+
stdOutput := <-stdOutChannel
110+
assert.Exactly(t, "error modifying commit message: feature reference is required in '@noissue rc-fix'", stdOutput)
111+
}
112+
113+
func TestMain_ExitFuncUsesAppropriateOsFunc(t *testing.T) {
114+
assert.Exactly(t, reflect.ValueOf(os.Exit).Pointer(), reflect.ValueOf(exitFunc).Pointer())
115+
}
116+
117+
func programExistsWith(t *testing.T, expectedExitCode int) {
118+
exitFunc = func(exitCode int) {
119+
assert.Exactly(t, expectedExitCode, exitCode)
120+
}
121+
}
122+
123+
func captureStdOut(t *testing.T) (*os.File, chan string) {
124+
r, w, err := os.Pipe()
125+
if err != nil {
126+
t.Fatalf("Did not exepect os.Pipe to return an error, but got: %v", err)
127+
}
128+
os.Stdout = w
129+
stdOutChannel := make(chan string)
130+
go func() {
131+
defer r.Close()
132+
133+
scanner := bufio.NewScanner(r)
134+
if scanner.Scan() {
135+
stdOutChannel <- scanner.Text()
136+
}
137+
138+
close(stdOutChannel)
139+
}()
140+
return w, stdOutChannel
141+
}
142+
143+
func initGitRepositoryWithBranch(t *testing.T, branchName string) {
144+
os.RemoveAll(testPath)
145+
os.MkdirAll(testPath, 0777)
146+
os.Chdir(testPath)
147+
git(t, "init")
148+
err := ioutil.WriteFile("README.md", []byte("# test file"), 0777)
149+
if err != nil {
150+
t.Fatalf("could not write README.md: %v", err)
151+
}
152+
git(t, "add", "-A")
153+
git(t, "commit", "-m", "initial commit")
154+
git(t, "checkout", "-b", branchName)
155+
}
156+
157+
func git(t *testing.T, args ...string) {
158+
o, err := exec.Command("git", args...).CombinedOutput()
159+
if err != nil {
160+
t.Fatalf("'git %v init' failed with error: %v - output: %s", args, err, string(o))
161+
}
162+
}

0 commit comments

Comments
 (0)