-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #753 from githru/feature/675
[engine] getCommitRaws 함수 개선
- Loading branch information
Showing
5 changed files
with
314 additions
and
211 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const COMMIT_SEPARATOR = "4itc2s8hH-oA64s08h19"; | ||
export const GIT_LOG_SEPARATOR = "I9M-0XOzvHlYPegVPpzb"; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { getCommitMessageType } from "./commit.util"; | ||
import getCommitRaws from "./parser"; | ||
import { COMMIT_SEPARATOR, GIT_LOG_SEPARATOR } from "./constant"; | ||
import getCommitRaws from "./parser"; | ||
import type { CommitRaw, DifferenceStatistic } from "./types"; | ||
|
||
describe("commit message type", () => { | ||
|
@@ -34,117 +35,229 @@ describe("commit message type", () => { | |
}); | ||
}); | ||
|
||
describe('getCommitRaws', () => { | ||
const testCommitLines = [ | ||
"commit a b (HEAD)", | ||
"commit a b (HEAD -> main, origin/main, origin/HEAD)", | ||
"commit a b (HEAD, tag: v1.0.0)", | ||
"commit a b (HEAD -> main, origin/main, origin/HEAD, tag: v2.0.0)", | ||
"commit a b (HEAD, tag: v2.0.0, tag: v1.4)" | ||
]; | ||
describe("getCommitRaws", () => { | ||
const fakeAuthorAndCommitter = `${GIT_LOG_SEPARATOR}John Park${GIT_LOG_SEPARATOR}[email protected]${GIT_LOG_SEPARATOR}Sun Sep 4 20:17:59 2022 +0900${GIT_LOG_SEPARATOR}John Park 2${GIT_LOG_SEPARATOR}[email protected]${GIT_LOG_SEPARATOR}Sun Sep 5 20:17:59 2022 +0900`; | ||
const fakeCommitMessage = `${GIT_LOG_SEPARATOR}commit message${GIT_LOG_SEPARATOR}`; | ||
const fakeCommitMessageAndBody = `${GIT_LOG_SEPARATOR}commit message title\n\ncommit message body${GIT_LOG_SEPARATOR}`; | ||
const fakeCommitHash = `a${GIT_LOG_SEPARATOR}b`; | ||
const fakeCommitRef = `${GIT_LOG_SEPARATOR}HEAD`; | ||
const fakeCommitFileChange = "10\t0\ta.ts\n1\t0\tREADME.md"; | ||
|
||
const expectedBranches = [ | ||
['HEAD'], | ||
['HEAD', 'main', 'origin/main', 'origin/HEAD'], | ||
['HEAD'], | ||
['HEAD', 'main', 'origin/main', 'origin/HEAD'], | ||
['HEAD'] | ||
]; | ||
|
||
const expectedTags = [ | ||
[], | ||
[], | ||
['v1.0.0'], | ||
['v2.0.0'], | ||
['v2.0.0', 'v1.4'] | ||
]; | ||
|
||
const testCommitFileChanges = [ | ||
"10\t0\ta.ts\n1\t0\tREADME.md", | ||
"3\t3\ta.ts", | ||
"4\t0\ta.ts", | ||
"0\t6\ta.ts\n2\t0\tb.ts\n3\t3\tc.ts" | ||
]; | ||
|
||
const expectedFileChanged:DifferenceStatistic[] = [ | ||
{ | ||
totalInsertionCount: 11, | ||
totalDeletionCount: 0, | ||
fileDictionary: { | ||
'a.ts': { insertionCount: 10, deletionCount: 0 }, | ||
'README.md': { insertionCount: 1, deletionCount: 0 }, | ||
} | ||
}, | ||
{ | ||
totalInsertionCount: 3, | ||
totalDeletionCount: 3, | ||
fileDictionary: { 'a.ts': { insertionCount: 3, deletionCount: 3 } } | ||
}, | ||
{ | ||
totalInsertionCount: 4, | ||
totalDeletionCount: 0, | ||
fileDictionary: { 'a.ts': { insertionCount: 4, deletionCount: 0 } } | ||
}, | ||
{ | ||
totalInsertionCount: 5, | ||
totalDeletionCount: 9, | ||
fileDictionary: { | ||
'a.ts': { insertionCount: 0, deletionCount: 6 }, | ||
'b.ts': { insertionCount: 2, deletionCount: 0 }, | ||
'c.ts': { insertionCount: 3, deletionCount: 3 }, | ||
} | ||
} | ||
]; | ||
|
||
const commonExpectatedResult: CommitRaw={ | ||
const commonExpectatedResult: CommitRaw = { | ||
sequence: 0, | ||
id: 'a', | ||
parents: ['b'], | ||
branches: ['HEAD'], | ||
id: "a", | ||
parents: ["b"], | ||
branches: ["HEAD"], | ||
tags: [], | ||
author: { name: 'John Park', email: '[email protected]' }, | ||
authorDate: new Date('Sun Sep 4 20:17:59 2022 +0900'), | ||
committer: { name: 'John Park', email: 'mail@gmail.com' }, | ||
committerDate: new Date('Sun Sep 4 20:17:59 2022 +0900'), | ||
message: 'commit message', | ||
author: { name: "John Park", email: "[email protected]" }, | ||
authorDate: new Date("Sun Sep 4 20:17:59 2022 +0900"), | ||
committer: { name: "John Park 2", email: "mail2@gmail.com" }, | ||
committerDate: new Date("Sun Sep 5 20:17:59 2022 +0900"), | ||
message: "commit message", | ||
differenceStatistic: { | ||
totalInsertionCount: 0, | ||
totalDeletionCount: 0, | ||
fileDictionary: {}, | ||
}, | ||
commitMessageType: "" | ||
commitMessageType: "", | ||
}; | ||
const expectedCommitMessageBody = "commit message title\n\ncommit message body"; | ||
const expectedFileChange: DifferenceStatistic = { | ||
totalInsertionCount: 11, | ||
totalDeletionCount: 0, | ||
fileDictionary: { | ||
"a.ts": { insertionCount: 10, deletionCount: 0 }, | ||
"README.md": { insertionCount: 1, deletionCount: 0 }, | ||
}, | ||
}; | ||
|
||
it.each([ | ||
[ | ||
`${COMMIT_SEPARATOR}${`a${GIT_LOG_SEPARATOR}`}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
id: "a", | ||
parents: [""], | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${`c${GIT_LOG_SEPARATOR}b`}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
id: "c", | ||
parents: ["b"], | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${`d${GIT_LOG_SEPARATOR}e f`}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
id: "d", | ||
parents: ["e", "f"], | ||
}, | ||
], | ||
])("should parse gitlog to commitRaw(hash)", (mockLog, expectedResult) => { | ||
const result = getCommitRaws(mockLog); | ||
expect(result).toEqual([expectedResult]); | ||
}); | ||
|
||
it.each([ | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${`${GIT_LOG_SEPARATOR}HEAD`}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
branches: ["HEAD"], | ||
tags: [], | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${`${GIT_LOG_SEPARATOR}HEAD -> main, origin/main, origin/HEAD`}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
branches: ["HEAD", "main", "origin/main", "origin/HEAD"], | ||
tags: [], | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${`${GIT_LOG_SEPARATOR}HEAD, tag: v1.0.0`}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
branches: ["HEAD"], | ||
tags: ["v1.0.0"], | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${`${GIT_LOG_SEPARATOR}HEAD -> main, origin/main, origin/HEAD, tag: v2.0.0`}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
branches: ["HEAD", "main", "origin/main", "origin/HEAD"], | ||
tags: ["v2.0.0"], | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${`${GIT_LOG_SEPARATOR}HEAD, tag: v2.0.0, tag: v1.4`}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
branches: ["HEAD"], | ||
tags: ["v2.0.0", "v1.4"], | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${GIT_LOG_SEPARATOR}${fakeAuthorAndCommitter}${fakeCommitMessage}`, | ||
{ | ||
...commonExpectatedResult, | ||
branches: [], | ||
tags: [], | ||
}, | ||
], | ||
])("should parse gitlog to commitRaw(branch, tag)", (mockLog, expectedResult) => { | ||
const result = getCommitRaws(mockLog); | ||
expect(result).toEqual([expectedResult]); | ||
}); | ||
|
||
it.each([ | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}\n${"10\t0\ta.ts\n1\t0\tREADME.md"}`, | ||
{ | ||
...commonExpectatedResult, | ||
differenceStatistic: { | ||
totalInsertionCount: 11, | ||
totalDeletionCount: 0, | ||
fileDictionary: { | ||
"a.ts": { insertionCount: 10, deletionCount: 0 }, | ||
"README.md": { insertionCount: 1, deletionCount: 0 }, | ||
}, | ||
}, | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}\n${"3\t3\ta.ts"}`, | ||
{ | ||
...commonExpectatedResult, | ||
differenceStatistic: { | ||
totalInsertionCount: 3, | ||
totalDeletionCount: 3, | ||
fileDictionary: { "a.ts": { insertionCount: 3, deletionCount: 3 } }, | ||
}, | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}\n${"4\t0\ta.ts"}`, | ||
{ | ||
...commonExpectatedResult, | ||
differenceStatistic: { | ||
totalInsertionCount: 4, | ||
totalDeletionCount: 0, | ||
fileDictionary: { "a.ts": { insertionCount: 4, deletionCount: 0 } }, | ||
}, | ||
}, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}\n${"0\t6\ta.ts\n2\t0\tb.ts\n3\t3\tc.ts"}`, | ||
{ | ||
...commonExpectatedResult, | ||
differenceStatistic: { | ||
totalInsertionCount: 5, | ||
totalDeletionCount: 9, | ||
fileDictionary: { | ||
"a.ts": { insertionCount: 0, deletionCount: 6 }, | ||
"b.ts": { insertionCount: 2, deletionCount: 0 }, | ||
"c.ts": { insertionCount: 3, deletionCount: 3 }, | ||
}, | ||
}, | ||
}, | ||
], | ||
])("should parse gitlog to commitRaw(file changed)", (mockLog, expectedResult) => { | ||
const result = getCommitRaws(mockLog); | ||
expect(result).toEqual([expectedResult]); | ||
}); | ||
|
||
it(`should parse gitlog to commitRaw(multiple commits)`, () => { | ||
const mockLog = `${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}\n${fakeCommitFileChange}${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessage}`; | ||
const result = getCommitRaws(mockLog); | ||
const expectedResult = [ | ||
{ ...commonExpectatedResult, differenceStatistic: expectedFileChange }, | ||
{ ...commonExpectatedResult, sequence: 1 }, | ||
]; | ||
|
||
testCommitLines.forEach((mockLog, index) => { | ||
it(`should parse gitlog to commitRaw(branch, tag)`, () => { | ||
const mock = `${mockLog} | ||
Author: John Park <[email protected]> | ||
AuthorDate: Sun Sep 4 20:17:59 2022 +0900 | ||
Commit: John Park <[email protected]> | ||
CommitDate: Sun Sep 4 20:17:59 2022 +0900 | ||
\n\tcommit message | ||
`; | ||
const result = getCommitRaws(mock); | ||
const expectedResult = { ...commonExpectatedResult, branches: expectedBranches[index], tags: expectedTags[index] }; | ||
|
||
expect(result).toEqual([expectedResult]); | ||
}); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it.each([ | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${`${GIT_LOG_SEPARATOR}commit message title${GIT_LOG_SEPARATOR}`}`, | ||
{ ...commonExpectatedResult, message: "commit message title" }, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${`${GIT_LOG_SEPARATOR}commit message title\ncommit message${GIT_LOG_SEPARATOR}`}`, | ||
{ ...commonExpectatedResult, message: "commit message title\ncommit message" }, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${`${GIT_LOG_SEPARATOR}commit message title\n\ncommit message body${GIT_LOG_SEPARATOR}`}`, | ||
{ ...commonExpectatedResult, message: "commit message title\n\ncommit message body" }, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${`${GIT_LOG_SEPARATOR}commit message title\n\n\ncommit message body${GIT_LOG_SEPARATOR}`}`, | ||
{ ...commonExpectatedResult, message: "commit message title\n\n\ncommit message body" }, | ||
], | ||
[ | ||
`${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${`${GIT_LOG_SEPARATOR}${GIT_LOG_SEPARATOR}`}`, | ||
{ ...commonExpectatedResult, message: "" }, | ||
], | ||
])("should parse gitlog to commitRaw(commit message)", (mockLog, expectedResult) => { | ||
const result = getCommitRaws(mockLog); | ||
expect(result).toEqual([expectedResult]); | ||
}); | ||
|
||
testCommitFileChanges.forEach((mockLog, index) => { | ||
it(`should parse gitlog to commitRaw(file changed)`, () => { | ||
const mock = `commit a b (HEAD) | ||
Author: John Park <[email protected]> | ||
AuthorDate: Sun Sep 4 20:17:59 2022 +0900 | ||
Commit: John Park <[email protected]> | ||
CommitDate: Sun Sep 4 20:17:59 2022 +0900 | ||
\n\tcommit message | ||
\n${mockLog} | ||
`; | ||
const result = getCommitRaws(mock); | ||
const expectedResult = { ...commonExpectatedResult, differenceStatistic: expectedFileChanged[index] }; | ||
it(`should parse gitlog to commitRaw(commit message body and file change)`, () => { | ||
const mockLog = `${COMMIT_SEPARATOR}${fakeCommitHash}${fakeCommitRef}${fakeAuthorAndCommitter}${fakeCommitMessageAndBody}\n${fakeCommitFileChange}`; | ||
const result = getCommitRaws(mockLog); | ||
const expectedResult = { | ||
...commonExpectatedResult, | ||
message: expectedCommitMessageBody, | ||
differenceStatistic: expectedFileChange, | ||
}; | ||
|
||
expect(result).toEqual([expectedResult]); | ||
}); | ||
expect(result).toEqual([expectedResult]); | ||
}); | ||
}); |
Oops, something went wrong.