|
| 1 | +package gitcommithook |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/pkg/errors" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +const commitMessageFile = ".git/COMMIT_MSG_EDIT" |
| 12 | +const commitMessage = "commitMessage" |
| 13 | +const modifiedCommitMessage = "modified commitMessage" |
| 14 | + |
| 15 | +var rewriteOriginals = struct { |
| 16 | + readFileFunc readFileFuncDef |
| 17 | + modifyFunc modifyFuncDef |
| 18 | + writeFileFunc writeFileFuncDef |
| 19 | +}{ |
| 20 | + readFileFunc: readFileFunc, |
| 21 | + modifyFunc: modifyFunc, |
| 22 | + writeFileFunc: writeFileFunc, |
| 23 | +} |
| 24 | + |
| 25 | +func restoreRewriteOriginals() { |
| 26 | + readFileFunc = rewriteOriginals.readFileFunc |
| 27 | + modifyFunc = rewriteOriginals.modifyFunc |
| 28 | + writeFileFunc = rewriteOriginals.writeFileFunc |
| 29 | +} |
| 30 | + |
| 31 | +func TestRewriteCommitMessage_ErrorCase_CannotFindCommitMessageFile(t *testing.T) { |
| 32 | + defer restoreRewriteOriginals() |
| 33 | + |
| 34 | + commitMessageFileCannotBeFound(t) |
| 35 | + |
| 36 | + err := RewriteCommitMessage(commitMessageFile) |
| 37 | + |
| 38 | + assert.Contains(t, err.Error(), "error reading commit message") |
| 39 | +} |
| 40 | + |
| 41 | +func TestRewriteCommitMessage_ErrorCase_ErrorModifyingMessage(t *testing.T) { |
| 42 | + defer restoreRewriteOriginals() |
| 43 | + commitMessageFileCanBeRead(t) |
| 44 | + errorModifyingCommitMessage(t) |
| 45 | + |
| 46 | + err := RewriteCommitMessage(commitMessageFile) |
| 47 | + |
| 48 | + assert.Contains(t, err.Error(), "error modifying commit message") |
| 49 | +} |
| 50 | + |
| 51 | +func TestRewriteCommitMessage_ErrorCase_CannotWriteMessageToFile(t *testing.T) { |
| 52 | + defer restoreRewriteOriginals() |
| 53 | + |
| 54 | + commitMessageFileCanBeRead(t) |
| 55 | + commitMessageIsModified(t) |
| 56 | + cannotWriteToFile(t) |
| 57 | + |
| 58 | + err := RewriteCommitMessage(commitMessageFile) |
| 59 | + |
| 60 | + assert.Contains(t, err.Error(), "error writing commit message to") |
| 61 | +} |
| 62 | + |
| 63 | +func TestRewriteCommitMessage_HappyPath(t *testing.T) { |
| 64 | + defer restoreRewriteOriginals() |
| 65 | + |
| 66 | + commitMessageFileCanBeRead(t) |
| 67 | + commitMessageIsModified(t) |
| 68 | + commitMessageIsWrittenToFile(t) |
| 69 | + |
| 70 | + err := RewriteCommitMessage(commitMessageFile) |
| 71 | + |
| 72 | + assert.NoError(t, err) |
| 73 | +} |
| 74 | + |
| 75 | +func cannotWriteToFile(t *testing.T) { |
| 76 | + writeFileFunc = func(fileName string, data []byte, perm os.FileMode) error { |
| 77 | + assert.Exactly(t, commitMessageFile, fileName) |
| 78 | + assert.Exactly(t, modifiedCommitMessage, string(data)) |
| 79 | + assert.Exactly(t, perm, os.FileMode(0777)) |
| 80 | + |
| 81 | + return errors.New("cannot write to file") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func commitMessageIsWrittenToFile(t *testing.T) { |
| 86 | + writeFileFunc = func(fileName string, data []byte, perm os.FileMode) error { |
| 87 | + assert.Exactly(t, commitMessageFile, fileName) |
| 88 | + assert.Exactly(t, modifiedCommitMessage, string(data)) |
| 89 | + assert.Exactly(t, perm, os.FileMode(0777)) |
| 90 | + |
| 91 | + return nil |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func commitMessageFileCanBeRead(t *testing.T) { |
| 96 | + readFileFunc = func(fileName string) ([]byte, error) { |
| 97 | + assert.Exactly(t, commitMessageFile, fileName) |
| 98 | + return []byte(commitMessage), nil |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func commitMessageFileCannotBeFound(t *testing.T) { |
| 103 | + readFileFunc = func(fileName string) ([]byte, error) { |
| 104 | + assert.Exactly(t, commitMessageFile, fileName) |
| 105 | + return []byte{}, errors.New("Could not find file") |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func errorModifyingCommitMessage(t *testing.T) { |
| 110 | + modifyFunc = func(message string) (string, error) { |
| 111 | + assert.Exactly(t, commitMessage, message) |
| 112 | + return "", errors.New("error modifying") |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func commitMessageIsModified(t *testing.T) { |
| 117 | + modifyFunc = func(message string) (string, error) { |
| 118 | + assert.Exactly(t, commitMessage, message) |
| 119 | + return modifiedCommitMessage, nil |
| 120 | + } |
| 121 | +} |
0 commit comments