Skip to content

Commit dd1b9d8

Browse files
authored
Merge pull request #281 from github/test-improvements
Test Improvements 🧪
2 parents 4365afb + a95f851 commit dd1b9d8

13 files changed

+210
-29
lines changed

__tests__/functions/help.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,41 @@ test('successfully calls help with non-defaults', async () => {
132132
expect.stringMatching(/## 📚 Branch Deployment Help/)
133133
)
134134
})
135+
136+
test('successfully calls help with non-defaults and unknown update_branch setting', async () => {
137+
const inputs = {
138+
trigger: '.deploy',
139+
reaction: 'eyes',
140+
environment: 'production',
141+
stable_branch: 'main',
142+
noop_trigger: '.noop',
143+
lock_trigger: '.lock',
144+
production_environments: 'production,production-eu,production-ap',
145+
environment_targets: 'production,staging,development',
146+
unlock_trigger: '.unlock',
147+
help_trigger: '.help',
148+
lock_info_alias: '.wcid',
149+
global_lock_flag: '--global',
150+
update_branch: 'bugzzz',
151+
outdated_mode: 'default_branch',
152+
required_contexts: 'cat',
153+
allowForks: 'false',
154+
skipCi: 'development',
155+
skipReviews: 'development',
156+
draft_permitted_targets: 'development',
157+
admins: 'monalisa',
158+
permissions: ['write', 'admin', 'maintain'],
159+
allow_sha_deployments: false,
160+
checks: 'required'
161+
}
162+
163+
expect(await help(octokit, context, 123, inputs))
164+
165+
expect(debugMock).toHaveBeenCalledWith(
166+
expect.stringMatching(/## 📚 Branch Deployment Help/)
167+
)
168+
169+
expect(debugMock).toHaveBeenCalledWith(
170+
expect.stringMatching(/Unknown value for update_branch/)
171+
)
172+
})

__tests__/functions/lock.test.js

+73-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const lockBase64Monalisa =
2626
const lockBase64Octocat =
2727
'ewogICAgInJlYXNvbiI6ICJUZXN0aW5nIG15IG5ldyBmZWF0dXJlIHdpdGggbG90cyBvZiBjYXRzIiwKICAgICJicmFuY2giOiAib2N0b2NhdHMtZXZlcnl3aGVyZSIsCiAgICAiY3JlYXRlZF9hdCI6ICIyMDIyLTA2LTE0VDIxOjEyOjE0LjA0MVoiLAogICAgImNyZWF0ZWRfYnkiOiAib2N0b2NhdCIsCiAgICAic3RpY2t5IjogdHJ1ZSwKICAgICJlbnZpcm9ubWVudCI6ICJwcm9kdWN0aW9uIiwKICAgICJ1bmxvY2tfY29tbWFuZCI6ICIudW5sb2NrIHByb2R1Y3Rpb24iLAogICAgImdsb2JhbCI6IGZhbHNlLAogICAgImxpbmsiOiAiaHR0cHM6Ly9naXRodWIuY29tL3Rlc3Qtb3JnL3Rlc3QtcmVwby9wdWxsLzIjaXNzdWVjb21tZW50LTQ1NiIKfQo='
2828

29+
const lockBase64OctocatNoReason =
30+
'ewogICAgInJlYXNvbiI6IG51bGwsCiAgICAiYnJhbmNoIjogIm9jdG9jYXRzLWV2ZXJ5d2hlcmUiLAogICAgImNyZWF0ZWRfYXQiOiAiMjAyMi0wNi0xNFQyMToxMjoxNC4wNDFaIiwKICAgICJjcmVhdGVkX2J5IjogIm9jdG9jYXQiLAogICAgInN0aWNreSI6IHRydWUsCiAgICAiZW52aXJvbm1lbnQiOiAicHJvZHVjdGlvbiIsCiAgICAidW5sb2NrX2NvbW1hbmQiOiAiLnVubG9jayBwcm9kdWN0aW9uIiwKICAgICJnbG9iYWwiOiBmYWxzZSwKICAgICJsaW5rIjogImh0dHBzOi8vZ2l0aHViLmNvbS90ZXN0LW9yZy90ZXN0LXJlcG8vcHVsbC8yI2lzc3VlY29tbWVudC00NTYiCn0K'
31+
2932
const lockBase64OctocatGlobal =
3033
'ewogICAgInJlYXNvbiI6ICJUZXN0aW5nIG15IG5ldyBmZWF0dXJlIHdpdGggbG90cyBvZiBjYXRzIiwKICAgICJicmFuY2giOiAib2N0b2NhdHMtZXZlcnl3aGVyZSIsCiAgICAiY3JlYXRlZF9hdCI6ICIyMDIyLTA2LTE0VDIxOjEyOjE0LjA0MVoiLAogICAgImNyZWF0ZWRfYnkiOiAib2N0b2NhdCIsCiAgICAic3RpY2t5IjogdHJ1ZSwKICAgICJlbnZpcm9ubWVudCI6IG51bGwsCiAgICAidW5sb2NrX2NvbW1hbmQiOiAiLnVubG9jayAtLWdsb2JhbCIsCiAgICAiZ2xvYmFsIjogdHJ1ZSwKICAgICJsaW5rIjogImh0dHBzOi8vZ2l0aHViLmNvbS90ZXN0LW9yZy90ZXN0LXJlcG8vcHVsbC8yI2lzc3VlY29tbWVudC00NTYiCn0K'
3134

@@ -728,6 +731,74 @@ test('Determines that the lock request is coming from current owner of the lock
728731
)
729732
})
730733

