-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathpr-title.spec.mjs
More file actions
81 lines (71 loc) · 2.55 KB
/
Copy pathpr-title.spec.mjs
File metadata and controls
81 lines (71 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import assert from 'node:assert/strict'
import fs from 'node:fs'
import vm from 'node:vm'
import { describe, it } from 'mocha'
import YAML from 'yaml'
const workflow = YAML.parse(fs.readFileSync(new URL('../.github/workflows/pr-title.yml', import.meta.url), 'utf8'))
const job = workflow.jobs['conventional-commit']
const steps = new Map()
for (const step of job.steps) steps.set(step.name, step)
const checkout = steps.get('Checkout base revision')
const validation = steps.get('Validate PR title and release-note context')
const labelSync = steps.get('Sync labels with PR title')
const validateTitle = vm.runInNewContext(
`(async function validateTitle (context, core, github, process, require) {\n${validation.with.script}\n})`
)
const releaseHelpers = {
appendChangedPaths: Function.prototype,
isInternalOnly: () => false,
}
const loadReleaseHelpers = () => releaseHelpers
describe('PR title workflow', () => {
it('lists changed files only for public title types', async () => {
const expectedCallsByType = new Map([
['feat', 1],
['fix', 1],
['perf', 1],
['docs', 1],
['style', 0],
['refactor', 0],
['test', 0],
['bench', 0],
['build', 0],
['ci', 0],
['chore', 0],
['revert', 0],
])
const validations = []
for (const [type, expectedCalls] of expectedCallsByType) {
let listFilesCalls = 0
const context = {
repo: { owner: 'DataDog', repo: 'dd-trace-js' },
payload: { pull_request: { number: 1, title: `${type}: change` } },
}
const core = {
info: Function.prototype,
setFailed: assert.fail,
}
const github = {
paginate: () => {
listFilesCalls++
return []
},
rest: { pulls: { listFiles: Function.prototype } },
}
const process = { env: { PR_TITLE_PATTERN: job.env.PR_TITLE_PATTERN } }
validations.push(validateTitle(context, core, github, process, loadReleaseHelpers))
assert.strictEqual(listFilesCalls, expectedCalls, type)
}
await Promise.all(validations)
})
it('syncs labels only for events that can require reconciliation', () => {
assert.strictEqual(labelSync.if.replaceAll(/\s+/g, ' '),
"steps.rename.outputs.renamed != 'true' && " +
"(github.event.action == 'opened' || " +
"github.event.action == 'reopened' || " +
"(github.event.action == 'edited' && github.event.changes.title != null))")
})
it('checks out the workflow revision', () => {
assert.strictEqual(checkout.with.ref, '$' + '{{ github.sha }}')
})
})