-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: add some of the missing tests * chore: enforce 100% code coverage * test: add missing github-client tests * fix: handle invalid PR titles and add test case for it * test: improve log module tests * test: fix invalid PR assertion * chore: remove .taprc file All options that were declared there are default now. * test: reduce util tests duplication * test: use sinon.restore instead of iterating through stubs * fix: undo behavior changes Those changes will be added back in another PR if needed. * test: add test case for packages with incomplete versions
- Loading branch information
1 parent
aa24d54
commit 5bc3320
Showing
10 changed files
with
242 additions
and
9 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
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
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,95 @@ | ||
'use strict' | ||
|
||
const tap = require('tap'); | ||
const sinon = require('sinon'); | ||
|
||
const githubContext = { | ||
repository: { | ||
owner: { | ||
login: 'owner-login.', | ||
}, | ||
name: 'repository-name' | ||
} | ||
} | ||
|
||
const data = 'octokit-result' | ||
|
||
const octokitStubs = { | ||
get: sinon.stub().returns(Promise.resolve({ data })), | ||
createReview: sinon.stub().returns(Promise.resolve({ data })), | ||
merge: sinon.stub().returns(Promise.resolve({ data })), | ||
} | ||
|
||
const { githubClient } = tap.mock('../src/github-client', { | ||
'@actions/github': { | ||
context: { | ||
payload: githubContext | ||
}, | ||
getOctokit: () => ({ | ||
rest: { | ||
pulls: octokitStubs | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
const TOKEN = 'GITHUB-TOKEN' | ||
const PR_NUMBER = Math.floor(Math.random() * 10); | ||
|
||
tap.afterEach(() => { | ||
sinon.resetHistory(); | ||
}) | ||
|
||
tap.test('githubClient', async t => { | ||
t.test('getPullRequest', async () => { | ||
const result = await githubClient(TOKEN).getPullRequest(PR_NUMBER) | ||
tap.equal(result, data) | ||
|
||
sinon.assert.calledWith(octokitStubs.get, { | ||
owner: githubContext.repository.owner.login, | ||
repo: githubContext.repository.name, | ||
pull_number: PR_NUMBER | ||
}) | ||
}) | ||
|
||
t.test('approvePullRequest', async () => { | ||
const comment = 'Test pull request comment' | ||
const result = await githubClient(TOKEN).approvePullRequest(PR_NUMBER, comment) | ||
tap.equal(result, data) | ||
|
||
sinon.assert.calledWith(octokitStubs.createReview, { | ||
owner: githubContext.repository.owner.login, | ||
repo: githubContext.repository.name, | ||
pull_number: PR_NUMBER, | ||
event: 'APPROVE', | ||
body: comment | ||
}) | ||
}) | ||
|
||
t.test('mergePullRequest', async () => { | ||
const method = 'squash' | ||
const result = await githubClient(TOKEN).mergePullRequest(PR_NUMBER, method) | ||
tap.equal(result, data) | ||
|
||
sinon.assert.calledWith(octokitStubs.merge, { | ||
owner: githubContext.repository.owner.login, | ||
repo: githubContext.repository.name, | ||
pull_number: PR_NUMBER, | ||
merge_method: method, | ||
}) | ||
}) | ||
|
||
t.test('getPullRequestDiff', async () => { | ||
const result = await githubClient(TOKEN).getPullRequestDiff(PR_NUMBER) | ||
tap.equal(result, data) | ||
|
||
sinon.assert.calledWith(octokitStubs.get, { | ||
owner: githubContext.repository.owner.login, | ||
repo: githubContext.repository.name, | ||
pull_number: PR_NUMBER, | ||
mediaType: { | ||
format: 'diff' | ||
} | ||
}) | ||
}) | ||
}) |
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,40 @@ | ||
'use strict' | ||
|
||
const tap = require('tap') | ||
const sinon = require('sinon') | ||
|
||
const coreStubs = { | ||
debug: sinon.stub(), | ||
error: sinon.stub(), | ||
info: sinon.stub(), | ||
warning: sinon.stub(), | ||
} | ||
const log = tap.mock('../src/log', { | ||
'@actions/core': coreStubs | ||
}) | ||
|
||
tap.afterEach(() => { | ||
sinon.resetHistory() | ||
}) | ||
|
||
tap.test('logError should work with numbers', async () => { | ||
log.logError(100) | ||
sinon.assert.calledWith(coreStubs.error, '100') | ||
}) | ||
|
||
tap.test('logError should work with strings', async () => { | ||
log.logError('100') | ||
sinon.assert.calledWith(coreStubs.error, '100') | ||
}) | ||
|
||
tap.test('log should call aproppriate core function', async () => { | ||
const msg = 'log message' | ||
log.logError(msg) | ||
sinon.assert.calledWith(coreStubs.error, msg) | ||
log.logWarning(msg) | ||
sinon.assert.calledWith(coreStubs.warning, msg) | ||
log.logInfo(msg) | ||
sinon.assert.calledWith(coreStubs.info, msg) | ||
log.logDebug(msg) | ||
sinon.assert.calledWith(coreStubs.debug, msg) | ||
}) |
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
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,29 @@ | ||
'use strict' | ||
const tap = require('tap') | ||
|
||
const coreStubs = { | ||
'getInput': () => '', | ||
debug: msg => msg, | ||
error: msg => msg, | ||
info: msg => msg, | ||
warning: msg => msg, | ||
} | ||
|
||
tap.test('MERGE_METHOD should be squash for invalid input', async t => { | ||
const { getInputs } = t.mock('../src/util', { | ||
'@actions/core': { | ||
...coreStubs | ||
} | ||
}) | ||
t.equal(getInputs().MERGE_METHOD, 'squash') | ||
}) | ||
|
||
tap.test('MERGE_METHOD should be correct for valid input', async t => { | ||
const { getInputs } = tap.mock('../src/util', { | ||
'@actions/core': { | ||
...coreStubs, | ||
'getInput': () => 'merge', | ||
} | ||
}) | ||
t.equal(getInputs().MERGE_METHOD, 'merge') | ||
}) |