Skip to content

Commit

Permalink
Add missing tests (#180)
Browse files Browse the repository at this point in the history
* 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
guilhermelimak committed Apr 14, 2022
1 parent aa24d54 commit 5bc3320
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 9 deletions.
5 changes: 0 additions & 5 deletions .taprc

This file was deleted.

5 changes: 3 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9383,6 +9383,7 @@ ${changedExcludedPackages.join(', ')}. Skipping.`)
function isAMajorReleaseBump(change) {
const from = change.delete
const to = change.insert

if (!from || !to) {
return false
}
Expand Down Expand Up @@ -9652,13 +9653,13 @@ const getMergeMethod = () => {
}

const parseCommaSeparatedValue = (value) => {
return value.split(',').map(el => el.trim());
return value ? value.split(',').map(el => el.trim()) : [];
}

exports.getInputs = () => ({
GITHUB_TOKEN: core.getInput('github-token', { required: true }),
MERGE_METHOD: getMergeMethod(),
EXCLUDE_PKGS: parseCommaSeparatedValue(core.getInput('exclude')) || [],
EXCLUDE_PKGS: parseCommaSeparatedValue(core.getInput('exclude')),
MERGE_COMMENT: core.getInput('merge-comment') || '',
APPROVE_ONLY: /true/i.test(core.getInput('approve-only')),
TARGET: getTargetInput(core.getInput('target')),
Expand Down
1 change: 1 addition & 0 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ ${changedExcludedPackages.join(', ')}. Skipping.`)
function isAMajorReleaseBump(change) {
const from = change.delete
const to = change.insert

if (!from || !to) {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const getMergeMethod = () => {
}

const parseCommaSeparatedValue = (value) => {
return value.split(',').map(el => el.trim());
return value ? value.split(',').map(el => el.trim()) : [];
}

exports.getInputs = () => ({
GITHUB_TOKEN: core.getInput('github-token', { required: true }),
MERGE_METHOD: getMergeMethod(),
EXCLUDE_PKGS: parseCommaSeparatedValue(core.getInput('exclude')) || [],
EXCLUDE_PKGS: parseCommaSeparatedValue(core.getInput('exclude')),
MERGE_COMMENT: core.getInput('merge-comment') || '',
APPROVE_ONLY: /true/i.test(core.getInput('approve-only')),
TARGET: getTargetInput(core.getInput('target')),
Expand Down
51 changes: 51 additions & 0 deletions test/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,54 @@ tap.test('should not merge major bump if updating github-action-merge-dependabot
sinon.assert.notCalled(stubs.approveStub)
sinon.assert.notCalled(stubs.mergeStub)
})

tap.test('should throw if the PR title is not valid', async () => {
const PR_NUMBER = Math.random()

const { action, stubs } = buildStubbedAction({
payload: {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'Invalid PR title'
}
},
inputs: {
PR_NUMBER,
TARGET: 'major',
EXCLUDE_PKGS: ['react'],
}
})

stubs.prDiffStub.resolves(diffs.noPackageJsonChanges)

await action()
sinon.assert.called(stubs.approveStub)
sinon.assert.called(stubs.mergeStub)
})

tap.test('should handle a newly added package', async () => {
const PR_NUMBER = Math.random()

const { action, stubs } = buildStubbedAction({
payload: {
pull_request: {
number: PR_NUMBER,
user: { login: BOT_NAME },
title: 'Invalid PR title'
}
},
inputs: {
PR_NUMBER,
TARGET: 'any',
EXCLUDE_PKGS: ['react'],
}
})

stubs.prDiffStub.resolves(diffs.thisModuleAdded)

await action()

sinon.assert.called(stubs.approveStub)
sinon.assert.called(stubs.mergeStub)
})
95 changes: 95 additions & 0 deletions test/github-client.test.js
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'
}
})
})
})
40 changes: 40 additions & 0 deletions test/log.test.js
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)
})
17 changes: 17 additions & 0 deletions test/moduleChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ index d3dfd3d..bd28161 100644
@@ -19,9 +19,9 @@
- "dotbot": "aa93350",
+ "dotbot": "ac5793c",
`,
thisModuleAdded: `
diff --git a/package.json b/package.json
index d3dfd3d..bd28161 100644
--- a/package.json
+++ b/package.json
@@ -19,9 +19,9 @@
+ "github-action-merge-dependabot": "1.4.0",
`,
multiplePackagesMajorMinor: `
diff --git a/package.json b/package.json
Expand Down Expand Up @@ -146,6 +154,15 @@ index d3dfd3d..bd28161 100644
@@ -19,9 +19,9 @@
- "github-action-merge-dependabot": "1.2.3",
+ "github-action-merge-dependabot": "2.1.0",
`,
thisModuleInvalidVersion: `
diff --git a/package.json b/package.json
index d3dfd3d..bd28161 100644
--- a/package.json
+++ b/package.json
@@ -19,9 +19,9 @@
- "github-action-merge-dependabot": "",
+ "github-action-merge-dependabot": "",
`,
noPackageJsonChanges: `
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
Expand Down
4 changes: 4 additions & 0 deletions test/moduleVersionChanges.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,8 @@ tap.test('checkModuleVersionChanges', async t => {
t.notOk(checkModuleVersionChanges(moduleChanges.multipleFilesDiff, targetOptions.minor))
t.ok(checkModuleVersionChanges(moduleChanges.multipleFilesDiff, targetOptions.major))
})

t.test('should deal with invalid package version', async t => {
t.notOk(checkModuleVersionChanges({ 'github-action-merge-dependabot-': { insert: '', delete: '' } }, targetOptions.minor))
})
})
29 changes: 29 additions & 0 deletions test/util.test.js
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')
})

0 comments on commit 5bc3320

Please sign in to comment.