Skip to content

Commit 2640e4c

Browse files
committed
fix(action): fix bug with value, add tests
1 parent 83467da commit 2640e4c

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/action.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { existsSync } from 'fs'
22
import { resolve } from 'path'
3-
import { getInput, setFailed } from '@actions/core'
3+
import { getInput, setFailed, setOutput } from '@actions/core'
44
import { context as eventContext, getOctokit } from '@actions/github'
55
import lint from '@commitlint/lint'
66
import { format } from '@commitlint/format'
@@ -16,7 +16,7 @@ const { GITHUB_EVENT_NAME, GITHUB_SHA } = process.env
1616

1717
const configPath = resolve(process.env.GITHUB_WORKSPACE, getInput('configFile'))
1818

19-
const failOnErrors = resolve(process.env.GITHUB_WORKSPACE, getInput('failOnErrors', { required: false }))
19+
const failOnErrors = getInput('failOnErrors')
2020

2121
const getCommitDepth = () => {
2222
const commitDepthString = getInput('commitDepth')
@@ -147,12 +147,12 @@ const showLintResults = async ([from, to]) => {
147147

148148
if (hasOnlyWarnings(lintedCommits)) {
149149
handleOnlyWarnings(formattedResults)
150-
} else if (formattedResults && (failOnErrors == false)) {
151-
// https://github.com/actions/toolkit/tree/master/packages/core#exit-codes
150+
} else if (formattedResults && failOnErrors === 'false') {
151+
// https://github.com/actions/toolkit/tree/master/packages/core#exit-codes
152152
// this would be a good place to implement the setNeutral() when it's eventually implimented.
153-
// for now it can pass with a check mark.
153+
// for now it can pass with a check mark.
154154
console.log('Passing despite errors ✅')
155-
console.log(`You have commit messages with errors\n\n${formattedResults}`)
155+
setOutput(`You have commit messages with errors\n\n${formattedResults}`)
156156
} else if (formattedResults) {
157157
setFailedAction(formattedResults)
158158
} else {
@@ -164,7 +164,6 @@ const exitWithMessage = (message) => (error) => {
164164
setFailedAction(`${message}\n${error.message}\n${error.stack}`)
165165
}
166166

167-
168167
const commitLinterAction = () =>
169168
getRangeForEvent()
170169
.catch(

src/action.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,49 @@ describe('Commit Linter action', () => {
110110

111111
await runAction()
112112

113+
console.log()
113114
td.verify(core.setFailed(contains('wrong message 1')))
114115
td.verify(core.setFailed(contains('wrong message 2')))
115116
})
116117

118+
it('should pass for push range with wrong messages with failOnErrors set to false', async () => {
119+
td.when(core.getInput('failOnErrors')).thenReturn('false')
120+
cwd = await git.bootstrap('fixtures/conventional')
121+
await gitEmptyCommit(cwd, 'message from before push')
122+
await gitEmptyCommit(cwd, 'wrong message 1')
123+
await gitEmptyCommit(cwd, 'wrong message 2')
124+
const [before, , to] = await getCommitHashes(cwd)
125+
await createPushEventPayload(cwd, { before, to })
126+
updatePushEnvVars(cwd, to)
127+
td.replace(process, 'cwd', () => cwd)
128+
td.replace(console, 'log')
129+
130+
await runAction()
131+
132+
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
133+
td.verify(console.log(contains('Passing despite errors ✅')))
134+
td.verify(core.setOutput(contains('wrong message 1')))
135+
td.verify(core.setOutput(contains('wrong message 2')))
136+
})
137+
138+
it('should pass for push range with correct messages with failOnErrors set to false', async () => {
139+
td.when(core.getInput('failOnErrors')).thenReturn('false')
140+
cwd = await git.bootstrap('fixtures/conventional')
141+
await gitEmptyCommit(cwd, 'message from before push')
142+
await gitEmptyCommit(cwd, 'chore: correct message 1')
143+
await gitEmptyCommit(cwd, 'chore: correct message 2')
144+
const [before, , to] = await getCommitHashes(cwd)
145+
await createPushEventPayload(cwd, { before, to })
146+
updatePushEnvVars(cwd, to)
147+
td.replace(process, 'cwd', () => cwd)
148+
td.replace(console, 'log')
149+
150+
await runAction()
151+
152+
td.verify(core.setFailed(), { times: 0, ignoreExtraArgs: true })
153+
td.verify(console.log('Lint free! 🎉'))
154+
})
155+
117156
it('should pass for push range with correct messages', async () => {
118157
cwd = await git.bootstrap('fixtures/conventional')
119158
await gitEmptyCommit(cwd, 'message from before push')

0 commit comments

Comments
 (0)