-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.test.ts
More file actions
213 lines (185 loc) · 6.19 KB
/
main.test.ts
File metadata and controls
213 lines (185 loc) · 6.19 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import type { ArtifactClient } from '@actions/artifact'
import core from '@actions/core'
import github from '@actions/github'
import type { Report } from '@code-pushup/models'
import { jest } from '@jest/globals'
import type { components } from '@octokit/openapi-types'
import type { RestEndpointMethodTypes } from '@octokit/plugin-rest-endpoint-methods'
import {
copyFile,
mkdir,
readFile,
rename,
rm,
writeFile
} from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import {
simpleGit,
type DiffResult,
type FetchResult,
type SimpleGit
} from 'simple-git'
import { run } from '../src/main'
describe('code-pushup action', () => {
const workDir = join(process.cwd(), 'tmp', 'git-repo')
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const getOctokit = () =>
({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
paginate: (async () => []) as any,
rest: {
issues: {
createComment: async () => ({
data: { id: 10 }
}),
updateComment: async ({
comment_id
}: RestEndpointMethodTypes['issues']['updateComment']['parameters']) => ({
data: { id: comment_id }
})
},
actions: {
getWorkflowRun: async () => ({ data: { workflow_id: 3 } }),
listWorkflowRuns: async () => ({
data: {
workflow_runs: [] as components['schemas']['workflow-run'][]
}
})
}
}
}) as ReturnType<typeof github.getOctokit>
let git: SimpleGit
let artifact: ArtifactClient
beforeEach(async () => {
jest.clearAllMocks()
jest.spyOn(process, 'cwd').mockReturnValue(workDir)
jest.spyOn(core, 'setFailed').mockReturnValue()
process.env['INPUT_TOKEN'] = '<mock-github-token>'
process.env['INPUT_BIN'] = 'npx code-pushup'
process.env['INPUT_DIRECTORY'] = workDir
process.env['INPUT_PARALLEL'] = 'false'
process.env['INPUT_RETENTION'] = '14'
process.env['INPUT_TASK'] = 'code-pushup'
process.env['INPUT_SILENT'] = 'true'
process.env['INPUT_ARTIFACTS'] = 'true'
process.env['INPUT_ANNOTATIONS'] = 'true'
process.env['INPUT_SKIPCOMMENT'] = 'false'
jest.spyOn(github.context, 'repo', 'get').mockReturnValue({
owner: 'dunder-mifflin',
repo: 'website'
})
jest.spyOn(github.context, 'issue', 'get').mockImplementation(() => ({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
number:
github.context.payload.pull_request?.number ??
github.context.payload.number
}))
artifact = {
uploadArtifact: jest
.fn<ArtifactClient['uploadArtifact']>()
.mockResolvedValue({ id: 123, size: 12345 })
} as Partial<ArtifactClient> as ArtifactClient
await rm(workDir, { recursive: true, force: true })
await mkdir(workDir, { recursive: true })
await copyFile(
join(
fileURLToPath(dirname(import.meta.url)),
'fixtures',
'code-pushup.config.ts'
),
join(workDir, 'code-pushup.config.ts')
)
await writeFile(join(workDir, 'index.js'), 'console.log("Hello, world!")')
git = simpleGit(workDir)
jest.spyOn(git, 'fetch').mockResolvedValue({} as FetchResult)
jest.spyOn(git, 'diffSummary').mockResolvedValue({
files: [{ file: 'index.ts', binary: false }]
} as DiffResult)
jest.spyOn(git, 'diff').mockResolvedValue('')
await git.init()
await git.addConfig('user.name', 'John Doe')
await git.addConfig('user.email', 'john.doe@example.com')
await git.addConfig('commit.gpgSign', 'false')
await git.addConfig('tag.gpgSign', 'false')
await git.branch(['-M', 'main'])
await git.add('index.js')
await git.add('code-pushup.config.ts')
await git.commit('Initial commit')
github.context.ref = 'refs/heads/main'
github.context.sha = await git.revparse('main')
})
afterAll(async () => {
await rm(workDir, { recursive: true, force: true })
})
describe('push event', () => {
beforeEach(async () => {
github.context.payload = {}
await git.checkout('main')
})
it('should collect report', async () => {
await run(artifact, getOctokit, git)
expect(core.setFailed).not.toHaveBeenCalled()
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(artifact.uploadArtifact).toHaveBeenCalledWith<
Parameters<ArtifactClient['uploadArtifact']>
>(
'code-pushup-report',
expect.arrayContaining([
expect.stringContaining('report.json'),
expect.stringContaining('report.md')
]),
process.cwd(),
{ retentionDays: 14 }
)
const jsonPromise = readFile(
join(workDir, '.code-pushup/report.json'),
'utf8'
)
await expect(jsonPromise).resolves.toBeTruthy()
const report = JSON.parse(await jsonPromise) as Report
expect(report).toEqual(
expect.objectContaining({
plugins: [
expect.objectContaining({
slug: 'ts-migration',
audits: [
expect.objectContaining({
score: 0.5,
displayValue: '50% converted'
})
]
})
]
})
)
})
})
describe('pull request event', () => {
beforeEach(async () => {
await git.checkoutLocalBranch('feature-1')
await rename(join(workDir, 'index.js'), join(workDir, 'index.ts'))
await git.add('index.ts')
await git.commit('Convert JS file to TS')
github.context.payload = {
pull_request: {
number: 42,
head: { ref: 'feature-1', sha: await git.revparse('feature-1') },
base: { ref: 'main', sha: await git.revparse('main') }
}
}
})
it('should compare reports', async () => {
await run(artifact, getOctokit, git)
const mdPromise = readFile(
join(workDir, '.code-pushup/report-diff.md'),
'utf8'
)
await expect(mdPromise).resolves.toBeTruthy()
const md = await mdPromise
expect(md.replace(/[\da-f]{40}/g, '`<commit-sha>`')).toMatchSnapshot()
})
})
})