734+
test('checks a lock and finds that it is from another owner and that no reason was set - it was a lock for the production environment', async () => {
735+
const octokit = {
736+
rest: {
737+
repos: {
738+
getBranch: jest
739+
.fn()
740+
.mockReturnValueOnce({data: {commit: {sha: 'abc123'}}}),
741+
get: jest.fn().mockReturnValue({data: {default_branch: 'main'}}),
742+
getContent: jest
743+
.fn()
744+
.mockReturnValue({data: {content: lockBase64OctocatNoReason}})
745+
}
746+
}
747+
}
748+
expect(
749+
await lock(octokit, context, ref, 123, true, environment)
750+
).toStrictEqual({
751+
environment: 'production',
752+
global: false,
753+
globalFlag: '--global',
754+
lockData: null,
755+
status: false
756+
})
757+
expect(debugMock).toHaveBeenCalledWith(`detected lock env: ${environment}`)
758+
expect(debugMock).toHaveBeenCalledWith(`detected lock global: false`)
759+
expect(debugMock).toHaveBeenCalledWith(
760+
`constructed lock branch name: ${environment}-branch-deploy-lock`
761+
)
762+
expect(debugMock).toHaveBeenCalledWith(`no reason detected`)
763+
expect(debugMock).toHaveBeenCalledWith(
764+
`the lock was not claimed as it is owned by octocat`
765+
)
766+
})
767+
768+
test('checks a lock and finds that it is from another owner and that no reason was set - it was a lock for the production environment and sticky is set to false', async () => {
769+
const octokit = {
770+
rest: {
771+
repos: {
772+
getBranch: jest
773+
.fn()
774+
.mockReturnValueOnce({data: {commit: {sha: 'abc123'}}}),
775+
get: jest.fn().mockReturnValue({data: {default_branch: 'main'}}),
776+
getContent: jest
777+
.fn()
778+
.mockReturnValue({data: {content: lockBase64OctocatNoReason}})
779+
}
780+
}
781+
}
782+
expect(
783+
await lock(octokit, context, ref, 123, false, environment)
784+
).toStrictEqual({
785+
environment: 'production',
786+
global: false,
787+
globalFlag: '--global',
788+
lockData: null,
789+
status: false
790+
})
791+
expect(debugMock).toHaveBeenCalledWith(`detected lock env: ${environment}`)
792+
expect(debugMock).toHaveBeenCalledWith(`detected lock global: false`)
793+
expect(debugMock).toHaveBeenCalledWith(
794+
`constructed lock branch name: ${environment}-branch-deploy-lock`
795+
)
796+
expect(debugMock).toHaveBeenCalledWith(`no reason detected`)
797+
expect(debugMock).toHaveBeenCalledWith(
798+
`the lock was not claimed as it is owned by octocat`
799+
)
800+
})
801+
731802
test('Determines that the lock request is coming from current owner of the lock (GLOBAL lock) and exits - sticky', async () => {
732803
context.actor = 'octocat'
733804
context.payload.comment.body = '.lock --global'
@@ -967,9 +1038,9 @@ test('successfully obtains a deployment lock (sticky) by creating the branch and
9671038
)
9681039
})
9691040

