-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set commit author as local config (#152)
* fix: set commit author as local config Close #151 Co-authored-by: Ryo Ota <[email protected]>
- Loading branch information
Showing
3 changed files
with
143 additions
and
24 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,116 @@ | ||
import {getUserName, getUserEmail, setCommitAuthor} from '../src/git-utils'; | ||
import {getWorkDirName, createWorkDir} from '../src/utils'; | ||
import {CmdResult} from '../src/interfaces'; | ||
import * as exec from '@actions/exec'; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env['GITHUB_ACTOR'] = 'default-octocat'; | ||
process.env['GITHUB_REPOSITORY'] = 'owner/repo'; | ||
}); | ||
|
||
afterEach(() => { | ||
delete process.env['GITHUB_ACTOR']; | ||
delete process.env['GITHUB_REPOSITORY']; | ||
}); | ||
|
||
describe('getUserName()', () => { | ||
test('get default git user name', () => { | ||
const userName = ''; | ||
const test = getUserName(userName); | ||
expect(test).toMatch('default-octocat'); | ||
}); | ||
|
||
test('get custom git user name', () => { | ||
const userName = 'custom-octocat'; | ||
const test = getUserName(userName); | ||
expect(test).toMatch(userName); | ||
}); | ||
}); | ||
|
||
describe('getUserEmail()', () => { | ||
test('get default git user email', () => { | ||
const userEmail = ''; | ||
const test = getUserEmail(userEmail); | ||
expect(test).toMatch('[email protected]'); | ||
}); | ||
|
||
test('get custom git user email', () => { | ||
const userEmail = '[email protected]'; | ||
const test = getUserEmail(userEmail); | ||
expect(test).toMatch(userEmail); | ||
}); | ||
}); | ||
|
||
describe('setCommitAuthor()', () => { | ||
let workDirName = ''; | ||
(async (): Promise<void> => { | ||
const date = new Date(); | ||
const unixTime = date.getTime(); | ||
workDirName = await getWorkDirName(`${unixTime}`); | ||
})(); | ||
|
||
beforeEach(async () => { | ||
await createWorkDir(workDirName); | ||
process.chdir(workDirName); | ||
await exec.exec('git', ['init']); | ||
}); | ||
|
||
test('get default commit author', async () => { | ||
const userName = ''; | ||
const userEmail = ''; | ||
const result: CmdResult = { | ||
exitcode: 0, | ||
output: '' | ||
}; | ||
const options = { | ||
listeners: { | ||
stdout: (data: Buffer): void => { | ||
result.output += data.toString(); | ||
} | ||
} | ||
}; | ||
await setCommitAuthor(userName, userEmail); | ||
result.exitcode = await exec.exec('git', ['config', 'user.name'], options); | ||
expect(result.output).toMatch('default-octocat'); | ||
result.exitcode = await exec.exec('git', ['config', 'user.email'], options); | ||
expect(result.output).toMatch('[email protected]'); | ||
}); | ||
|
||
test('get custom commit author', async () => { | ||
const userName = 'custom-octocat'; | ||
const userEmail = '[email protected]'; | ||
const result: CmdResult = { | ||
exitcode: 0, | ||
output: '' | ||
}; | ||
const options = { | ||
listeners: { | ||
stdout: (data: Buffer): void => { | ||
result.output += data.toString(); | ||
} | ||
} | ||
}; | ||
await setCommitAuthor(userName, userEmail); | ||
result.exitcode = await exec.exec('git', ['config', 'user.name'], options); | ||
expect(result.output).toMatch(userName); | ||
result.exitcode = await exec.exec('git', ['config', 'user.email'], options); | ||
expect(result.output).toMatch(userEmail); | ||
}); | ||
|
||
test('throw error user_email is undefined', async () => { | ||
const userName = 'custom-octocat'; | ||
const userEmail = ''; | ||
await expect(setCommitAuthor(userName, userEmail)).rejects.toThrowError( | ||
'user_email is undefined' | ||
); | ||
}); | ||
|
||
test('throw error user_name is undefined', async () => { | ||
const userName = ''; | ||
const userEmail = '[email protected]'; | ||
await expect(setCommitAuthor(userName, userEmail)).rejects.toThrowError( | ||
'user_name is undefined' | ||
); | ||
}); | ||
}); |
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