-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
360 lines (314 loc) · 10.9 KB
/
index.js
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* eslint no-console: 0 */
const path = require('path');
const nodegit = require('nodegit');
const { Client } = require('pg');
const fse = require('fs-extra');
const spawn = require('cross-spawn');
const { At } = require('../../models');
const {
createTestPlanVersion,
getTestPlanVersions
} = require('../../models/services/TestPlanVersionService');
const {
createTestId,
createScenarioId,
createAssertionId
} = require('../../services/PopulatedData/locationOfDataId');
const deepPickEqual = require('../../util/deepPickEqual');
const args = require('minimist')(process.argv.slice(2), {
alias: {
h: 'help',
c: 'commit'
}
});
if (args.help) {
console.log(`
Default use:
No arguments:
Fetch most recent aria-at tests to update database. By default, the latest commit on the default branch.
Arguments:
-h, --help
Show this message.
-c, --commit
Import tests at the specified git commit
`);
process.exit();
}
const client = new Client();
const ariaAtRepo = 'https://github.com/w3c/aria-at.git';
const DEFAULT_BRANCH = 'master';
const gitCloneDirectory = path.resolve(__dirname, 'tmp');
const builtTestsDirectory = path.resolve(gitCloneDirectory, 'build', 'tests');
const testsDirectory = path.resolve(gitCloneDirectory, 'tests');
const importTestPlanVersions = async () => {
await client.connect();
const { gitCommitDate, gitMessage, gitSha } = await readRepo();
console.log('Running `npm install` ...\n');
const installOutput = spawn.sync('npm', ['install'], {
cwd: gitCloneDirectory
});
console.log('`npm install` output', installOutput.stdout.toString());
console.log('Running `npm run build` ...\n');
const buildOutput = spawn.sync('npm', ['run', 'build'], {
cwd: gitCloneDirectory
});
console.log('`npm run build` output', buildOutput.stdout.toString());
if (buildOutput.status !== 0) {
throw new Error(
'When executed within the ARIA-AT project, the command `npm run build` failed.'
);
}
const ats = await At.findAll();
await updateAtsJson(ats);
await updateCommandsJson();
for (const directory of fse.readdirSync(builtTestsDirectory)) {
if (directory === 'resources') continue;
const builtDirectoryPath = path.join(builtTestsDirectory, directory);
const sourceDirectoryPath = path.join(testsDirectory, directory);
// https://github.com/w3c/aria-at/commit/9d73d6bb274b3fe75b9a8825e020c0546a33a162
// This is the date of the last commit before the build folder removal.
// Meant to support backward compatability until the existing tests can
// be updated to the current structure
const buildRemovalDate = new Date('2022-03-10 18:08:36.000000 +00:00');
const useBuildInAppAppUrlPath =
gitCommitDate.getTime() <= buildRemovalDate.getTime();
if (
!(
fse.existsSync(sourceDirectoryPath) &&
fse.statSync(builtDirectoryPath).isDirectory()
)
) {
continue;
}
const existing = await getTestPlanVersions('', {
directory,
gitSha
});
if (existing.length) continue;
// Gets the next ID and increments the ID counter in Postgres
// Needed to create the testIds - see LocationOfDataId.js for more info
const testPlanVersionId = (
await client.query(
`SELECT nextval(
pg_get_serial_sequence('"TestPlanVersion"', 'id')
)`
)
).rows[0].nextval;
const { title, exampleUrl, designPatternUrl, testPageUrl } = readCsv({
sourceDirectoryPath
});
const tests = getTests({
builtDirectoryPath,
testPlanVersionId,
ats,
gitSha
});
await createTestPlanVersion({
id: testPlanVersionId,
title,
directory,
testPageUrl: getAppUrl(testPageUrl, {
gitSha,
directoryPath: useBuildInAppAppUrlPath
? builtDirectoryPath
: sourceDirectoryPath
}),
gitSha,
gitMessage,
updatedAt: gitCommitDate,
metadata: {
designPatternUrl,
exampleUrl
},
tests
});
}
};
const readRepo = async () => {
fse.ensureDirSync(gitCloneDirectory);
let repo = await nodegit.Clone(ariaAtRepo, gitCloneDirectory, {});
console.log(`Cloned ${path.basename(ariaAtRepo)} to ${repo.workdir()}`);
let commit;
if (args.commit) {
try {
commit = await nodegit.Commit.lookup(repo, args.commit);
} catch (error) {
console.log(
`IMPORT FAILED! Cannot checkout repo at commit: ${args.commit}`
);
throw error;
}
await nodegit.Checkout.tree(repo, commit);
await repo.setHeadDetached(commit);
} else {
let latestCommit = fse
.readFileSync(
path.join(
gitCloneDirectory,
'.git',
'refs',
'heads',
DEFAULT_BRANCH
),
'utf8'
)
.trim();
commit = await nodegit.Commit.lookup(repo, latestCommit);
}
return {
gitCommitDate: commit.date(),
gitMessage: commit.message(),
gitSha: commit.id().tostrS()
};
};
const getAppUrl = (directoryRelativePath, { gitSha, directoryPath }) => {
return path.join(
'/',
'aria-at', // The app's proxy to the ARIA-AT repo
gitSha,
path.relative(
gitCloneDirectory,
path.join(directoryPath, directoryRelativePath)
)
);
};
const readCsv = ({ sourceDirectoryPath }) => {
// 'references.csv' only exists in <root>/tests/<directory>/data/references.csv
// doesn't exist in <root>/build/tests/<directory>/data/references.csv
const referencesCsvPath = path.join(
sourceDirectoryPath,
'data',
'references.csv'
);
const referencesCsv = fse.readFileSync(referencesCsvPath, {
encoding: 'utf-8'
});
const getCsvValue = refId => {
const line = referencesCsv
.split('\n')
.find(line => line.includes(refId));
const columns = line?.split(',');
return columns?.[1];
};
return {
title: getCsvValue('title'),
exampleUrl: getCsvValue('example'),
designPatternUrl: getCsvValue('designPattern'),
testPageUrl: getCsvValue('reference')
};
};
const updateCommandsJson = async () => {
const keysMjsPath = path.join(testsDirectory, 'resources', 'keys.mjs');
const commands = Object.entries(
await import(keysMjsPath)
).map(([id, text]) => ({ id, text }));
await fse.writeFile(
path.resolve(__dirname, '../../resources/commands.json'),
JSON.stringify(commands, null, 4)
);
};
const updateAtsJson = async ats => {
await fse.writeFile(
path.resolve(__dirname, '../../resources/ats.json'),
JSON.stringify(
ats.map(at => at.dataValues),
null,
4
)
);
};
const getTests = ({ builtDirectoryPath, testPlanVersionId, ats, gitSha }) => {
const tests = [];
const renderedUrlsByNumber = {};
const allCollectedByNumber = {};
fse.readdirSync(builtDirectoryPath).forEach(filePath => {
if (!filePath.endsWith('.collected.json')) return;
const jsonPath = path.join(builtDirectoryPath, filePath);
const jsonString = fse.readFileSync(jsonPath, 'utf8');
const collected = JSON.parse(jsonString);
const renderedUrl = filePath.replace(/\.json$/, '.html');
if (!allCollectedByNumber[collected.info.testId]) {
allCollectedByNumber[collected.info.testId] = [];
renderedUrlsByNumber[collected.info.testId] = [];
}
allCollectedByNumber[collected.info.testId].push(collected);
renderedUrlsByNumber[collected.info.testId].push(renderedUrl);
});
Object.entries(allCollectedByNumber).forEach(([number, allCollected]) => {
const renderedUrls = renderedUrlsByNumber[number];
if (
!deepPickEqual(allCollected, {
excludeKeys: ['at', 'mode', 'commands']
})
) {
throw new Error(
'Difference found in a part of a .collected.json file which ' +
'should be equivalent'
);
}
const common = allCollected[0];
const testId = createTestId(testPlanVersionId, common.info.testId);
const atIds = allCollected.map(
collected => ats.find(at => at.name === collected.target.at.name).id
);
tests.push({
id: testId,
rowNumber: number,
title: common.info.title,
atIds,
atMode: common.target.mode.toUpperCase(),
renderableContent: Object.fromEntries(
allCollected.map((collected, index) => {
return [atIds[index], collected];
})
),
renderedUrls: Object.fromEntries(
atIds.map((atId, index) => {
return [
atId,
getAppUrl(renderedUrls[index], {
gitSha,
directoryPath: builtDirectoryPath
})
];
})
),
scenarios: (() => {
const scenarios = [];
allCollected.forEach(collected => {
collected.commands.forEach(command => {
scenarios.push({
id: createScenarioId(testId, scenarios.length),
atId: ats.find(
at => at.name === collected.target.at.name
).id,
commandIds: command.keypresses.map(({ id }) => id)
});
});
});
return scenarios;
})(),
assertions: common.assertions.map((assertion, index) => ({
id: createAssertionId(testId, index),
priority: assertion.priority === 1 ? 'REQUIRED' : 'OPTIONAL',
text: assertion.expectation
})),
viewers: []
});
});
return tests;
};
importTestPlanVersions()
.then(
() => console.log('Done, no errors'),
err => {
console.error(`Error found: ${err.stack}`);
process.exitCode = 1;
}
)
.finally(() => {
// Delete temporary files
fse.removeSync(gitCloneDirectory);
client.end();
process.exit();
});