-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
action.test.ts
177 lines (161 loc) · 6.79 KB
/
action.test.ts
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
import * as process from 'process';
import * as path from 'path';
import * as fs from 'fs';
import { Context } from '@actions/github/lib/context';
import * as core from '@actions/core';
import { Octokit } from '@octokit/rest';
import { action } from './action';
import { AppError } from './error';
import { fixturesLocation, root } from './test/helper';
import * as helper from './test/helper';
const configFile = path.resolve(root, 'cspell.json');
const timeout = 20000;
const spyLog = jest.spyOn(console, 'log').mockImplementation(() => {});
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
const spyError = jest.spyOn(console, 'error').mockImplementation(() => {});
const spyStdout = jest.spyOn(process.stdout, 'write').mockImplementation(function () {
return true;
});
describe('Validate Action', () => {
beforeEach(() => {
cleanEnv();
spyWarn.mockClear();
spyError.mockClear();
spyLog.mockClear();
spyStdout.mockClear();
process.env.INPUT_GITHUB_TOKEN = process.env['GITHUB_TOKEN'];
});
test.each`
test | file | expected
${'bad root'} | ${'bad_params/bad_root.json'} | ${new AppError('Bad Configuration.')}
${'missing config'} | ${'bad_params/missing_config.json'} | ${new AppError('Bad Configuration.')}
${'bad inline'} | ${'bad_params/bad_inline.json'} | ${new AppError('Bad Configuration.')}
${'bad_incremental_files_only'} | ${'bad_params/bad_incremental_files_only.json'} | ${new AppError('Bad Configuration.')}
${'bad strict'} | ${'bad_params/bad_strict.json'} | ${new AppError('Bad Configuration.')}
`(
'$test',
async ({ file, expected }) => {
const context = createContextFromFile(file);
const octokit = createOctokit();
expect.assertions(1);
await expect(action(context, octokit)).rejects.toEqual(expected);
},
timeout
);
test.each`
testName | file | expected
${'event push main.js'} | ${'push.json'} | ${true}
${'event pull_request main.js'} | ${'pull_request.json'} | ${true}
${'event pull_request_with_files main.js'} | ${'pull_request_with_files.json'} | ${true}
`(
'$testName',
async ({ testName, file, expected }) => {
return helper.pollyRun(
__filename,
testName,
async () => {
const context = createContextFromFile(file);
const octokit = createOctokit();
expect.assertions(1);
await expect(action(context, octokit)).resolves.toBe(expected);
},
{ recordIfMissing: true }
);
},
timeout
);
test.each`
files | expected
${'**'} | ${false}
${'**/*.md'} | ${true}
`(
'check all $files',
async ({ files, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const context = createContextFromFile('pull_request.json', {
INPUT_FILES: files,
INPUT_INCREMENTAL_FILES_ONLY: 'false',
});
const octokit = createOctokit();
await expect(action(context, octokit)).resolves.toBe(expected);
expect(warnings).toMatchSnapshot();
expect(spyStdout).toHaveBeenCalled();
},
timeout
);
test.each`
files | incremental | contextFile | expected
${'fixtures/sampleCode/**'} | ${false} | ${'pull_request_with_files.json'} | ${false}
${'fixtures/sampleCode/**'} | ${true} | ${'bad_params/bad_unsupported_event.json'} | ${true}
`(
'check files with issues $files',
async ({ files, incremental, contextFile, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const context = createContextFromFile(contextFile, {
INPUT_FILES: files,
INPUT_INCREMENTAL_FILES_ONLY: incremental ? 'true' : 'false',
});
const octokit = createOctokit();
await expect(action(context, octokit)).resolves.toBe(expected);
expect(warnings).toMatchSnapshot();
expect(spyLog.mock.calls).toMatchSnapshot();
expect(spyStdout.mock.calls).toMatchSnapshot();
expect(spyStdout.mock.calls.map((call) => call.join('').trim()).filter((a) => !!a)).toMatchSnapshot();
},
timeout
);
});
function cleanEnv() {
delete process.env.GITHUB_ACTION;
delete process.env.GITHUB_ACTOR;
delete process.env.GITHUB_EVENT_NAME;
delete process.env.GITHUB_EVENT_PATH;
delete process.env.GITHUB_JOB;
delete process.env.GITHUB_REF;
delete process.env.GITHUB_RUN_ID;
delete process.env.GITHUB_RUN_NUMBER;
delete process.env.GITHUB_SHA;
delete process.env.GITHUB_WORKFLOW;
for (const key of Object.keys(process.env)) {
if (key.startsWith('INPUT_')) {
delete process.env[key];
}
}
}
function fetchGithubActionFixture(filename: string): Record<string, string> {
const githubEnv = JSON.parse(fs.readFileSync(path.resolve(fixturesLocation, filename), 'utf-8'));
if (githubEnv['GITHUB_EVENT_PATH']) {
githubEnv['GITHUB_EVENT_PATH'] = path.resolve(root, githubEnv['GITHUB_EVENT_PATH']);
}
return githubEnv;
}
function getGithubToken(): string {
const t0 = core.getInput('github_token', { required: true });
if (t0[0] !== '$') {
return t0;
}
return process.env[t0.slice(1)] || 'undefined';
}
function createContextFromFile(filename: string, ...params: Record<string, string>[]): Context {
return createContext(fetchGithubActionFixture(filename), ...params);
}
function createContext(...params: Record<string, string>[]): Context {
Object.assign(process.env, ...params);
setEnvIfNotExist('INPUT_ROOT', root);
setEnvIfNotExist('INPUT_CONFIG', configFile);
process.env.INPUT_CONFIG = path.resolve(root, process.env.INPUT_CONFIG || configFile);
return new Context();
}
function setEnvIfNotExist(key: string, value: string) {
if (process.env[key] === undefined) {
process.env[key] = value;
}
}
function createOctokit(): Octokit {
return new Octokit({
auth: getGithubToken(),
userAgent: 'cspell-action-test',
});
}