970-
test('successfully obtains a deployment lock (sticky) by creating the branch and lock file with a --reason', async () => {
1041+
test('successfully obtains a deployment lock (sticky) by creating the branch and lock file with a --reason and assuming a null environment to start (but it is production)', async () => {
9711042
context.payload.comment.body = '.lock --reason because something is broken'
972-
expect(await lock(octokit, context, ref, 123, true, null)).toStrictEqual(
1043+
expect(await lock(octokit, context, ref, 123, true)).toStrictEqual(
9731044
createdLock
9741045
)
9751046
expect(debugMock).toHaveBeenCalledWith(`detected lock env: ${environment}`)

__tests__/functions/outdated-check.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as core from '@actions/core'
22
import {isOutdated} from '../../src/functions/outdated-check'
3+
import {COLORS} from '../../src/functions/colors'
34

45
const debugMock = jest.spyOn(core, 'debug')
6+
const warningMock = jest.spyOn(core, 'warning')
57

68
var context
79
var octokit
@@ -121,6 +123,23 @@ test('checks if the branch is out-of-date via commit comparison and finds that i
121123
outdated: true
122124
})
123125
expect(debugMock).toHaveBeenCalledWith('checking isOutdated with strict mode')
126+
expect(warningMock).toHaveBeenCalledWith(
127+
`The PR branch is behind the base branch by ${COLORS.highlight}1 commit${COLORS.reset}`
128+
)
129+
})
130+
131+
test('checks if the branch is out-of-date via commit comparison and finds that it is by many commits', async () => {
132+
octokit.rest.repos.compareCommits = jest
133+
.fn()
134+
.mockReturnValue({data: {behind_by: 45}, status: 200})
135+
expect(await isOutdated(context, octokit, data)).toStrictEqual({
136+
branch: 'test-branch',
137+
outdated: true
138+
})
139+
expect(debugMock).toHaveBeenCalledWith('checking isOutdated with strict mode')
140+
expect(warningMock).toHaveBeenCalledWith(
141+
`The PR branch is behind the base branch by ${COLORS.highlight}45 commits${COLORS.reset}`
142+
)
124143
})
125144

126145
test('checks if the branch is out-of-date via commit comparison and finds that it is only behind the stable branch', async () => {

__tests__/functions/post-deploy-message.test.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {postDeployMessage} from '../../src/functions/post-deploy-message'
22
import * as core from '@actions/core'
33
import dedent from 'dedent-js'
44

5-
// const debugMock = jest.spyOn(core, 'debug')
5+
const debugMock = jest.spyOn(core, 'debug')
66

77
var context
88
var environment
@@ -124,6 +124,28 @@ test('successfully constructs a post deploy message with the defaults during a d
124124
)
125125
})
126126

127+
test('successfully constructs a post deploy message with the defaults during a deployment with an unknown status and the DEPLOY_MESSAGE_PATH is unset', async () => {
128+
process.env.INPUT_DEPLOY_MESSAGE_PATH = ''
129+
expect(
130+
await postDeployMessage(
131+
context, // context
132+
environment, // environment
133+
environment_url, // environment_url
134+
'unknown', // status
135+
noop, // noop
136+
ref, // ref
137+
approved_reviews_count // approved_reviews_count
138+
)
139+
).toStrictEqual(
140+
dedent(`
141+
### Deployment Results ⚠️
142+
143+
Warning: deployment status is unknown, please use caution`)
144+
)
145+
146+
expect(debugMock).toHaveBeenCalledWith('deployMessagePath is not set - null')
147+
})
148+
127149
test('successfully constructs a post deploy message with a custom env var', async () => {
128150
process.env.DEPLOY_MESSAGE = 'Deployed 1 shiny new server'
129151

__tests__/functions/unlock-on-merge.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ test('successfully unlocks all environments on a pull request merge', async () =
6868
)
6969
})
7070

71+
test('finds that no deployment lock is set so none are removed', async () => {
72+
jest.spyOn(unlock, 'unlock').mockImplementation(() => {
73+
return 'no deployment lock currently set - silent'
74+
})
75+
76+
expect(
77+
await unlockOnMerge(octokit, context, environment_targets)
78+
).toStrictEqual(true)
79+
expect(debugMock).toHaveBeenCalledWith(
80+
'unlock result for unlock-on-merge: no deployment lock currently set - silent'
81+
)
82+
expect(setOutputMock).toHaveBeenCalledWith('unlocked_environments', '')
83+
})
84+
7185
test('only unlocks one environment because the other has no lock and the other is not associated with the pull request', async () => {
7286
checkLockFile.checkLockFile.mockImplementationOnce(() => {
7387
return {

dist/index.js

+19-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
],
4646
"coverageThreshold": {
4747
"global": {
48-
"lines": 100
48+
"lines": 100,
49+
"statements": 100,
50+
"branches": 100,
51+
"functions": 100
4952
}
5053
}
5154
},

src/functions/environment-targets.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,8 @@ export async function environmentTargets(
446446
)
447447

448448
return {environment: false, environmentUrl: null}
449-
}
450-
451-
// If lockChecks is set to false, this request is for a branch deploy to check the body for an environment target
452-
if (lockChecks === false) {
449+
} else {
450+
// If lockChecks is set to false, this request is for a branch deploy to check the body for an environment target
453451
const environmentObj = await onDeploymentChecks(
454452
environment_targets_sanitized,
455453
body,

src/functions/help.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ export async function help(octokit, context, reactionId, inputs) {
1111
if (inputs.update_branch.trim() === 'warn') {
1212
update_branch_message =
1313
'This Action will warn if the branch is out of date with the base branch'
14-
} else if (inputs.update_branch === 'force') {
14+
} else if (inputs.update_branch.trim() === 'force') {
1515
update_branch_message =
1616
'This Action will force update the branch to the base branch if it is out of date'
17-
} else if (inputs.update_branch === 'disabled') {
17+
} else if (inputs.update_branch.trim() === 'disabled') {
1818
update_branch_message =
1919
'This Action will not update the branch to the base branch before deployment'
20-
}
20+
} else update_branch_message = 'Unknown value for update_branch'
2121

2222
var required_contexts_message = defaultSpecificMessage
2323
if (inputs.required_contexts.trim() === 'false') {

0 commit comments

Comments
 (0)